Files
tanami-admin-panel/src/Pages/IO_Management/KeyMeritsAdd.jsx

205 lines
6.0 KiB
React
Raw Normal View History

2024-07-08 12:23:26 +05:30
import {
2024-07-22 14:50:31 +05:30
Box,
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Stack,
useToast,
} from "@chakra-ui/react";
import React, { useRef, useState } from "react";
import { useForm, Controller } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import CustomAlertDialog from "../../Components/CustomAlertDialog";
import { useCreateKeyMeritsMutation } from "../../Services/io.service";
import * as yup from "yup";
import ToastBox from "../../Components/ToastBox";
const keyMeritsSchema = yup.object().shape({
meritsHeader: yup.string().required("Title is required"),
meritsDescription: yup.string().required("Sub title is required"),
iconImage: yup.mixed().required("Icon is required"), // Adjust based on file or string
});
const KeyMeritsAdd = ({ isOpen, onClose, firstField, id }) => {
const toast = useToast()
const [alert, setAlert] = useState(false);
const [createKeyMerits] = useCreateKeyMeritsMutation();
const [isLoading, setIsLoading] = useState(false);
const {
control,
reset,
handleSubmit,
formState: { errors },
} = useForm({
resolver: yupResolver(keyMeritsSchema),
defaultValues: {
meritsHeader: "",
meritsDescription: "",
iconImage: null,
},
2024-07-08 12:23:26 +05:30
});
2024-07-22 14:50:31 +05:30
const onSubmit = async (data) => {
setIsLoading(true);
try {
const formData = new FormData();
formData.append("meritsHeader", data.meritsHeader);
formData.append("meritsDescription", data.meritsDescription);
if (data.iconImage && data.iconImage instanceof File) {
formData.append("iconImage", data.iconImage);
}
const res = await createKeyMerits({ data: formData, id });
if (res?.data?.statusCode === 201) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
setAlert(false);
onClose();
setIsLoading(false);
reset()
return
}
if(res?.error?.data?.code === 400){
toast({
render: () => <ToastBox message={res?.error?.data?.message} status={'error'} />,
});
setAlert(false);
onClose();
setIsLoading(false);
reset()
return
}
} catch (error) {
if (error) {
toast({
render: () => <ToastBox message={"Something went wrong, please try again!"} status={'error'} />,
});
}
setIsLoading(false);
setAlert(false);
onClose();
reset()
2024-07-08 12:23:26 +05:30
}
2024-07-22 14:50:31 +05:30
reset()
};
const handleSave = () => {
handleSubmit(onSubmit)();
};
return (
<>
<Drawer
isOpen={isOpen}
placement="right"
initialFocusRef={firstField}
onClose={onClose}
>
<DrawerOverlay />
<DrawerContent>
<DrawerCloseButton />
<DrawerHeader fontSize={"sm"}>Key Merits</DrawerHeader>
<DrawerBody>
<Stack spacing={2}>
<FormControl isInvalid={!!errors.meritsHeader} isRequired={true}>
2024-07-08 12:23:26 +05:30
<FormLabel fontSize={"sm"}>Title</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="meritsHeader"
control={control}
render={({ field }) => (
<Input {...field} fontSize={"sm"} type="text" size={"sm"} />
)}
2024-07-08 12:23:26 +05:30
/>
2024-07-22 14:50:31 +05:30
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsHeader?.message}
</FormErrorMessage>
2024-07-08 12:23:26 +05:30
</FormControl>
2024-07-22 14:50:31 +05:30
<FormControl isInvalid={!!errors.meritsDescription} isRequired={true}>
2024-07-08 12:23:26 +05:30
<FormLabel fontSize={"sm"}>Sub Title</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="meritsDescription"
control={control}
render={({ field }) => (
<Input
{...field}
fontSize={"sm"}
type="textarea"
size={"sm"}
/>
)}
2024-07-08 12:23:26 +05:30
/>
2024-07-22 14:50:31 +05:30
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsDescription?.message}
</FormErrorMessage>
2024-07-08 12:23:26 +05:30
</FormControl>
2024-07-22 14:50:31 +05:30
<FormControl isInvalid={!!errors.iconImage} isRequired={true}>
2024-07-08 12:23:26 +05:30
<FormLabel fontSize={"sm"}>Icon</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="iconImage"
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<Input
type="file"
className="form-control"
onChange={(e) => {
onChange(e.target.files[0]);
}}
onBlur={onBlur}
fontSize={"sm"}
size={"sm"}
/>
)}
2024-07-08 12:23:26 +05:30
/>
2024-07-22 14:50:31 +05:30
<FormErrorMessage fontSize={"xs"} fontWeight={500} >
{errors.iconImage?.message}
</FormErrorMessage>
2024-07-08 12:23:26 +05:30
</FormControl>
2024-07-22 14:50:31 +05:30
</Stack>
</DrawerBody>
<DrawerFooter>
<Button
variant="outline"
colorScheme={"green"}
rounded={"sm"}
size={"sm"}
mr={3}
onClick={onClose}
>
Cancel
</Button>
<Button
colorScheme={"green"}
rounded={"sm"}
size={"sm"}
onClick={() => setAlert(true)}
>
Save
</Button>
</DrawerFooter>
</DrawerContent>
</Drawer>
<CustomAlertDialog
isOpen={alert}
onClose={() => setAlert(false)}
alertHandler={handleSave}
message={"Are you sure you want to add this key merit?"}
isLoading={isLoading}
/>
</>
);
};
export default KeyMeritsAdd;