2024-02-28 18:30:59 -05:00
|
|
|
import React from 'react';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-02-28 18:30:59 -05:00
|
|
|
import { Button, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
2024-02-12 16:46:51 -05:00
|
|
|
import { SubmitHandler, useForm } from 'react-hook-form';
|
2024-03-07 19:16:23 +01:00
|
|
|
import { useMutation, useQueryClient } from 'react-query';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
import { ApiError, ItemCreate, ItemsService } from '../../client';
|
2024-02-26 09:39:09 -05:00
|
|
|
import useCustomToast from '../../hooks/useCustomToast';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-02-15 17:17:26 -05:00
|
|
|
interface AddItemProps {
|
2024-02-12 16:46:51 -05:00
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
}
|
|
|
|
|
2024-02-15 17:17:26 -05:00
|
|
|
const AddItem: React.FC<AddItemProps> = ({ isOpen, onClose }) => {
|
2024-03-07 19:16:23 +01:00
|
|
|
const queryClient = useQueryClient();
|
2024-02-26 09:39:09 -05:00
|
|
|
const showToast = useCustomToast();
|
2024-02-28 18:30:59 -05:00
|
|
|
const { register, handleSubmit, reset, formState: { errors, isSubmitting } } = useForm<ItemCreate>({
|
|
|
|
mode: 'onBlur',
|
|
|
|
criteriaMode: 'all',
|
|
|
|
defaultValues: {
|
|
|
|
title: '',
|
|
|
|
description: '',
|
|
|
|
},
|
|
|
|
});
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
const addItem = async (data: ItemCreate) => {
|
|
|
|
await ItemsService.createItem({ requestBody: data })
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutation = useMutation(addItem, {
|
|
|
|
onSuccess: () => {
|
2024-02-26 09:39:09 -05:00
|
|
|
showToast('Success!', 'Item created successfully.', 'success');
|
2024-02-15 17:17:26 -05:00
|
|
|
reset();
|
2024-02-12 16:46:51 -05:00
|
|
|
onClose();
|
2024-03-07 19:16:23 +01:00
|
|
|
},
|
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = err.body.detail;
|
2024-02-26 09:39:09 -05:00
|
|
|
showToast('Something went wrong.', `${errDetail}`, 'error');
|
2024-03-07 19:16:23 +01:00
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries('items');
|
2024-02-12 16:46:51 -05:00
|
|
|
}
|
2024-03-07 19:16:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const onSubmit: SubmitHandler<ItemCreate> = (data) => {
|
|
|
|
mutation.mutate(data);
|
|
|
|
}
|
2024-02-12 16:46:51 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Modal
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
size={{ base: 'sm', md: 'md' }}
|
|
|
|
isCentered
|
|
|
|
>
|
|
|
|
<ModalOverlay />
|
2024-02-26 09:39:09 -05:00
|
|
|
<ModalContent as='form' onSubmit={handleSubmit(onSubmit)}>
|
2024-02-15 17:17:26 -05:00
|
|
|
<ModalHeader>Add Item</ModalHeader>
|
2024-02-12 16:46:51 -05:00
|
|
|
<ModalCloseButton />
|
|
|
|
<ModalBody pb={6}>
|
2024-02-28 18:30:59 -05:00
|
|
|
<FormControl isRequired isInvalid={!!errors.title}>
|
2024-02-26 09:39:09 -05:00
|
|
|
<FormLabel htmlFor='title'>Title</FormLabel>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Input
|
2024-02-26 09:39:09 -05:00
|
|
|
id='title'
|
2024-02-28 18:30:59 -05:00
|
|
|
{...register('title', { required: 'Title is required.' })}
|
2024-02-26 09:39:09 -05:00
|
|
|
placeholder='Title'
|
|
|
|
type='text'
|
2024-02-12 16:46:51 -05:00
|
|
|
/>
|
2024-02-28 18:30:59 -05:00
|
|
|
{errors.title && <FormErrorMessage>{errors.title.message}</FormErrorMessage>}
|
2024-02-12 16:46:51 -05:00
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4}>
|
2024-02-26 09:39:09 -05:00
|
|
|
<FormLabel htmlFor='description'>Description</FormLabel>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Input
|
2024-02-26 09:39:09 -05:00
|
|
|
id='description'
|
2024-02-12 16:46:51 -05:00
|
|
|
{...register('description')}
|
2024-02-26 09:39:09 -05:00
|
|
|
placeholder='Description'
|
|
|
|
type='text'
|
2024-02-12 16:46:51 -05:00
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
</ModalBody>
|
|
|
|
|
|
|
|
<ModalFooter gap={3}>
|
2024-02-28 18:30:59 -05:00
|
|
|
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting}>
|
2024-02-12 16:46:51 -05:00
|
|
|
Save
|
|
|
|
</Button>
|
2024-02-28 18:30:59 -05:00
|
|
|
<Button onClick={onClose}>
|
2024-02-12 16:46:51 -05:00
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</ModalFooter>
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-02-15 17:17:26 -05:00
|
|
|
export default AddItem;
|