diff --git a/app/caramel/[id]/_components/Card.tsx b/app/caramel/[id]/_components/Card.tsx new file mode 100644 index 0000000..16c7d1c --- /dev/null +++ b/app/caramel/[id]/_components/Card.tsx @@ -0,0 +1,35 @@ +import React from 'react' + +export type CardProps = { + by: string + descendants: number + id: number + score: number + time: number + title: string + type: 'job' | 'story' | 'comment' | 'poll' + url: string +} + +const Card: React.FC = ({ by, descendants, id, score, time, title, type, url }) => { + return ( +
+

+ {id}: {title} +

+

By: {by}

+

Score: {score}

+

Type: {type}

+

Time: {new Date(time * 1000).toLocaleString()}

+

+ URL:{' '} + + {url} + +

+

Got {descendants} comments

+
+ ) +} + +export default Card diff --git a/app/caramel/[id]/page.tsx b/app/caramel/[id]/page.tsx new file mode 100644 index 0000000..e01eb91 --- /dev/null +++ b/app/caramel/[id]/page.tsx @@ -0,0 +1,17 @@ +import Link from 'next/link' +import Card from './_components/Card' +// before routing: import Card, { CardProps } from '@/components/Card' + +export default async function Article({ params }: { params: { id: string } }) { + const res = await fetch(`https://hacker-news.firebaseio.com/v0/item/${params.id}.json`) + const article = await res.json() + + return ( +
+ {article ? :
} + + Back + +
+ ) +} diff --git a/pages/caramel/[id].tsx b/pages/caramel/[id].tsx deleted file mode 100644 index 7fda074..0000000 --- a/pages/caramel/[id].tsx +++ /dev/null @@ -1,29 +0,0 @@ -import Card, { CardProps } from '@/components/Card' -import Link from 'next/link' -import { useRouter } from 'next/router' -import { useEffect, useState } from 'react' - -export default function Article() { - const router = useRouter() - const { id } = router.query - - const [article, setArticle] = useState() - - useEffect(() => { - if (!id) return - ;(async () => { - const res = await fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`) - const article = await res.json() - setArticle(article) - })() - }, []) - - return ( -
- {article ? :
} - - Back - -
- ) -}