import {
Box,
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
FormControl,
FormErrorMessage,
FormLabel,
HStack,
Image,
Input,
Menu,
MenuButton,
MenuItem,
MenuList,
Stack,
Text,
Textarea,
useToast,
} from "@chakra-ui/react";
import React, { useEffect, useRef, useState } from "react";
import { useForm, Controller } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import CustomAlertDialog from "../../Components/CustomAlertDialog";
import * as yup from "yup";
import ToastBox from "../../Components/ToastBox";
import { useUpdateKeyMeritsMutation } from "../../Services/io.service";
import { ChevronDownIcon } from "@chakra-ui/icons";
const keyMeritsSchema = yup.object().shape({
meritsHeader: yup.string().required("Title is required"),
meritsDescription: yup.string().required("Sub title is required"),
meritsHeaderArabic: yup.string().required("Arabic title is required"),
meritsDescriptionArabic: yup
.string()
.required("Arabic Description is required"),
icon_xid: yup.mixed().required("Icon is required"), // Adjust based on file or string
});
const KeyMeritsEdit = ({ isOpen, onClose, firstField, id, actionId, data, icons }) => {
const toast = useToast();
const [alert, setAlert] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [editKeyMerits] = useUpdateKeyMeritsMutation();
const [selectedIcon, setSelectedIcon] = useState("Select Icon");
const [selectedImageIcon, setSelectedImageIcon] = useState(null);
const found = data?.find((item) => item?.id === actionId);
const {
control,
reset,setValue,
handleSubmit,
formState: { errors },
} = useForm({
resolver: yupResolver(keyMeritsSchema),
});
// useEffect to reset the form when `found` changes
useEffect(() => {
if (found) {
reset({
meritsHeader: found?.meritsHeader,
meritsDescription: found?.meritsDescription,
meritsHeaderArabic:found?.meritsHeaderArabic,
meritsDescriptionArabic:found?.meritsDescriptionArabic,
iconImage: null,
});
}
}, [found, reset]);
const onSubmit = async (data) => {
setIsLoading(true);
try {
const id = actionId;
const res = await editKeyMerits({ data, 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) {
console.log(error);
toast({
render: () => (
),
});
}
setIsLoading(false);
setAlert(false);
onClose();
reset();
}
reset();
};
const handleSave = () => {
handleSubmit(onSubmit)();
};
console.log(errors);
const handleIconSelect = (id, iconName, iconFilePath) => {
setValue("icon_xid", id);
setSelectedIcon(iconName); // Update selected icon name
setSelectedImageIcon(iconFilePath);
};
return (
<>
Edit Key Merits
Title (English)
(
)}
/>
{errors.meritsHeader?.message}
Title (Arabic)
(
)}
/>
{errors.meritsHeaderArabic?.message}
Description (English)
(
)}
/>
{errors.meritsDescription?.message}
Description (Arabic)
(
)}
/>
{errors.meritsDescriptionArabic?.message}
Select Icon
(
)}
/>
{!errors.icon_xid?.message || !selectedImageIcon && errors.icon_xid?.message}
{errors.icon_xid?.message && errors.icon_xid?.message}
setAlert(false)}
alertHandler={handleSave}
message={"Are you sure you want to add this key merit?"}
isLoading={isLoading}
/>
>
);
};
export default KeyMeritsEdit;