Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blog): Show the two most recent blog posts, excluding the current one, at the end of each blog #48

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/[lang]/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { decodeMdxFilePathData } from '@/lib/utils';
import { Blog } from '@/components/blog/blog';
import { Locale } from '@/i18n-config';
import { getDictionary } from '@/get-dictionary';
import { getBlogPostItems } from '@/lib/blog';
import LocalePrettyUrlsCache from '@/lib/locale-pretty-urls-cache';

type Params = {
Expand Down Expand Up @@ -45,13 +46,21 @@ export async function generateStaticParams({ params }: Params) {

export default async function Page({ params }: Params) {
const blogPostItem = await generateBlogPostItem(params.lang, params.slug);

const dictionary = await getDictionary(params.lang);

// Related blog posts, for now, just the two most recent ones, excluding the current one.
const relatedPosts: BlogPostItem[] = getBlogPostItems(params.lang)
.filter(
(post) => post.data.published && post.urlPath !== blogPostItem.urlPath
)
.sort((a, b) => (a.data.publishedDate > b.data.publishedDate ? -1 : 1))
.slice(0, 2);

return (
<div className="relative">
<Blog
blogPostItem={blogPostItem}
relatedPosts={relatedPosts}
dictionary={dictionary}
lang={params.lang}
></Blog>
Expand All @@ -77,7 +86,7 @@ async function generateBlogPostItem(lang: Locale, slug?: string) {
content: contentHtml,
data: data as BlogPostItemData,
filePath: `${slug}.mdx`,
urlPath: `${lang}/blog/${slug}`,
urlPath: `/${lang}/blog/${slug}`,
};
return blogPostItem;
}
4 changes: 3 additions & 1 deletion app/[lang]/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export default async function Page({
</section>
<div className="relative">
<div className="gjs-gradient-background absolute w-full h-full" />
<BlogList posts={posts}></BlogList>
<div className="container mx-auto py-10">
<BlogList posts={posts}></BlogList>
</div>
</div>
</div>
);
Expand Down
30 changes: 18 additions & 12 deletions components/blog-list/blog-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import { BlogPostItem } from '@/types/blog';
import BlogListItem from './blog-list-item';
import { cn } from '@/lib/utils';

export default function BlogList({ posts }: { posts: BlogPostItem[] }) {
export default function BlogList({
posts,
className,
}: {
posts: BlogPostItem[];
className?: string;
}) {
posts = [...posts]
.filter((post) => post.data.published)
.sort((a, b) => (a.data.publishedDate > b.data.publishedDate ? -1 : 1));

return (
<>
<div className="relative w-full h-full">
<div className="container mx-auto py-10">
<div className="py-4 grid grid-cols-1 md:grid-cols-2 gap-12">
{posts.map((post) => (
<BlogListItem key={post.filePath} post={post}></BlogListItem>
))}
</div>
</div>
</div>
</>
<div
className={cn(
'relative grid h-full w-full grid-cols-1 gap-12 py-4 md:grid-cols-3',
className
)}
>
{posts.map((post) => (
<BlogListItem key={post.filePath} post={post}></BlogListItem>
))}
</div>
);
}
9 changes: 8 additions & 1 deletion components/blog/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import Comments from '../comments';

import './blog.scss'; // TODO: This styles will be global even only imported here. We should
import Link from 'next/link';
import BlogList from '../blog-list/blog-list';

type Props = {
blogPostItem: BlogPostItem;
relatedPosts: BlogPostItem[];
dictionary: LangDictionary;
lang: string;
};

export function Blog({ blogPostItem, dictionary, lang }: Props) {
export function Blog({ blogPostItem, relatedPosts, dictionary, lang }: Props) {
const host = `https://gironajs.com`; // TODO: Put host value on environment variables
const getFullHref = (post: BlogPostItem) => `${host}/${post.urlPath}`;
return (
Expand Down Expand Up @@ -55,6 +57,11 @@ export function Blog({ blogPostItem, dictionary, lang }: Props) {
</div>

<Comments lang={lang} />

<div className="my-10">
<h2 className="my-2 flex-1">{dictionary.blog.related_posts_title}</h2>
<BlogList posts={relatedPosts} className="md:grid-cols-2"></BlogList>
</div>
</div>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion dictionaries/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"subtitle_1": "Aquí pots trobar totes les entrades del blog publicades per GironaJS",
"subtitle_2": "Qualsevol contribució és benvinguda, així que si vols escriure una entrada, si us plau, contacta'ns!",
"edit_github": "Has trobat una errada? ",
"edit_github_link": "Edita-la al Github"
"edit_github_link": "Edita-la al Github",
"related_posts_title": "Altres blogs que podrien interessar-te..."
},
"map": {
"title": "GironaJS - Mapa 3D de la ciutat",
Expand Down
3 changes: 2 additions & 1 deletion dictionaries/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"subtitle_1": "Here you can find all the blog posts published by GironaJS",
"subtitle_2": "Any contribution is welcome, so if you want to write a post, please contact us!",
"edit_github": "Have you found a mistake? ",
"edit_github_link": "Edit it on Github!"
"edit_github_link": "Edit it on Github!",
"related_posts_title": "Other blogs that might interest you..."
},
"map": {
"title": "GironaJS - 3D City Map",
Expand Down
Loading