From 7f26ce5e2f376da3552baadd569aa41c0d400522 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Sat, 6 Apr 2024 00:27:12 +0600 Subject: [PATCH] fix: implement author page --- src/components/NavigationDropdown.tsx | 3 ++- src/lib/video.ts | 22 +++++++++++++------ src/pages/authors/[authorId].astro | 19 ++++++++-------- src/pages/authors/[authorId].json.ts | 31 +++++++++++---------------- 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/components/NavigationDropdown.tsx b/src/components/NavigationDropdown.tsx index ac05bf95c..4d7fb8faa 100644 --- a/src/components/NavigationDropdown.tsx +++ b/src/components/NavigationDropdown.tsx @@ -68,12 +68,13 @@ export function NavigationDropdown() { })} onClick={() => setIsOpen(true)} onMouseOver={() => setIsOpen(true)} + aria-label="Open Navigation Dropdown" >
{ const allVideos = await getAllVideos(); @@ -75,10 +75,20 @@ export async function getAllVideos(): Promise { } export async function getVideoById(id: string): Promise { - const video = await import(`../data/videos/${id}.md`); + const videoFilesMap: Record = + import.meta.glob('../data/videos/*.md', { + eager: true, + }); + + const videoFile = Object.values(videoFilesMap).find((videoFile) => { + return videoPathToId(videoFile.file) === id; + }); + if (!videoFile) { + throw new Error(`Video with ID ${id} not found`); + } return { - ...video, - id: videoPathToId(video.file), + ...videoFile, + id: videoPathToId(videoFile.file), }; } diff --git a/src/pages/authors/[authorId].astro b/src/pages/authors/[authorId].astro index 1731ec9da..ae01a9d6e 100644 --- a/src/pages/authors/[authorId].astro +++ b/src/pages/authors/[authorId].astro @@ -2,24 +2,23 @@ import BaseLayout from '../../layouts/BaseLayout.astro'; import AstroIcon from '../../components/AstroIcon.astro'; import { getGuidesByAuthor } from '../../lib/guide'; -import { getAllVideos, getVideosByAuthor } from '../../lib/video'; +import { getVideosByAuthor } from '../../lib/video'; import GuideListItem from '../../components/GuideListItem.astro'; -import { getAuthorById, getAuthorIds } from '../../lib/author'; +import { getAuthorById } from '../../lib/author'; import VideoListItem from '../../components/VideoListItem.astro'; interface Params extends Record {} -export async function getStaticPaths() { - const authorIds = await getAuthorIds(); - - return authorIds.map((authorId) => ({ - params: { authorId }, - })); -} - const { authorId } = Astro.params; +if (!authorId) { + return Astro.redirect('/404'); +} const author = await getAuthorById(authorId); +if (!author) { + return Astro.redirect('/404'); +} + const authorFrontmatter = author.frontmatter; const guides = await getGuidesByAuthor(authorId); diff --git a/src/pages/authors/[authorId].json.ts b/src/pages/authors/[authorId].json.ts index 4193292f7..24851c7e7 100644 --- a/src/pages/authors/[authorId].json.ts +++ b/src/pages/authors/[authorId].json.ts @@ -1,25 +1,20 @@ import type { APIRoute } from 'astro'; -import { getAuthorById, getAuthorIds } from '../../lib/author'; +import { getAuthorById } from '../../lib/author'; -export async function getStaticPaths() { - const authorIds = await getAuthorIds(); - - return await Promise.all( - authorIds.map(async (authorId) => { - const authorDetails = await getAuthorById(authorId); +export const GET: APIRoute = async function ({ params, request, props }) { + const { authorId } = params as { authorId: string }; - return { - params: { authorId }, - props: { - authorDetails: authorDetails?.frontmatter || {}, - }, - }; - }), - ); -} + const authorDetails = await getAuthorById(authorId); + if (!authorDetails) { + return new Response(JSON.stringify({ error: 'Not found' }), { + status: 404, + headers: { + 'Content-Type': 'application/json', + }, + }); + } -export const GET: APIRoute = async function ({ params, request, props }) { - return new Response(JSON.stringify(props.authorDetails), { + return new Response(JSON.stringify(authorDetails?.frontmatter), { status: 200, headers: { 'Content-Type': 'application/json',