├── .eslintrc.json ├── src ├── app │ ├── globals.css │ ├── posts │ │ ├── Posts.module.css │ │ ├── page.js │ │ └── [id] │ │ │ └── page.js │ ├── favicon.ico │ ├── users │ │ └── page.js │ ├── page.js │ ├── api │ │ └── blogs │ │ │ └── route.js │ └── layout.js └── components │ └── Users │ ├── Users.module.css │ └── Users.js ├── jsconfig.json ├── next.config.js ├── postcss.config.js ├── .gitignore ├── tailwind.config.js ├── public ├── vercel.svg └── next.svg ├── package.json ├── README.md └── fakeDB.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /src/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | -------------------------------------------------------------------------------- /src/app/posts/Posts.module.css: -------------------------------------------------------------------------------- 1 | .header_text{ 2 | font-size: 50px; 3 | color: red; 4 | } -------------------------------------------------------------------------------- /src/components/Users/Users.module.css: -------------------------------------------------------------------------------- 1 | .header_text{ 2 | font-size: 40px; 3 | color: green; 4 | } -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "paths": { 4 | "@/*": ["./src/*"] 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {} 3 | 4 | module.exports = nextConfig 5 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /src/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProgrammingHero1/B8-Next.Js-Next-Level-Data-Fetching/HEAD/src/app/favicon.ico -------------------------------------------------------------------------------- /src/app/users/page.js: -------------------------------------------------------------------------------- 1 | import Users from "@/components/Users/Users"; 2 | 3 | const UsersPage = () => { 4 | return ( 5 |
6 | 7 |
8 | ); 9 | }; 10 | 11 | export default UsersPage; 12 | -------------------------------------------------------------------------------- /src/app/page.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | const HomePage = () => { 4 | return ( 5 |
6 |

7 | Welcome to next level data fetching module 8 |

9 |
10 | ); 11 | }; 12 | 13 | export default HomePage; 14 | -------------------------------------------------------------------------------- /src/app/api/blogs/route.js: -------------------------------------------------------------------------------- 1 | import { NextResponse } from "next/server"; 2 | 3 | // GET API 4 | export const GET = () => { 5 | return NextResponse.json({ message: "Hello, world!" }); 6 | }; 7 | 8 | // POST API 9 | export const POST = () => {}; 10 | 11 | // PATCH API 12 | export const PATCH = () => {}; 13 | 14 | // DELETE API 15 | export const DELETE = () => {}; 16 | -------------------------------------------------------------------------------- /src/app/layout.js: -------------------------------------------------------------------------------- 1 | import { Inter } from "next/font/google"; 2 | import "./globals.css"; 3 | 4 | const inter = Inter({ subsets: ["latin"] }); 5 | 6 | export const metadata = { 7 | title: "Create Next App", 8 | description: "Generated by create next app", 9 | }; 10 | 11 | export default function RootLayout({ children }) { 12 | return ( 13 | 14 | {children} 15 | 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 | .yarn/install-state.gz 8 | 9 | # testing 10 | /coverage 11 | 12 | # next.js 13 | /.next/ 14 | /out/ 15 | 16 | # production 17 | /build 18 | 19 | # misc 20 | .DS_Store 21 | *.pem 22 | 23 | # debug 24 | npm-debug.log* 25 | yarn-debug.log* 26 | yarn-error.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | next-env.d.ts 37 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | "./src/pages/**/*.{js,ts,jsx,tsx,mdx}", 5 | "./src/components/**/*.{js,ts,jsx,tsx,mdx}", 6 | "./src/app/**/*.{js,ts,jsx,tsx,mdx}", 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | "gradient-radial": "radial-gradient(var(--tw-gradient-stops))", 12 | "gradient-conic": 13 | "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))", 14 | }, 15 | }, 16 | }, 17 | plugins: [require("daisyui")], 18 | }; 19 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-level-data-fetching", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "json-server": "json-server --watch fakeDB.json --port 5000", 8 | "build": "next build", 9 | "start": "next start", 10 | "lint": "next lint" 11 | }, 12 | "dependencies": { 13 | "json-server": "^0.17.4", 14 | "next": "14.0.3", 15 | "react": "^18", 16 | "react-dom": "^18" 17 | }, 18 | "devDependencies": { 19 | "autoprefixer": "^10.0.1", 20 | "daisyui": "^4.4.14", 21 | "eslint": "^8", 22 | "eslint-config-next": "14.0.3", 23 | "postcss": "^8", 24 | "tailwindcss": "^3.3.0" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/components/Users/Users.js: -------------------------------------------------------------------------------- 1 | "use client"; 2 | import React, { useEffect, useState } from "react"; 3 | import styles from "./Users.module.css"; 4 | 5 | const Users = () => { 6 | const [users, setUsers] = useState([]); 7 | 8 | useEffect(() => { 9 | fetch("https://jsonplaceholder.typicode.com/users") 10 | .then((res) => res.json()) 11 | .then((data) => setUsers(data)); 12 | }, []); 13 | 14 | return ( 15 |
16 |

Total Users:{users.length}

17 | {users.map((user) => ( 18 |
22 |
23 |

{user.name}

24 |

{user.email}

25 |
26 |
27 | ))} 28 |
29 | ); 30 | }; 31 | 32 | export default Users; 33 | -------------------------------------------------------------------------------- /src/app/posts/page.js: -------------------------------------------------------------------------------- 1 | import Link from "next/link"; 2 | import styles from "./Posts.module.css"; 3 | 4 | const PostsPage = async () => { 5 | const res = await fetch("http://localhost:5000/posts", { 6 | cache: "force-cache", 7 | }); 8 | const posts = await res.json(); 9 | 10 | // console.log(posts); 11 | 12 | return ( 13 |
14 |

15 | Total Number of Posts: {posts.length}{" "} 16 |

17 | {posts.map((post) => ( 18 |
22 |
23 |

{post.title}

24 |

{post.description}

25 |

Likes: {post.likesCount}

26 |
27 | 28 | 29 | 30 |
31 |
32 |
33 | ))} 34 |
35 | ); 36 | }; 37 | 38 | export default PostsPage; 39 | -------------------------------------------------------------------------------- /src/app/posts/[id]/page.js: -------------------------------------------------------------------------------- 1 | import Link from "next/link"; 2 | 3 | export async function generateStaticParams() { 4 | const res = await fetch("http://localhost:5000/posts"); 5 | const posts = await res.json(); 6 | 7 | const ids = posts.map((post) => { 8 | return { 9 | id: post.id + "", 10 | }; 11 | }); 12 | // console.log(ids); 13 | 14 | return ids; 15 | } 16 | 17 | const DetailPage = async ({ params }) => { 18 | // console.log(params.id); 19 | const res = await fetch(`http://localhost:5000/posts/${params.id}`); 20 | const post = await res.json(); 21 | // console.log(post); 22 | return ( 23 |
24 |
28 |
29 |

{post.title}

30 |

{post.description}

31 |

Likes: {post.likesCount}

32 |
33 | 34 | 35 | 36 |
37 |
38 |
39 |
40 | ); 41 | }; 42 | 43 | export default DetailPage; 44 | -------------------------------------------------------------------------------- /public/next.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | # or 12 | pnpm dev 13 | # or 14 | bun dev 15 | ``` 16 | 17 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 18 | 19 | You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file. 20 | 21 | This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. 22 | 23 | ## Learn More 24 | 25 | To learn more about Next.js, take a look at the following resources: 26 | 27 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 28 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 29 | 30 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 31 | 32 | ## Deploy on Vercel 33 | 34 | 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. 35 | 36 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 37 | -------------------------------------------------------------------------------- /fakeDB.json: -------------------------------------------------------------------------------- 1 | { 2 | "posts": [ 3 | { 4 | "id": 1, 5 | "title": "Exploring all the Universe", 6 | "description": "Embark on a cosmic journey through space, discovering celestial wonders and unraveling the mysteries of distant galaxies. Join us as we delve into the vastness of the cosmos.", 7 | "likesCount": 1500 8 | }, 9 | { 10 | "id": 2, 11 | "title": "The Art of Photography", 12 | "description": "Capture moments frozen in time with the artistry of photography. Learn the techniques, perspectives, and nuances that make every snapshot a unique masterpiece.", 13 | "likesCount": 1100 14 | }, 15 | { 16 | "id": 3, 17 | "title": "Healthy Eating Habits", 18 | "description": "Discover the keys to maintaining a balanced diet and nourishing your body with wholesome foods. Explore recipes, nutrition tips, and sustainable eating habits.", 19 | "likesCount": 980 20 | }, 21 | { 22 | "id": 4, 23 | "title": "Mastering Productivity", 24 | "description": "Unlock your full potential and boost productivity with effective strategies, time management techniques, and tools to achieve your goals efficiently.", 25 | "likesCount": 1450 26 | }, 27 | { 28 | "id": 5, 29 | "title": "DIY Home Improvement", 30 | "description": "Transform your living space with creative do-it-yourself home improvement projects. Get inspired and learn practical tips for renovations and decor.", 31 | "likesCount": 670 32 | }, 33 | { 34 | "id": 6, 35 | "title": "Mindfulness Meditation", 36 | "description": "Embrace tranquility and inner peace through mindfulness meditation practices. Experience the benefits of mindfulness for mental well-being and relaxation.", 37 | "likesCount": 1150 38 | } 39 | ] 40 | } 41 | --------------------------------------------------------------------------------