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); console.log(found); const { control, reset, setValue, handleSubmit, formState: { errors }, } = useForm({ resolver: yupResolver(keyMeritsSchema), }); // useEffect to reset the form when `found` changes useEffect(() => { setValue("icon_xid", found?.icon?.id); setSelectedIcon(found?.icon?.iconName); // Update selected icon name setSelectedImageIcon(found?.icon?.iconFilePath); if (found) { reset({ meritsHeader: found?.meritsHeader, meritsDescription: found?.meritsDescription, meritsHeaderArabic: found?.meritsHeaderArabic, meritsDescriptionArabic: found?.meritsDescriptionArabic, icon_xid: found?.icon_xid, }); console.log("==============",found); } }, [found, reset]); const onSubmit = async (data) => { console.log(data); setIsLoading(true); try { const id = actionId; const res = await editKeyMerits({ data, id }); if (res?.data?.statusCode === 201) { toast({ render: () => , }); handleClose() reset({ meritsHeader: "", }); return; } if (res?.error?.data?.code === 400) { toast({ render: () => ( ), }); handleClose() reset(); return; } } catch (error) { if (error) { console.log(error); toast({ render: () => ( ), }); } handleClose() } reset(); }; const handleSave = () => { handleSubmit(onSubmit)(); }; const handleIconSelect = (id, iconName, iconFilePath) => { setValue("icon_xid", id); setSelectedIcon(iconName); // Update selected icon name setSelectedImageIcon(iconFilePath); }; const handleClose = () => { setIsLoading(false); setAlert(false); onClose(); reset(); } return ( <> Edit Key Merits Title (English) ( )} /> {errors.meritsHeader?.message} Title (Arabic) ( )} /> {errors.meritsHeaderArabic?.message} Description (English) (