|
| 1 | +import http from 'http'; |
| 2 | +import { |
| 3 | + getSite, |
| 4 | + getAuthorProfile, |
| 5 | + postsByUser, |
| 6 | +} from '@/services/serversideApi'; |
| 7 | +import { NextSeo } from 'next-seo'; |
| 8 | +import Layout from '@/layout/Layout'; |
| 9 | +import { Site } from '@/models/site.model'; |
| 10 | +import { UserInfoExtended } from '@/models/user.model'; |
| 11 | +import { Post, PostType } from '@/models/post.model'; |
| 12 | +import PostsCards from '@/components/PostsCards'; |
| 13 | +import AuthorCard from '@/components/authors/AuthorCard'; |
| 14 | + |
| 15 | +export default function AuthorPage({ |
| 16 | + site, |
| 17 | + author, |
| 18 | + courses, |
| 19 | + tutorials, |
| 20 | + posts, |
| 21 | +}: { |
| 22 | + site: Site | null; |
| 23 | + author: UserInfoExtended; |
| 24 | + courses: Post[] | null; |
| 25 | + tutorials: Post[] | null; |
| 26 | + posts: Post[] | null; |
| 27 | +}): JSX.Element { |
| 28 | + return ( |
| 29 | + <Layout site={site}> |
| 30 | + <NextSeo |
| 31 | + title={`${author.displayName ? author.displayName : ''} | CodingCatDev`} |
| 32 | + canonical={`https://codingcat.dev/authors/`} |
| 33 | + ></NextSeo> |
| 34 | + <section className="grid grid-cols-1 gap-20 p-4 sm:p-10 place-items-center"> |
| 35 | + <AuthorCard author={author} /> |
| 36 | + </section> |
| 37 | + {courses && courses.length > 0 && ( |
| 38 | + <section className="grid w-full gap-10 px-4 mx-auto xl:px-10"> |
| 39 | + <h2 className="mt-4 text-4xl text-primary-900 lg:text-5xl"> |
| 40 | + Courses |
| 41 | + </h2> |
| 42 | + {courses && <PostsCards posts={courses} />} |
| 43 | + </section> |
| 44 | + )} |
| 45 | + {tutorials && tutorials.length > 0 && ( |
| 46 | + <section className="grid w-full gap-10 px-4 mx-auto xl:px-10"> |
| 47 | + <h2 className="mt-4 text-4xl text-primary-900 lg:text-5xl"> |
| 48 | + Tutorials |
| 49 | + </h2> |
| 50 | + {tutorials && <PostsCards posts={tutorials} />} |
| 51 | + </section> |
| 52 | + )} |
| 53 | + {posts && posts.length > 0 && ( |
| 54 | + <section className="grid w-full gap-10 px-4 mx-auto xl:px-10"> |
| 55 | + <h2 className="mt-4 text-4xl text-primary-900 lg:text-5xl"> |
| 56 | + Blog Posts |
| 57 | + </h2> |
| 58 | + {posts && <PostsCards posts={posts} />} |
| 59 | + </section> |
| 60 | + )} |
| 61 | + </Layout> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export async function getServerSideProps({ |
| 66 | + params, |
| 67 | + req, |
| 68 | +}: { |
| 69 | + params: { authorPath: string }; |
| 70 | + req: http.IncomingMessage; |
| 71 | +}): Promise< |
| 72 | + | { |
| 73 | + props: { |
| 74 | + site: Site | null; |
| 75 | + author: UserInfoExtended | null; |
| 76 | + courses: Post[] | null; |
| 77 | + tutorials: Post[] | null; |
| 78 | + posts: Post[] | null; |
| 79 | + }; |
| 80 | + } |
| 81 | + | { redirect: { destination: string; permanent: boolean } } |
| 82 | + | { notFound: boolean } |
| 83 | +> { |
| 84 | + const { authorPath } = params; |
| 85 | + |
| 86 | + if (!authorPath) { |
| 87 | + return { |
| 88 | + notFound: true, |
| 89 | + }; |
| 90 | + } |
| 91 | + const site = await getSite(); |
| 92 | + const author = (await getAuthorProfile(authorPath)) as UserInfoExtended; |
| 93 | + const courses = await postsByUser(PostType.course, authorPath); |
| 94 | + const tutorials = await postsByUser(PostType.tutorial, authorPath); |
| 95 | + const posts = await postsByUser(PostType.post, authorPath); |
| 96 | + |
| 97 | + if (!author) { |
| 98 | + console.log('Author not found'); |
| 99 | + return { |
| 100 | + notFound: true, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + props: { |
| 106 | + site, |
| 107 | + author, |
| 108 | + courses, |
| 109 | + tutorials, |
| 110 | + posts, |
| 111 | + }, |
| 112 | + }; |
| 113 | +} |
0 commit comments