Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace page with app router #37

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Actions folder

This folder contains all the server actions that will be used in the app.

### How to organise this folder

The name of the file should be similar to the name of the page.
For example, if you want to create a server action for a page called `Dashboard`, the file should be called `dashboard.ts`. So that it is easy to find the actions for a specific page.
25 changes: 25 additions & 0 deletions src/actions/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use server';

export type PostType = {
id: number;
title: string;
completed: boolean;
};

export const getPosts = async () => {
// mimic api call
try {
const response = await fetch(
'https://jsonplaceholder.typicode.com/todos?_limit=5',
);
const data = await response.json();

return { posts: data as PostType[] };
} catch (error) {
return {
error: {
message: 'Something went wrong',
},
};
}
};
41 changes: 41 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Metadata, Viewport } from 'next';
import type { PropsWithChildren } from 'react';

import { CoreLayout } from '@/common/components/CoreLayout';
import { Favicon } from '@/common/components/FavIcon';
import { inter } from '@/common/fonts';
import '@/common/styles/globals.scss';

export const viewport: Viewport = {
themeColor: 'black',
};

export const metadata: Metadata = {
title: 'Your App Name | Home',
description: 'Your app description',
metadataBase: new URL('https://google.com'),
openGraph: {
title: 'Your App Name',
description: 'Your app description',
url: 'https://google.com',
siteName: 'Your App Name',
},
twitter: {
card: 'summary_large_image',
site: '@username',
creator: '@username',
},
};

const GlobalLayout = ({ children }: PropsWithChildren) => {
return (
<html className={[inter.variable].join(' ')} lang="en">
<Favicon />
<body>
<CoreLayout>{children}</CoreLayout>
</body>
</html>
);
};

export default GlobalLayout;
9 changes: 9 additions & 0 deletions src/app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Error404 = () => {
return (
<section className="grid h-full place-content-center">
Page not found
</section>
);
};

export default Error404;
11 changes: 11 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Posts } from '@/modules/Home/Posts';

const Homepage = async () => {
return (
<section className="grid h-full place-content-center">
<Posts />
</section>
);
};

export default Homepage;
46 changes: 0 additions & 46 deletions src/common/components/PageHead/PageHead.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/common/components/PageHead/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/common/fonts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './urbanist';
6 changes: 6 additions & 0 deletions src/common/fonts/urbanist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Urbanist } from 'next/font/google';

export const inter = Urbanist({
subsets: ['latin'],
variable: '--font-urbanist',
});
19 changes: 19 additions & 0 deletions src/modules/Home/Post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { PostType } from '@/actions/home';

interface PostProps {
post: PostType;
}

export const Post = ({ post }: PostProps) => {
return (
<div
key={post.id}
className="flex items-center justify-between gap-8 rounded-lg border border-transparent bg-zinc-100 p-4 transition-all hover:border-neutral-100 hover:bg-stone-50 hover:shadow-sm"
>
<h1 className="text-sm capitalize text-zinc-600">{post.title}</h1>
<p className="text-[9px]">{post.completed ? '✅' : '❌'}</p>
</div>
);
};
22 changes: 22 additions & 0 deletions src/modules/Home/Posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Post } from '@/modules/Home/Post';

import { getPosts } from '@/actions/home';

export const Posts = async () => {
const data = await getPosts();

if (data.posts) {
const posts = data.posts.map((post) => {
return <Post key={post.id} post={post} />;
});

return (
<div className="rounded-lg bg-zinc-50 p-4 shadow-md">
<h1 className="mb-4 text-center text-2xl">Posts</h1>
<div className="space-y-2">{posts}</div>
</div>
);
}

return <>{data.error.message}</>;
};
11 changes: 0 additions & 11 deletions src/pages/404.tsx

This file was deleted.

25 changes: 0 additions & 25 deletions src/pages/_app.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions src/pages/_document.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/pages/index.tsx

This file was deleted.

12 changes: 9 additions & 3 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import type { Config } from "tailwindcss";
import type { Config } from 'tailwindcss';
import { fontFamily } from 'tailwindcss/defaultTheme';

export default {
content: ["./src/**/*.{js,ts,jsx,tsx}"],
const tailwindConfig = {
content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {
fontFamily: {
sans: ['var(--font-urbanist)', 'Urbanist', ...fontFamily.sans],
},
colors: {},
},
},
plugins: [],
} satisfies Config;

export default tailwindConfig;
15 changes: 11 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
"jsx": "preserve",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
"@/*": ["src/*"],
},
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "./**/.*"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"**/*.js",
"./**/.*",
".next/types/**/*.ts",
],
"exclude": ["node_modules"],
}