refactor: fix validation for add gacha roll item

- Pisahkan type dan schema untuk gacha roll item
- Perbaiki validasi gacha roll item
- Parsing angka pada component ControlledInputField
- Update type number untuk component Input dan InputField
This commit is contained in:
Hafid Nur
2025-04-05 07:39:57 +07:00
parent 885d156833
commit 3958ae1098
8 changed files with 58 additions and 27 deletions
@@ -78,7 +78,8 @@ const StepOne = ({ nextStep }: IStepOneProps) => {
control={form.control}
label="Quantity"
name="quantity"
type="text"
type="number"
min={1}
placeholder="Masukkan Kuantitas Item"
size="lg"
className="w-full"
@@ -87,8 +88,12 @@ const StepOne = ({ nextStep }: IStepOneProps) => {
control={form.control}
label="Chance Rate"
name="chanceRate"
type="text"
placeholder="Masukkan Chance Rate (0.1 - 1)"
type="number"
value={0.1}
min={0.1}
step={0.1}
max={1}
placeholder="Masukkan Chance Rate (0,1 - 1)"
size="lg"
className="w-full"
/>
@@ -1,27 +1,13 @@
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<typeof addItemSchema>;
import {
gachaRollItemSchema,
TGachaRollItem
} from '@imphnen-frontend-service/service';
export const useAddItem = (nextStep: () => void) => {
const form = useForm<TAddItemForm>({
resolver: zodResolver(addItemSchema),
const form = useForm<TGachaRollItem>({
resolver: zodResolver(gachaRollItemSchema),
mode: 'all',
});