Files
fast_api_template/frontend/src/components/Items/EditItem.tsx
T

125 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Button,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
2024-04-04 16:30:42 +02:00
import { useMutation, useQueryClient } from "@tanstack/react-query"
2024-04-08 15:49:22 -05:00
import { type SubmitHandler, useForm } from "react-hook-form"
2024-03-17 17:28:45 +01:00
import {
type ApiError,
2024-04-06 18:26:12 -05:00
type ItemPublic,
2024-03-17 17:28:45 +01:00
type ItemUpdate,
2024-03-28 21:15:11 -05:00
ItemsService,
2024-03-17 17:28:45 +01:00
} from "../../client"
import useCustomToast from "../../hooks/useCustomToast"
interface EditItemProps {
2024-04-06 18:26:12 -05:00
item: ItemPublic
2024-03-08 14:58:36 +01:00
isOpen: boolean
onClose: () => void
}
2024-03-28 20:22:28 -05:00
const EditItem = ({ item, isOpen, onClose }: EditItemProps) => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const showToast = useCustomToast()
const {
register,
handleSubmit,
reset,
formState: { isSubmitting, errors, isDirty },
} = useForm<ItemUpdate>({
2024-03-17 17:28:45 +01:00
mode: "onBlur",
criteriaMode: "all",
2024-03-08 14:58:36 +01:00
defaultValues: item,
})
2024-04-04 16:30:42 +02:00
const mutation = useMutation({
mutationFn: (data: ItemUpdate) =>
ItemsService.updateItem({ id: item.id, requestBody: data }),
2024-04-04 16:30:42 +02:00
onSuccess: () => {
showToast("Success!", "Item updated successfully.", "success")
onClose()
2024-03-08 14:58:36 +01:00
},
2024-04-04 16:30:42 +02:00
onError: (err: ApiError) => {
const errDetail = (err.body as any)?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["items"] })
},
})
2024-03-08 14:58:36 +01:00
const onSubmit: SubmitHandler<ItemUpdate> = async (data) => {
mutation.mutate(data)
}
2024-03-08 14:58:36 +01:00
const onCancel = () => {
reset()
onClose()
}
2024-03-08 14:58:36 +01:00
return (
<>
<Modal
isOpen={isOpen}
onClose={onClose}
2024-03-17 17:28:45 +01:00
size={{ base: "sm", md: "md" }}
2024-03-08 14:58:36 +01:00
isCentered
>
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Edit Item</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isInvalid={!!errors.title}>
<FormLabel htmlFor="title">Title</FormLabel>
<Input
id="title"
2024-03-17 17:28:45 +01:00
{...register("title", {
required: "Title is required",
2024-03-08 14:58:36 +01:00
})}
type="text"
/>
{errors.title && (
<FormErrorMessage>{errors.title.message}</FormErrorMessage>
)}
</FormControl>
<FormControl mt={4}>
<FormLabel htmlFor="description">Description</FormLabel>
<Input
id="description"
2024-03-17 17:28:45 +01:00
{...register("description")}
2024-03-08 14:58:36 +01:00
placeholder="Description"
type="text"
/>
</FormControl>
</ModalBody>
<ModalFooter gap={3}>
<Button
2024-03-11 16:50:46 +01:00
variant="primary"
2024-03-08 14:58:36 +01:00
type="submit"
isLoading={isSubmitting}
isDisabled={!isDirty}
>
2024-03-08 14:58:36 +01:00
Save
</Button>
<Button onClick={onCancel}>Cancel</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
)
}
2024-03-08 14:58:36 +01:00
export default EditItem