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

378 lines
11 KiB
React
Raw Normal View History

2024-07-22 14:50:31 +05:30
import {
Box,
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
FormControl,
FormErrorMessage,
FormLabel,
HStack,
Image,
2024-07-22 14:50:31 +05:30
Input,
Menu,
MenuButton,
MenuItem,
MenuList,
2024-07-22 14:50:31 +05:30
Stack,
Text,
Textarea,
2024-07-22 14:50:31 +05:30
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";
2024-07-22 14:50:31 +05:30
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"),
2024-08-02 16:56:53 +05:30
icon_xid: yup.mixed().required("Icon is required"), // Adjust based on file or string
2024-07-22 14:50:31 +05:30
});
2024-08-02 16:56:53 +05:30
const KeyMeritsEdit = ({
isOpen,
onClose,
firstField,
id,
actionId,
data,
icons,
}) => {
const toast = useToast();
2024-07-22 14:50:31 +05:30
const [alert, setAlert] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [editKeyMerits] = useUpdateKeyMeritsMutation();
const [selectedIcon, setSelectedIcon] = useState("Select Icon");
const [selectedImageIcon, setSelectedImageIcon] = useState(null);
2024-07-22 14:50:31 +05:30
const found = data?.find((item) => item?.id === actionId);
2024-08-14 12:19:27 +05:30
console.log(found);
2024-07-22 14:50:31 +05:30
const {
control,
2024-08-02 16:56:53 +05:30
reset,
setValue,
2024-07-22 14:50:31 +05:30
handleSubmit,
formState: { errors },
} = useForm({
resolver: yupResolver(keyMeritsSchema),
});
// useEffect to reset the form when `found` changes
useEffect(() => {
2024-08-14 12:19:27 +05:30
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,
2024-08-02 16:56:53 +05:30
meritsHeaderArabic: found?.meritsHeaderArabic,
meritsDescriptionArabic: found?.meritsDescriptionArabic,
2024-11-11 12:48:06 +05:30
icon_xid: found?.icon_xid,
});
2024-11-11 12:48:06 +05:30
console.log("==============",found);
}
}, [found, reset]);
2024-07-22 14:50:31 +05:30
const onSubmit = async (data) => {
2024-11-11 12:48:06 +05:30
console.log(data);
2024-07-22 14:50:31 +05:30
setIsLoading(true);
try {
const id = actionId;
const res = await editKeyMerits({ data, id });
2024-07-22 14:50:31 +05:30
if (res?.data?.statusCode === 201) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
2024-08-14 12:19:27 +05:30
handleClose()
2024-11-11 12:48:06 +05:30
reset({
meritsHeader: "",
});
return;
}
if (res?.error?.data?.code === 400) {
2024-07-22 14:50:31 +05:30
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status={"error"} />
),
2024-07-22 14:50:31 +05:30
});
2024-08-14 12:19:27 +05:30
handleClose()
2024-11-11 12:48:06 +05:30
reset();
return;
2024-07-22 14:50:31 +05:30
}
} catch (error) {
if (error) {
console.log(error);
2024-07-22 14:50:31 +05:30
toast({
render: () => (
<ToastBox
message={"Something went wrong, please try again!"}
status={"error"}
/>
),
2024-07-22 14:50:31 +05:30
});
}
2024-08-14 12:19:27 +05:30
handleClose()
2024-07-22 14:50:31 +05:30
}
reset();
2024-07-22 14:50:31 +05:30
};
const handleSave = () => {
handleSubmit(onSubmit)();
};
const handleIconSelect = (id, iconName, iconFilePath) => {
setValue("icon_xid", id);
setSelectedIcon(iconName); // Update selected icon name
setSelectedImageIcon(iconFilePath);
};
2024-08-14 12:19:27 +05:30
const handleClose = () => {
setIsLoading(false);
setAlert(false);
onClose();
reset();
}
2024-07-22 14:50:31 +05:30
return (
<>
<Drawer
2024-08-02 16:56:53 +05:30
size={"xl"}
2024-07-22 14:50:31 +05:30
isOpen={isOpen}
placement="right"
initialFocusRef={firstField}
onClose={onClose}
>
<DrawerOverlay />
<DrawerContent>
<DrawerCloseButton />
<DrawerHeader fontSize={"sm"}>Edit Key Merits</DrawerHeader>
<DrawerBody>
2024-08-02 16:56:53 +05:30
<HStack spacing={2} w={"100%"} flexWrap={"wrap"}>
<FormControl
mb={2}
w={"49%"}
isInvalid={!!errors.meritsHeader}
isRequired={true}
>
<FormLabel fontSize={"sm"}>Title (English)</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="meritsHeader"
control={control}
render={({ field }) => (
<Input {...field} fontSize={"sm"} type="text" size={"sm"} />
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsHeader?.message}
</FormErrorMessage>
</FormControl>
2024-08-02 16:56:53 +05:30
<FormControl
mb={2}
w={"49%"}
isInvalid={!!errors.meritsHeaderArabic}
isRequired={true}
>
<FormLabel fontSize={"sm"}>Title (Arabic)</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="meritsHeaderArabic"
2024-07-22 14:50:31 +05:30
control={control}
render={({ field }) => (
<Input
2024-08-02 16:56:53 +05:30
textAlign={"right"}
{...field}
fontSize={"sm"}
type="textarea"
size={"sm"}
/>
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsHeaderArabic?.message}
</FormErrorMessage>
</FormControl>
2024-08-02 16:56:53 +05:30
<FormControl
mb={2}
w={"49%"}
isInvalid={!!errors.meritsDescription}
isRequired={true}
>
<FormLabel fontSize={"sm"}>Description (English)</FormLabel>
<Controller
name="meritsDescription"
control={control}
render={({ field }) => (
<Textarea
2024-07-22 14:50:31 +05:30
{...field}
fontSize={"sm"}
type="textarea"
size={"sm"}
rows={2}
2024-07-22 14:50:31 +05:30
/>
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsDescription?.message}
</FormErrorMessage>
</FormControl>
2024-08-02 16:56:53 +05:30
<FormControl
mb={2}
w={"49%"}
isInvalid={!!errors.meritsDescriptionArabic}
isRequired={true}
>
<FormLabel fontSize={"sm"}>Description (Arabic)</FormLabel>
2024-07-22 14:50:31 +05:30
<Controller
name="meritsDescriptionArabic"
2024-07-22 14:50:31 +05:30
control={control}
render={({ field }) => (
<Textarea
2024-08-02 16:56:53 +05:30
rows={2}
textAlign={"right"}
{...field}
2024-07-22 14:50:31 +05:30
fontSize={"sm"}
type="textarea"
2024-07-22 14:50:31 +05:30
size={"sm"}
/>
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
{errors.meritsDescriptionArabic?.message}
2024-07-22 14:50:31 +05:30
</FormErrorMessage>
</FormControl>
2024-08-02 16:56:53 +05:30
<FormControl
w={"49%"}
isInvalid={!!errors.icon_xid}
isRequired={true}
>
<FormLabel fontSize={"sm"}>Select Icon</FormLabel>
<Controller
name="icon_xid"
control={control}
render={({ field }) => (
<Menu>
<MenuButton
2024-08-02 16:56:53 +05:30
h={34}
className="border"
bg="transparent"
size={"sm"}
fontWeight={500}
rounded={"sm"}
w={"100%"}
textAlign={"left"}
as={Button}
rightIcon={<ChevronDownIcon />}
2024-08-02 16:56:53 +05:30
border={
!errors.icon_xid?.message ||
(!selectedImageIcon && "2px solid #e53e3e")
}
>
<Box display="flex" alignItems="center">
2024-08-12 12:22:01 +05:30
{selectedImageIcon&&<Image
2024-08-20 14:52:13 +05:30
src={`${import.meta.env.VITE_IMAGE_URL}${selectedImageIcon}`}
alt={selectedImageIcon}
boxSize="1rem"
mr="12px"
2024-11-11 12:48:06 +05:30
/>}
<Text as={"span"} fontSize={"sm"} fontWeight={500}>
{selectedIcon}
</Text>
</Box>
</MenuButton>
2024-08-02 16:56:53 +05:30
<MenuList minW="415px" size={"sm"} fontWeight={500}>
{icons?.map(({ iconName, id, iconFilePath }) => (
<MenuItem
key={id}
onClick={() =>
handleIconSelect(id, iconName, iconFilePath)
}
>
<Box display="flex" alignItems="center">
<Image
2024-08-20 14:52:13 +05:30
src={`${import.meta.env.VITE_IMAGE_URL}${iconFilePath}`}
alt={iconName}
boxSize="1rem"
mr="12px"
/>
<Text
as={"span"}
fontSize={"sm"}
fontWeight={500}
>
{iconName}
</Text>
</Box>
</MenuItem>
))}
</MenuList>
</Menu>
)}
/>
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
2024-08-02 16:56:53 +05:30
{!errors.icon_xid?.message ||
(!selectedImageIcon && errors.icon_xid?.message)}
{errors.icon_xid?.message && errors.icon_xid?.message}
</FormErrorMessage>
</FormControl>
</HStack>
2024-07-22 14:50:31 +05:30
</DrawerBody>
2024-07-22 14:50:31 +05:30
<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}
2024-12-02 12:23:27 +05:30
message={"Are you sure you want to update this key merits?"}
2024-07-22 14:50:31 +05:30
isLoading={isLoading}
/>
</>
);
};
export default KeyMeritsEdit;