5chan - The 4chan clone with NextJs
––– views
•
4 mins
18 Sep 2021
# Get the nextjs app template npx create-next-app 5chan --typescript cd 5chan pnpm install # The chakra-ui UI library, icons for chakra-ui & swr pnpm add @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4 @chakra-ui/icons swr zustand @dicebear/avatars @dicebear/micah formik
import { AppProps } from 'next/app'; import { ChakraProvider } from '@chakra-ui/react'; import '@/styles/globals.css'; export default function MyApp({ Component, pageProps }: AppProps) { return ( <ChakraProvider> <Component {...pageProps} /> </ChakraProvider> ); }
// types.ts //User State export interface UserStore { user: User | undefined; setUser: (user: User) => void; } // User Object export interface User { id: string; avatar: string; } // Posts export interface Post { CreatedAt?: Date; UpdatedAt?: Date; DeletedAt?: boolean; ID?: string; title: string; author: string; body: string; replies?: Reply[]; } // Post Reply export interface Reply { CreatedAt?: Date; UpdatedAt?: Date; DeletedAt?: boolean; ID?: string; author: string; body: string; postId: string; }
//user.ts import { customAlphabet } from 'nanoid/async'; import { User } from './types'; import { createAvatar } from '@dicebear/avatars'; import * as style from '@dicebear/micah'; const userKey = 'currentUid'; const createUser = async (): Promise<User> => { const nanoid = customAlphabet('0123456789', 10); const id = await nanoid(); const avatar = createAvatar(style, { seed: 'id', dataUri: true, }); const user: User = { id, avatar }; localStorage.setItem(userKey, JSON.stringify(user)); return user; }; export const getUser = async (): Promise<User> => { let result = localStorage.getItem(userKey); let user: User; if (!result) { return await createUser(); } user = JSON.parse(result) as User; return user; };
//stores.ts import create from 'zustand'; import { User, UserStore } from './types'; export const userStore = create<UserStore>((set) => ({ user: undefined, set((state) => { state.user = user; }), }));
//replies.ts import { Reply } from './types'; /// export const fetchReplies = async (url: string):Promise<Reply[]> => { const result = await fetch(url); if (result.status >= 400) { return []; } return (await result.json()) as Reply[]; }; /// export const postReply = async (reply: Reply): Promise<Reply | undefined> => { const apiUrl = `${process.env.NEXT_PUBLIC_API_URL!}/api/v1/replies`; const apiKey = process.env.NEXT_PUBLIC_API_KEY; if (!apiKey) { return; } const req: RequestInit = { method: `POST`, headers: { 'Content-Type': 'application/json', Authorization: apiKey }, body: JSON.stringify(reply), }; const result = await fetch(apiUrl, req); if (result.status >= 400) { return; } return (await result.json()) as Reply; };
import { Post } from './types'; /// export const fetchPosts = async (url: string): Promise<Post[]> => { const result = await fetch(url); if (result.status >= 400) { return []; } return (await result.json()) as Post[]; }; export const fetchPostById = async (url: string): Promise<Post | undefined> => { const result = await fetch(url); if (result.status >= 400) { return; } return (await result.json()) as Post; }; /// export const createPost = async (post: Post): Promise<Post | undefined> => { const apiUrl = `${process.env.NEXT_PUBLIC_API_URL!}/api/v1/posts`; const apiKey = process.env.NEXT_PUBLIC_API_KEY; if (!apiKey) { return; } const req: RequestInit = { method: `POST`, headers: { 'Content-Type': 'application/json', Authorization: apiKey }, body: JSON.stringify({ ...post }), }; const result = await fetch(apiUrl, req); if (result.status >= 400) { return; } return (await result.json()) as Post; }; /// export const deletePost = async (url: string): Promise<boolean> => { const apiKey = process.env.NEXT_PUBLIC_API_KEY; const req: RequestInit = { method: `DELETE`, headers: { Authorization: apiKey! }, }; const result = await fetch(url, req); if (result.status >= 400) { return false; } return true; };