feat: refactor signin action and form to use validation schema
This commit is contained in:
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
import { setAccessToken, setRefreshToken } from '@/lib/cookies';
|
import { setAccessToken, setRefreshToken } from '@/lib/cookies';
|
||||||
import { fetcher } from '@/lib/openapi';
|
import { fetcher } from '@/lib/openapi';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { signInValidationSchema } from '../_validation/signin-validation';
|
||||||
|
|
||||||
|
export async function SigninAction(
|
||||||
|
data: z.infer<typeof signInValidationSchema>
|
||||||
|
) {
|
||||||
|
const { email, password } = signInValidationSchema.parse(data);
|
||||||
|
|
||||||
export async function SigninAction({
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
}: {
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
}) {
|
|
||||||
const { data: json, error } = await fetcher.POST('/v1/auth/login', {
|
const { data: json, error } = await fetcher.POST('/v1/auth/login', {
|
||||||
body: {
|
body: {
|
||||||
email,
|
email,
|
||||||
|
|||||||
@@ -11,21 +11,19 @@ import {
|
|||||||
Input,
|
Input,
|
||||||
} from '@components/atoms';
|
} from '@components/atoms';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { authLoginSchema } from '@schemas';
|
|
||||||
import { useMutation } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { LuLoader } from 'react-icons/lu';
|
import { LuLoader } from 'react-icons/lu';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { SigninAction } from '../_action/signin-action';
|
import { SigninAction } from '../_action/signin-action';
|
||||||
|
import { signInValidationSchema } from '../_validation/signin-validation';
|
||||||
type SigninData = z.infer<typeof authLoginSchema>;
|
|
||||||
|
|
||||||
export function SigninForm() {
|
export function SigninForm() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const form = useForm<SigninData>({
|
const form = useForm<z.infer<typeof signInValidationSchema>>({
|
||||||
resolver: zodResolver(authLoginSchema),
|
resolver: zodResolver(signInValidationSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
@@ -42,7 +40,7 @@ export function SigninForm() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = (values: SigninData) => {
|
const onSubmit = (values: z.infer<typeof signInValidationSchema>) => {
|
||||||
mutate(values);
|
mutate(values);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,7 +48,7 @@ export function SigninForm() {
|
|||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||||
{error && (
|
{error && (
|
||||||
<div className="mb-6 p-2 text-xs bg-red-50 border border-red-200 text-red-800 rounded-sm">
|
<div className="p-2 text-xs bg-red-50 border border-red-200 text-red-800 rounded-sm">
|
||||||
{(error as Error).message}
|
{(error as Error).message}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export const signInValidationSchema = z.object({
|
||||||
|
email: z
|
||||||
|
.string({
|
||||||
|
required_error: 'Email tidak boleh kosong',
|
||||||
|
invalid_type_error: 'Email harus berupa string',
|
||||||
|
})
|
||||||
|
.min(1, 'Email tidak boleh kosong')
|
||||||
|
.email('Email harus valid'),
|
||||||
|
password: z
|
||||||
|
.string({
|
||||||
|
required_error: 'Password tidak boleh kosong',
|
||||||
|
invalid_type_error: 'Password harus berupa string',
|
||||||
|
})
|
||||||
|
.min(1, 'Password tidak boleh kosong'),
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user