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, { 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";
import { ChevronDownIcon } from "@chakra-ui/icons"; // Import ChevronDownIcon for the dropdown button
const keyMeritsSchema = yup.object().shape({
meritsHeader: yup.string().required("Title is required"),
meritsDescription: yup.string().required("Description is required"),
meritsHeaderArabic: yup.string().required("Arabic title is required"),
meritsDescriptionArabic: yup
.string()
.required("Arabic Description is required"),
icon_xid: yup.string().required("Icon is required"), // Adjust based on file or string
});
const KeyMeritsAdd = ({ isOpen, onClose, firstField, id, icons }) => {
const toast = useToast();
const [formData, setFormData] = useState(null);
const [alert, setAlert] = useState(false);
const [createKeyMerits] = useCreateKeyMeritsMutation();
const [isLoading, setIsLoading] = useState(false);
const [file, setFile] = useState(null);
const [selectedIcon, setSelectedIcon] = useState("Select Icon");
const [selectedImageIcon, setSelectedImageIcon] = useState(null);
const {
control,
reset,
handleSubmit,
setValue, // Add setValue to programmatically set the selected icon
formState: { errors },
} = useForm({
resolver: yupResolver(keyMeritsSchema),
defaultValues: {
meritsHeader: "",
meritsDescription: "",
meritsHeaderArabic: "",
meritsDescriptionArabic: "",
icon_xid: "",
},
});
const onSubmit = (data) => {
if (Object.keys(errors).length === 0) {
setFormData(data);
setAlert(true);
}
};
const handleConfirm = async () => {
setIsLoading(true);
try {
const res = await createKeyMerits({ data: formData, id });
console.log(res?.error?.status);
if (res?.data?.statusCode === 201) {
toast({
render: () => ,
});
handleClose()
return;
}
if (res?.error?.status === 400 || res?.error?.status === 500 ) {
toast({
render: () => (
),
});
setIsLoading(false);
setAlert(false);
return;
}
} catch (error) {
if (error) {
toast({
render: () => (
),
});
}
setIsLoading(false);
handleClose()
}
reset();
};
const handleSave = () => {
handleSubmit(onSubmit)();
};
const handleFileChange = (e) => {
const file = e.target.files[0];
if (file) {
setFile(URL.createObjectURL(file));
}
};
const handleIconSelect = (id, iconName, iconFilePath) => {
setValue("icon_xid", id);
setSelectedIcon(iconName); // Update selected icon name
setSelectedImageIcon(iconFilePath);
};
const handleClose = () => {
onClose();
setIsLoading(false);
setAlert(false);
reset();
setFile(null);
setSelectedImageIcon(null);
setSelectedIcon("Select Icon");
};
return (
<>
Key Merits
Title (English)
(
)}
/>
{errors.meritsHeader?.message}
Title (Arabic)
(
)}
/>
{errors.meritsHeaderArabic?.message}
Sub title
(
)}
/>
{errors.meritsDescription?.message}
Sub title (Arabic)
(
)}
/>
{errors.meritsDescriptionArabic?.message}
Select Icon
(
)}
/>
{!errors.icon_xid?.message ||
(!selectedImageIcon && errors.icon_xid?.message)}
>
);
};
export default KeyMeritsAdd;