Skip to content

Commit 89dd92d

Browse files
committed
made the basic article view
1 parent 94afa68 commit 89dd92d

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

Diff for: frontend/pages/api/blog/[slug].ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import type { NextApiRequest, NextApiResponse } from 'next'
22
import admin from '../../../src/firebase/admin';
3+
import { Content } from '../../blog/[slug]';
34
type Data = {
45
slug: string,
56
content?: any,
67
error?: string
78
}
89

9-
export default async (req: NextApiRequest, res: NextApiResponse<Data>) => {
10+
const api = async (req: NextApiRequest, res: NextApiResponse<Data>) => {
1011
const firestore = admin.firestore();
1112
const slug = req.query.slug as string;
1213
try {
1314

14-
const doc = await firestore.collection('blogs').where("slug", "==", slug).get();
15+
const blog = await getBlog(slug)
16+
res.status(200).json({ slug: slug, content: blog })
17+
} catch (error) {
18+
res.status(404).json(
19+
{ error: error.message == "Cannot read property 'data' of undefined" ? "blog not found" : error.message, slug: slug });
20+
}
21+
}
22+
export default api;
23+
export const getBlog = async (slug:string):Promise<Content> =>{
24+
const firestore = admin.firestore();
25+
const doc = await firestore.collection('blogs').where("slug", "==", slug).get();
1526
const blog = doc.docs[0].data();
1627
if (blog.user) {
1728
blog.user = (await firestore.collection('users').doc(blog.user).get()).data();
1829
}
1930
if (blog.time) {
20-
blog.time = new Date(blog.time._seconds * 1000);
31+
blog.time = new Date(blog.time._seconds * 1000).toString();
2132
}
22-
console.log(blog)
23-
res.status(200).json({ slug: slug, content: blog })
24-
} catch (error) {
25-
res.status(404).json(
26-
{ error: error.message == "Cannot read property 'data' of undefined" ? "blog not found" : error.message, slug: slug });
27-
}
33+
return blog as Content;
2834
}

Diff for: frontend/pages/blog/[slug].tsx

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { getBlog } from "../api/blog/[slug]";
2+
3+
export interface Root {
4+
slug: string;
5+
content: Content;
6+
}
7+
8+
export interface Content {
9+
time: string;
10+
content: string;
11+
slug: string;
12+
user: User;
13+
}
14+
15+
export interface User {
16+
profile: string;
17+
name: string;
18+
}
19+
20+
const Blog = ({ content, found }: { content?: Content; found: boolean }) => {
21+
console.log(content);
22+
if (found) {
23+
return (
24+
<figure className="md:flex flex-col bg-gray-100 rounded-xl p-8">
25+
<div className="pt-6 text-left space-y-4">
26+
<blockquote>
27+
<p className="text-lg font-semibold">
28+
{content.content}
29+
</p>
30+
</blockquote>
31+
32+
<figcaption className="font-medium flex flex-row justify-start">
33+
<img
34+
className="w-12 h-12 rounded-full mx-1"
35+
src={content.user.profile}
36+
alt={content.user.name}
37+
width="64"
38+
height="96"
39+
/>
40+
<div>
41+
<div className="text-cyan-600">
42+
{content.user.name}
43+
</div>
44+
<div className="text-gray-500">
45+
{new Date(content.time).toDateString()}
46+
</div>
47+
</div>
48+
</figcaption>
49+
</div>
50+
</figure>
51+
);
52+
}
53+
return (
54+
<div className="flex justify-center align-middle">
55+
Aritcle Not Found
56+
</div>
57+
);
58+
};
59+
export async function getServerSideProps(context) {
60+
const slug = context.params.slug;
61+
try {
62+
const blog = await getBlog(slug);
63+
return {
64+
props: {
65+
found: true,
66+
content: blog,
67+
}, // will be passed to the page component as props
68+
};
69+
} catch (error) {
70+
return { props: { found: false } };
71+
}
72+
}
73+
export default Blog;

0 commit comments

Comments
 (0)