Skip to content

Commit f71d311

Browse files
authored
Merge pull request #18 from YogPanjarale/main
Made basic Blog :thisisfine:
2 parents 533783a + b53f356 commit f71d311

File tree

5 files changed

+1083
-10
lines changed

5 files changed

+1083
-10
lines changed

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@heroicons/react": "^1.0.1",
12+
"firebase-admin": "^9.11.0",
1213
"graphql-code-generator": "^0.18.2",
1314
"next": "11.0.1",
1415
"react": "17.0.2",

frontend/pages/api/blog/[slug].ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { NextApiRequest, NextApiResponse } from 'next'
2+
import admin from '../../../src/firebase/admin';
3+
import { Content } from '../../blog/[slug]';
4+
type Data = {
5+
slug: string,
6+
content?: any,
7+
error?: string
8+
}
9+
10+
const api = async (req: NextApiRequest, res: NextApiResponse<Data>) => {
11+
const firestore = admin.firestore();
12+
const slug = req.query.slug as string;
13+
try {
14+
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();
26+
const blog = doc.docs[0].data();
27+
if (blog.user) {
28+
blog.user = (await firestore.collection('users').doc(blog.user).get()).data();
29+
}
30+
if (blog.time) {
31+
blog.time = new Date(blog.time._seconds * 1000).toString();
32+
}
33+
return blog as Content;
34+
}

frontend/pages/blog/[slug].tsx

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

frontend/src/firebase/admin.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as admin from 'firebase-admin';
2+
3+
var serviceAccount =JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT as string)
4+
// console.log(serviceAccount,typeof serviceAccount);
5+
if (admin.apps.length<=0){
6+
admin.initializeApp({
7+
credential: admin.credential.cert(serviceAccount),
8+
databaseURL: "https://farm-helper-app-mqtt.firebaseio.com/"
9+
});
10+
}
11+
12+
export default admin

0 commit comments

Comments
 (0)