Skip to content

Commit b423881

Browse files
Next: Feedback Backend API Project
1 parent 20dc1d9 commit b423881

File tree

16 files changed

+2174
-0
lines changed

16 files changed

+2174
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
16+
17+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
18+
19+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":"2022-09-22T00:38:24.569Z","email":"[email protected]","feedback":"sas"},{"id":"2022-09-22T00:38:46.839Z","email":"[email protected]","feedback":"Hi on Chai?"},{"id":"2022-09-22T00:45:56.291Z","email":"[email protected]","feedback":"Hi on Coffee?"}]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
swcMinify: true,
5+
}
6+
7+
module.exports = nextConfig
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "nextjs-backend-apis",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"next": "12.3.1",
13+
"react": "18.2.0",
14+
"react-dom": "18.2.0"
15+
},
16+
"devDependencies": {
17+
"eslint": "8.23.1",
18+
"eslint-config-next": "12.3.1"
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../styles/globals.css'
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getData, getPath } from ".";
2+
3+
const handler = (req, res) => {
4+
const { feedbackId } = req.query;
5+
6+
const path = getPath();
7+
const data = getData(path);
8+
9+
const feedback = data.find((fb) => fb.id === feedbackId);
10+
11+
if (!feedback) {
12+
return res.status(404).json({ error: "Feedback not found!" });
13+
}
14+
15+
res.status(200).json(feedback);
16+
};
17+
18+
export default handler;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
export const getPath = () => {
5+
return path.join(process.cwd(), "data", "feedback.json");
6+
};
7+
8+
export const getData = (path) => {
9+
const fileData = fs.readFileSync(path);
10+
const data = JSON.parse(fileData);
11+
12+
return data;
13+
};
14+
15+
export default function handler(req, res) {
16+
const { method } = req;
17+
18+
if (method == "GET") {
19+
const path = getPath();
20+
const feedbackData = getData(path);
21+
22+
return res.status(200).json({ feedback: feedbackData });
23+
} else if (method == "POST") {
24+
const { email, feedback } = req.body;
25+
const path = getPath();
26+
const data = getData(path);
27+
28+
const newFeedback = {
29+
id: new Date().toISOString(),
30+
email,
31+
feedback,
32+
};
33+
34+
data.push(newFeedback);
35+
fs.writeFileSync(path, JSON.stringify(data));
36+
37+
return res.status(200).json({ message: "Success", feedback: data });
38+
} else {
39+
res.status(200).json({ error: "Invalid Method" });
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Fragment, useState } from "react";
2+
import { getPath, getData } from "./api/feedback/index";
3+
4+
// DATA FROM APIs:
5+
// While using data from API server in getStaticProps or getServerSideProps
6+
// It is advisable to directly call the code running on the server
7+
// Instead of calling the data through fetch/API
8+
// This increases performance and speed
9+
10+
// BIND()
11+
// The bind() methods can be used to assign "this" value to the function
12+
// And also assign it arguments on run-time
13+
14+
const FeedbackListPage = (props) => {
15+
const [feedbackDetail, setFeedbackDetails] = useState();
16+
17+
const showDetails = (id) => {
18+
fetch(`/api/feedback/${id}`)
19+
.then((response) => response.json())
20+
.then((data) => {
21+
setFeedbackDetails(data);
22+
})
23+
.catch(console.log);
24+
};
25+
26+
return (
27+
<Fragment>
28+
{feedbackDetail && <p>{feedbackDetail.email}</p>}
29+
<ul>
30+
{props.feedbackData.map((feedback) => {
31+
return (
32+
<Fragment>
33+
<li key={feedback.id}>{feedback.feedback}</li>
34+
<button onClick={showDetails.bind(null, feedback.id)}>
35+
Details
36+
</button>
37+
</Fragment>
38+
);
39+
})}
40+
</ul>
41+
</Fragment>
42+
);
43+
};
44+
45+
const getStaticProps = (_) => {
46+
const path = getPath();
47+
const data = getData(path);
48+
49+
return {
50+
props: {
51+
feedbackData: data,
52+
},
53+
revalidate: 60,
54+
};
55+
};
56+
57+
export default FeedbackListPage;
58+
export { getStaticProps };

0 commit comments

Comments
 (0)