generated from theodorusclarence/ts-nextjs-tailwind-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
login.tsx
91 lines (80 loc) · 2.61 KB
/
login.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import axios from 'axios';
import router from 'next/router';
import * as React from 'react';
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import useLoadingToast from '@/hooks/toast/useLoadingToast';
import Accent from '@/components/Accent';
import Button from '@/components/buttons/Button';
import Input from '@/components/forms/Input';
import Layout from '@/components/layout/Layout';
import Seo from '@/components/Seo';
import { DEFAULT_TOAST_MESSAGE } from '@/constant/toast';
type NewLinkFormData = {
slug: string;
link: string;
};
export default function NewLinkPage() {
const isLoading = useLoadingToast();
//#region //*=========== Form ===========
const methods = useForm<NewLinkFormData>({
mode: 'onTouched',
});
const { handleSubmit } = methods;
//#endregion //*======== Form ===========
//#region //*=========== Form Submit ===========
const onSubmit: SubmitHandler<NewLinkFormData> = (data) => {
toast.promise(
axios.post<{ token: string }>('/api/login', data).then((res) => {
localStorage.setItem('@notiolink/app_token', res.data.token);
router.replace(`/new`);
}),
{
...DEFAULT_TOAST_MESSAGE,
success: 'Logged in, you can now add new link',
}
);
};
//#endregion //*======== Form Submit ===========
return (
<Layout>
<Seo templateTitle='Login' />
<main>
<section>
<div className='layout flex min-h-screen flex-col items-center justify-center py-20'>
<h1 className='h0'>
<Accent>Login to the account</Accent>
</h1>
<FormProvider {...methods}>
<form
onSubmit={handleSubmit(onSubmit)}
className='mt-8 w-full max-w-sm'
>
<div className='space-y-4'>
<Input
id='password'
label='Password'
type='password'
validation={{
required: 'Password must be filled',
}}
/>
</div>
<div className='mt-5 flex flex-col'>
<Button
isLoading={isLoading}
className='w-full justify-center md:ml-auto md:w-auto'
variant='outline'
type='submit'
>
Login!
</Button>
</div>
</form>
</FormProvider>
</div>
</section>
</main>
</Layout>
);
}