Files
imphnen-frontend-service/apps/gacha/src/app/_hooks/use-register.ts
T

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-04-03 23:32:44 +07:00
import { useForm } from 'react-hook-form';
import {
authRegisterSchema,
2025-04-04 16:38:21 +07:00
stepOneRegisterSchema,
2025-04-03 23:32:44 +07:00
TRegisterRequest,
usePostRegister,
} from '@imphnen-frontend-service/service';
2025-04-04 16:38:21 +07:00
import { z } from 'zod';
2025-04-03 23:32:44 +07:00
import { zodResolver } from '@hookform/resolvers/zod';
import { toast } from 'sonner';
2025-04-04 16:38:21 +07:00
import { useEffect, useState } from 'react';
2025-04-03 23:32:44 +07:00
export const useRegister = () => {
const postRegister = usePostRegister();
const form = useForm<TRegisterRequest>({
resolver: zodResolver(authRegisterSchema),
mode: 'all',
2025-04-04 16:38:21 +07:00
defaultValues: {
fullname: '',
email: '',
password: '',
confirm_password: '',
phone_number: '',
referral_code: '',
referred_by: '',
student_type: '',
},
2025-04-03 23:32:44 +07:00
});
2025-04-04 16:38:21 +07:00
const [stepValid, setStepValid] = useState(false);
useEffect(() => {
const subscription = form.watch(() => {
const { fullname, email, password, confirm_password } = form.getValues();
const valid = stepOneRegisterSchema.safeParse({
fullname,
email,
password,
confirm_password,
}).success;
setStepValid(valid);
});
return () => subscription.unsubscribe();
}, [form]);
2025-04-03 23:32:44 +07:00
const onSubmit = form.handleSubmit((data) => {
postRegister.mutate(data, {
onSuccess: (data) => toast.success(data.message),
onError: (error) => toast.error(error.message),
});
});
return {
form,
onSubmit,
2025-04-04 16:38:21 +07:00
isStepOneValid: stepValid,
2025-04-03 23:32:44 +07:00
};
};