Built Twitter Like App - 12

Install toast

1
npm install react-hot-toast

Add Register Logic in Modal

modify file pages/_app.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import type { AppProps } from 'next/app';
import { Toaster } from 'react-hot-toast';
import { SessionProvider } from 'next-auth/react';

import Layout from '../components/Layout'
import LoginModal from '../components/modals/LoginModal'
import RegisterModal from '../components/modals/RegisterModal'
import '../styles/globals.css';

export default function App({ Component, pageProps }: AppProps) {
return (
<SessionProvider session={ pageProps.session }>
<Toaster />
<RegisterModal />
<LoginModal />
<Layout>
<Component {...pageProps} />
</Layout>
</SessionProvider>
)
}
More...

Built Twitter Like App - 11

Install SWR & axios

1
2
npm install swr
npm install axios

Add Hooks for CurrentUser

Add file libs/fetcher.ts

1
2
3
4
5
import axios from "axios";

const fetcher = (url: string) => axios.get(url).then( (res) => res.data);

export default fetcher;

Add file hooks/useCurrentUser.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import useSWR from 'swr';

import fetcher from '../libs/fetcher';

const useCurrentUser = () => {
const { data, error, isLoading, mutate } = useSWR('/api/current', fetcher)

return {
data,
error,
isLoading,
mutate
}
};

export default useCurrentUser;

Built Twitter Like App - 10

Add Register Function Code

Add file pages/api/register.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import bcrypt, { compareSync } from 'bcrypt';

import prisma from '../../libs/prismadb';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if ( req.method !== 'POST'){
return res.status(405).end();
}

try{
const { email,username, name, password } = req.body

const hashedPassword = await bcrypt.hash(password, 12);

const user = await prisma.user.create({
data: {
email,
username,
name,
hashedPassword
}
});

return res.status(200).json(user)
} catch (error){
console.log(error);
return res.status(400).end();
}
}
More...

Built Twitter Like App - 7

Install Zustand

1
npm install zustand

Add Hooks

Add file hooks/useLoginModal.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { create } from "zustand";

interface LoginModalStore{
isOpen: boolean;
onOpen: ()=> void;
onClose: ()=> void;
};

const useLoginModal = create<LoginModalStore>((set) =>({
isOpen: true,
onOpen: () => set({ isOpen: true}),
onClose: () => set({ isOpen: false})
}));

export default useLoginModal;
More...

Built Twitter Like App - 6

Add ModalBox

Modify file pages/_app.tsx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import '../styles/globals.css'
import type { AppProps } from 'next/app'

import Layout from '../components/Layout'
import ModalBox from '../components/ModalBox'

export default function App({ Component, pageProps }: AppProps) {
return (
<>
<ModalBox actionLabel='Submit' isOpen title='Test Modal' />
<Layout>
<Component {...pageProps} />
</Layout>
</>
)
}

More...

Built Twitter Like App - 4

Add FollowBar

Modify file conmponent/Layout.tsx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import FollowBar from "./layout/FollowBar";
import Sidebar from "./layout/Siderbar";

interface LayoutProps{
children: React.ReactNode;
}

const Layout:React.FC<LayoutProps> = ({ children }) => {
return (
<div className="h-screen bg-black">
<div className="container h-full mx-auto xl:px-30 max-w-6xl">
<div className="grid grid-cols-4 h-full">
<Sidebar />
<div className="
col-span-3
lg:col-span-2
border-x-[1px]
border-neutral-800
">
{ children }
</div>
<FollowBar />
</div>
</div>
</div>
);
}

export default Layout;
More...

请我喝杯咖啡吧~

支付宝
微信