import { 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, }, }); 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: () => , }); setAlert(false); onClose(); setIsLoading(false); reset() return } if(res?.error?.data?.code === 400){ toast({ render: () => , }); setAlert(false); onClose(); setIsLoading(false); reset() return } } catch (error) { if (error) { toast({ render: () => , }); } setIsLoading(false); setAlert(false); onClose(); reset() } reset() }; const handleSave = () => { handleSubmit(onSubmit)(); }; return ( <> Key Merits Title ( )} /> {errors.meritsHeader?.message} Sub Title ( )} /> {errors.meritsDescription?.message} Icon ( { onChange(e.target.files[0]); }} onBlur={onBlur} fontSize={"sm"} size={"sm"} /> )} /> {errors.iconImage?.message} setAlert(false)} alertHandler={handleSave} message={"Are you sure you want to add this key merit?"} isLoading={isLoading} /> ); }; export default KeyMeritsAdd;