diff --git a/apps/backoffice/src/app/gacha-roll/_components/modal-add-item.tsx b/apps/backoffice/src/app/gacha-roll/_components/modal-add-item.tsx index b077c94..5da4435 100644 --- a/apps/backoffice/src/app/gacha-roll/_components/modal-add-item.tsx +++ b/apps/backoffice/src/app/gacha-roll/_components/modal-add-item.tsx @@ -1,6 +1,7 @@ import { Button } from '@imphnen-frontend-service/ui/atoms'; -import { InputField, Modal } from '@imphnen-frontend-service/ui/molecules'; -import { useState } from 'react'; +import { Modal } from '@imphnen-frontend-service/ui/molecules'; +import { ControlledInputField } from '@imphnen-frontend-service/ui/organisms'; +import { useAddItem, useConfirmAddItem } from '../_hook/use-add-item'; interface IModalAddItem { isOpen: boolean; @@ -49,9 +50,7 @@ interface IStepOneProps { } const StepOne = ({ nextStep }: IStepOneProps) => { - const [itemName, setItemName] = useState(''); - const [quantity, setQuantity] = useState(''); - const [chanceRate, setChanceRate] = useState(''); + const { form, onSubmit } = useAddItem(nextStep); return ( <> @@ -63,45 +62,42 @@ const StepOne = ({ nextStep }: IStepOneProps) => { Lengkapi detal di bawah ini, untuk menambahkan item gacha

- -
- setItemName(e.target.value)} - size="lg" - className="w-full" - /> - setQuantity(e.target.value)} - size="lg" - className="w-full" - /> - setChanceRate(e.target.value)} - size="lg" - className="w-full" - /> -
+ +
+
+ + + +
- + +
); @@ -113,43 +109,40 @@ interface IStepTwoProps { resetStep: () => void; } -const StepTwo = ({ onClose, handleAddItem, resetStep }: IStepTwoProps) => ( - <> - -

- Tambah ke Roll Gacha -

-

- Apakah kamu yakin ingin -
menambahkan item ini ke roll gacha? -

-
- - - - - -); +const StepTwo = ({ onClose, handleAddItem, resetStep }: IStepTwoProps) => { + const { onConfirm, onCancel } = useConfirmAddItem(onClose, resetStep); + + return ( + <> + +

+ Tambah ke Roll Gacha +

+

+ Apakah kamu yakin ingin +
menambahkan item ini ke roll gacha? +

+
+ + + + + + ); +}; export default ModalAddItem; diff --git a/apps/backoffice/src/app/gacha-roll/_hook/use-add-item.ts b/apps/backoffice/src/app/gacha-roll/_hook/use-add-item.ts new file mode 100644 index 0000000..2fc48d1 --- /dev/null +++ b/apps/backoffice/src/app/gacha-roll/_hook/use-add-item.ts @@ -0,0 +1,59 @@ +import { useForm } from 'react-hook-form'; +import { z } from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; + +const addItemSchema = z.object({ + itemName: z.string().min(1, 'Item name is required'), + quantity: z.string().min(1, 'Quantity is required'), + chanceRate: z + .string() + .min(1, 'Chance rate is required') + .refine( + (val) => { + const num = parseFloat(val); + return num >= 0.1 && num <= 1; + }, + { message: 'Chance rate must be between 0.1 and 1' } + ), +}); + +type TAddItemForm = z.infer; + +export const useAddItem = (nextStep: () => void) => { + const form = useForm({ + resolver: zodResolver(addItemSchema), + mode: 'all', + }); + + const onSubmit = form.handleSubmit((data) => { + console.log('Form data:', data); + nextStep(); + }); + + return { + form, + onSubmit, + }; +}; + +export const useConfirmAddItem = ( + onClose: () => void, + resetStep: () => void, + handleAddItem?: () => void, +) => { + const onConfirm = () => { + handleAddItem?.(); + onClose(); + resetStep(); + }; + + const onCancel = () => { + onClose(); + resetStep(); + }; + + return { + onConfirm, + onCancel, + }; +};