Files
imphnen-frontend-service/apps/backoffice/src/app/(protected)/gacha-roll/_hook/use-item.ts
T

62 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-04-05 06:41:11 +07:00
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import {
gachaRollItemSchema,
TGachaRollItem
} from '@imphnen-frontend-service/service';
2025-04-05 08:40:19 +07:00
import { toast } from 'sonner';
2025-04-05 06:41:11 +07:00
2025-04-05 08:02:13 +07:00
export const useItem = (
nextStep: () => void,
initialValues?: TGachaRollItem
) => {
const form = useForm<TGachaRollItem>({
resolver: zodResolver(gachaRollItemSchema),
2025-04-05 06:41:11 +07:00
mode: 'all',
2025-04-05 08:02:13 +07:00
defaultValues: initialValues,
2025-04-05 06:41:11 +07:00
});
const onSubmit = form.handleSubmit((data) => {
console.log('Form data:', data);
nextStep();
});
return {
form,
onSubmit,
};
};
2025-04-05 08:02:13 +07:00
export const useConfirmItem = (
2025-04-05 06:41:11 +07:00
onClose: () => void,
resetStep: () => void,
2025-04-05 08:40:19 +07:00
actionFunction?: () => Promise<boolean>,
messages?: {
success?: string;
error?: string;
}
2025-04-05 06:41:11 +07:00
) => {
2025-04-05 08:40:19 +07:00
const onConfirm = async () => {
try {
// const result = await actionFunction?.();
// result ? toast.success(messages?.success) : toast.error(messages?.error);
toast.success(messages?.success);
onClose();
resetStep();
} catch (error) {
console.log(error);
toast.error(messages?.error);
}
2025-04-05 06:41:11 +07:00
};
const onCancel = () => {
onClose();
resetStep();
};
return {
onConfirm,
onCancel,
};
};