()
| 38 | type FormData = z.infer<typeof formSchema> |
| 39 | |
| 40 | const AddItem = () => { |
| 41 | const [isOpen, setIsOpen] = useState(false) |
| 42 | const queryClient = useQueryClient() |
| 43 | const { showSuccessToast, showErrorToast } = useCustomToast() |
| 44 | |
| 45 | const form = useForm<FormData>({ |
| 46 | resolver: zodResolver(formSchema), |
| 47 | mode: "onBlur", |
| 48 | criteriaMode: "all", |
| 49 | defaultValues: { |
| 50 | title: "", |
| 51 | description: "", |
| 52 | }, |
| 53 | }) |
| 54 | |
| 55 | const mutation = useMutation({ |
| 56 | mutationFn: (data: ItemCreate) => |
| 57 | ItemsService.createItem({ requestBody: data }), |
| 58 | onSuccess: () => { |
| 59 | showSuccessToast("Item created successfully") |
| 60 | form.reset() |
| 61 | setIsOpen(false) |
| 62 | }, |
| 63 | onError: handleError.bind(showErrorToast), |
| 64 | onSettled: () => { |
| 65 | queryClient.invalidateQueries({ queryKey: ["items"] }) |
| 66 | }, |
| 67 | }) |
| 68 | |
| 69 | const onSubmit = (data: FormData) => { |
| 70 | mutation.mutate(data) |
| 71 | } |
| 72 | |
| 73 | return ( |
| 74 | <Dialog open={isOpen} onOpenChange={setIsOpen}> |
| 75 | <DialogTrigger asChild> |
| 76 | <Button className="my-4"> |
| 77 | <Plus className="mr-2" /> |
| 78 | Add Item |
| 79 | </Button> |
| 80 | </DialogTrigger> |
| 81 | <DialogContent className="sm:max-w-md"> |
| 82 | <DialogHeader> |
| 83 | <DialogTitle>Add Item</DialogTitle> |
| 84 | <DialogDescription> |
| 85 | Fill in the details to add a new item. |
| 86 | </DialogDescription> |
| 87 | </DialogHeader> |
| 88 | <Form {...form}> |
| 89 | <form onSubmit={form.handleSubmit(onSubmit)}> |
| 90 | <div className="grid gap-4 py-4"> |
| 91 | <FormField |
| 92 | control={form.control} |
| 93 | name="title" |
| 94 | render={({ field }) => ( |
| 95 | <FormItem> |
| 96 | <FormLabel> |
| 97 | Title <span className="text-destructive">*</span> |
nothing calls this directly
no test coverage detected