({ item, onSuccess }: EditItemProps)
| 43 | } |
| 44 | |
| 45 | const EditItem = ({ item, onSuccess }: EditItemProps) => { |
| 46 | const [isOpen, setIsOpen] = useState(false) |
| 47 | const queryClient = useQueryClient() |
| 48 | const { showSuccessToast, showErrorToast } = useCustomToast() |
| 49 | |
| 50 | const form = useForm<FormData>({ |
| 51 | resolver: zodResolver(formSchema), |
| 52 | mode: "onBlur", |
| 53 | criteriaMode: "all", |
| 54 | defaultValues: { |
| 55 | title: item.title, |
| 56 | description: item.description ?? undefined, |
| 57 | }, |
| 58 | }) |
| 59 | |
| 60 | const mutation = useMutation({ |
| 61 | mutationFn: (data: FormData) => |
| 62 | ItemsService.updateItem({ id: item.id, requestBody: data }), |
| 63 | onSuccess: () => { |
| 64 | showSuccessToast("Item updated successfully") |
| 65 | setIsOpen(false) |
| 66 | onSuccess() |
| 67 | }, |
| 68 | onError: handleError.bind(showErrorToast), |
| 69 | onSettled: () => { |
| 70 | queryClient.invalidateQueries({ queryKey: ["items"] }) |
| 71 | }, |
| 72 | }) |
| 73 | |
| 74 | const onSubmit = (data: FormData) => { |
| 75 | mutation.mutate(data) |
| 76 | } |
| 77 | |
| 78 | return ( |
| 79 | <Dialog open={isOpen} onOpenChange={setIsOpen}> |
| 80 | <DropdownMenuItem |
| 81 | onSelect={(e) => e.preventDefault()} |
| 82 | onClick={() => setIsOpen(true)} |
| 83 | > |
| 84 | <Pencil /> |
| 85 | Edit Item |
| 86 | </DropdownMenuItem> |
| 87 | <DialogContent className="sm:max-w-md"> |
| 88 | <Form {...form}> |
| 89 | <form onSubmit={form.handleSubmit(onSubmit)}> |
| 90 | <DialogHeader> |
| 91 | <DialogTitle>Edit Item</DialogTitle> |
| 92 | <DialogDescription> |
| 93 | Update the item details below. |
| 94 | </DialogDescription> |
| 95 | </DialogHeader> |
| 96 | <div className="grid gap-4 py-4"> |
| 97 | <FormField |
| 98 | control={form.control} |
| 99 | name="title" |
| 100 | render={({ field }) => ( |
| 101 | <FormItem> |
| 102 | <FormLabel> |
nothing calls this directly
no test coverage detected