-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from DuckyMomo20012/dev
fix: update UI & configs
- Loading branch information
Showing
23 changed files
with
606 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { | ||
Button, | ||
Group, | ||
PasswordInput, | ||
Space, | ||
Stack, | ||
TextInput, | ||
} from '@mantine/core'; | ||
import { useRouter } from 'next/navigation'; | ||
import { signIn } from 'next-auth/react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
export const signInSchema = z.object({ | ||
email: z.string().email(), | ||
password: z.string().min(8), | ||
}); | ||
|
||
export type TSignInForm = z.infer<typeof signInSchema>; | ||
|
||
const SignInForm = () => { | ||
const router = useRouter(); | ||
|
||
const { | ||
setError, | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<TSignInForm>({ | ||
defaultValues: { | ||
email: '', | ||
password: '', | ||
}, | ||
resolver: zodResolver(signInSchema), | ||
}); | ||
|
||
const onSubmit = async (formData: TSignInForm) => { | ||
const res = await signIn('credentials', { | ||
email: formData.email, | ||
password: formData.password, | ||
redirect: false, | ||
}); | ||
|
||
if (res?.status === 401) { | ||
setError('email', { | ||
type: 'manual', | ||
message: 'Invalid credentials', | ||
}); | ||
|
||
setError('password', { | ||
type: 'manual', | ||
message: 'Invalid credentials', | ||
}); | ||
} else if (res?.status === 200) { | ||
router.push('/'); | ||
} | ||
}; | ||
|
||
return ( | ||
<form className="h-full w-full" onSubmit={handleSubmit(onSubmit)}> | ||
<Stack> | ||
<TextInput | ||
error={errors.email?.message} | ||
label="Email" | ||
withAsterisk | ||
{...register('email')} | ||
/> | ||
<PasswordInput | ||
error={errors.password?.message} | ||
label="Password" | ||
withAsterisk | ||
{...register('password')} | ||
/> | ||
|
||
<Space h="md" /> | ||
|
||
<Group className="w-full" justify="center"> | ||
<Button type="submit">Sign in</Button> | ||
</Group> | ||
</Stack> | ||
</form> | ||
); | ||
}; | ||
|
||
export { SignInForm }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { | ||
Button, | ||
Group, | ||
PasswordInput, | ||
Space, | ||
Stack, | ||
TextInput, | ||
} from '@mantine/core'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import axios, { AxiosError } from 'axios'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
export const signUpSchema = z | ||
.object({ | ||
email: z.string().email(), | ||
name: z.string().nullish(), | ||
password: z.string().min(8), | ||
confirmPassword: z.string(), | ||
}) | ||
.refine((data) => data.password === data.confirmPassword, { | ||
message: "Passwords don't match", | ||
path: ['confirmPassword'], | ||
}); | ||
|
||
export type TSignUpForm = z.infer<typeof signUpSchema>; | ||
|
||
const SignUpForm = () => { | ||
const router = useRouter(); | ||
|
||
const { | ||
setError, | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<TSignUpForm>({ | ||
defaultValues: { | ||
name: '', | ||
email: '', | ||
password: '', | ||
confirmPassword: '', | ||
}, | ||
resolver: zodResolver(signUpSchema), | ||
}); | ||
|
||
const { mutate: signUp } = useMutation({ | ||
mutationKey: ['users', 'signUp'], | ||
mutationFn: async (formData: TSignUpForm) => { | ||
const { data } = await axios.post('/api/users', { | ||
name: formData.name, | ||
email: formData.email, | ||
password: formData.password, | ||
}); | ||
|
||
return data; | ||
}, | ||
onSuccess: () => { | ||
router.push('/auth/sign-in'); | ||
}, | ||
onError: (error) => { | ||
if (error instanceof AxiosError) { | ||
if (error.response?.status === 409) { | ||
setError('email', { | ||
message: 'Email already exists', | ||
}); | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
const onSubmit = (formData: TSignUpForm) => { | ||
signUp(formData); | ||
}; | ||
|
||
return ( | ||
<form className="h-full w-full" onSubmit={handleSubmit(onSubmit)}> | ||
<Stack> | ||
<TextInput | ||
error={errors.email?.message} | ||
label="Email" | ||
withAsterisk | ||
{...register('email')} | ||
/> | ||
<TextInput | ||
error={errors.name?.message} | ||
label="Name" | ||
{...register('name')} | ||
/> | ||
<PasswordInput | ||
error={errors.password?.message} | ||
label="Password" | ||
withAsterisk | ||
{...register('password')} | ||
/> | ||
<PasswordInput | ||
error={errors.confirmPassword?.message} | ||
label="Confirm password" | ||
withAsterisk | ||
{...register('confirmPassword')} | ||
/> | ||
|
||
<Space h="md" /> | ||
|
||
<Group className="w-full" justify="center"> | ||
<Button type="submit">Sign up</Button> | ||
</Group> | ||
</Stack> | ||
</form> | ||
); | ||
}; | ||
|
||
export { SignUpForm }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Icon } from '@iconify/react/dist/iconify.js'; | ||
import { | ||
ActionIcon, | ||
Anchor, | ||
Group, | ||
AppShell as MantineAppShell, | ||
Text, | ||
useMantineColorScheme, | ||
} from '@mantine/core'; | ||
|
||
const AppShell = ({ children }: { children?: React.ReactNode }) => { | ||
const { colorScheme } = useMantineColorScheme(); | ||
const dark = colorScheme === 'dark'; | ||
|
||
return ( | ||
<MantineAppShell footer={{ height: 20 }} header={{ height: 60 }}> | ||
<MantineAppShell.Header> | ||
<Group className="h-full w-full items-center px-4" justify="flex-end"> | ||
<Anchor | ||
data-test-id="github-link" | ||
href="https://github.com/DuckyMomo20012/nextjs-template" | ||
target="_blank" | ||
> | ||
<ActionIcon | ||
aria-label="GitHub link" | ||
role="link" | ||
size="lg" | ||
variant="outline" | ||
> | ||
<Icon icon="ant-design:github-filled" width={24} /> | ||
</ActionIcon> | ||
</Anchor> | ||
</Group> | ||
</MantineAppShell.Header> | ||
|
||
<MantineAppShell.Main className="flex flex-col"> | ||
{children} | ||
</MantineAppShell.Main> | ||
|
||
<MantineAppShell.Footer> | ||
<Group className="h-full w-full items-center" justify="center"> | ||
<Text className="w-full text-center" size="sm"> | ||
Made with{' '} | ||
<Icon | ||
icon={`fluent-emoji-flat:${ | ||
dark ? 'teacup-without-handle' : 'sparkling-heart' | ||
}`} | ||
inline={true} | ||
/>{' '} | ||
by{' '} | ||
<Anchor href="https://github.com/DuckyMomo20012">Tien Vinh</Anchor> | ||
</Text> | ||
</Group> | ||
</MantineAppShell.Footer> | ||
</MantineAppShell> | ||
); | ||
}; | ||
|
||
export { AppShell }; |
2 changes: 1 addition & 1 deletion
2
src/components/ButtonDemo.tsx → src/components/modules/ButtonDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
84cf207
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
my-nextjs-template – ./
my-nextjs-template-one.vercel.app
my-nextjs-template-git-main-duckymomo20012.vercel.app
my-nextjs-template-duckymomo20012.vercel.app