2024-07-22 14:50:31 +05:30
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Drawer,
|
|
|
|
|
DrawerBody,
|
|
|
|
|
DrawerCloseButton,
|
|
|
|
|
DrawerContent,
|
|
|
|
|
DrawerFooter,
|
|
|
|
|
DrawerHeader,
|
|
|
|
|
DrawerOverlay,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormErrorMessage,
|
|
|
|
|
FormLabel,
|
2024-07-26 12:10:47 +05:30
|
|
|
HStack,
|
|
|
|
|
Image,
|
2024-07-22 14:50:31 +05:30
|
|
|
Input,
|
2024-07-26 12:10:47 +05:30
|
|
|
Menu,
|
|
|
|
|
MenuButton,
|
|
|
|
|
MenuItem,
|
|
|
|
|
MenuList,
|
2024-07-22 14:50:31 +05:30
|
|
|
Stack,
|
2024-07-26 12:10:47 +05:30
|
|
|
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";
|
2024-07-26 12:10:47 +05:30
|
|
|
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"),
|
2024-07-26 12:10:47 +05:30
|
|
|
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
|
2024-07-22 14:50:31 +05:30
|
|
|
});
|
|
|
|
|
|
2024-07-26 12:10:47 +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);
|
2024-07-26 12:10:47 +05:30
|
|
|
const [editKeyMerits] = useUpdateKeyMeritsMutation();
|
|
|
|
|
const [selectedIcon, setSelectedIcon] = useState("Select Icon");
|
|
|
|
|
const [selectedImageIcon, setSelectedImageIcon] = useState(null);
|
2024-07-22 14:50:31 +05:30
|
|
|
|
2024-07-26 12:10:47 +05:30
|
|
|
const found = data?.find((item) => item?.id === actionId);
|
2024-07-22 14:50:31 +05:30
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
control,
|
2024-07-26 12:10:47 +05:30
|
|
|
reset,setValue,
|
2024-07-22 14:50:31 +05:30
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = useForm({
|
|
|
|
|
resolver: yupResolver(keyMeritsSchema),
|
|
|
|
|
});
|
2024-07-26 12:10:47 +05:30
|
|
|
// 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]);
|
2024-07-22 14:50:31 +05:30
|
|
|
|
|
|
|
|
const onSubmit = async (data) => {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
try {
|
2024-07-26 12:10:47 +05:30
|
|
|
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} />,
|
|
|
|
|
});
|
|
|
|
|
setAlert(false);
|
|
|
|
|
onClose();
|
|
|
|
|
setIsLoading(false);
|
2024-07-26 12:10:47 +05:30
|
|
|
reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (res?.error?.data?.code === 400) {
|
2024-07-22 14:50:31 +05:30
|
|
|
toast({
|
2024-07-26 12:10:47 +05:30
|
|
|
render: () => (
|
|
|
|
|
<ToastBox message={res?.error?.data?.message} status={"error"} />
|
|
|
|
|
),
|
2024-07-22 14:50:31 +05:30
|
|
|
});
|
|
|
|
|
setAlert(false);
|
|
|
|
|
onClose();
|
|
|
|
|
setIsLoading(false);
|
2024-07-26 12:10:47 +05:30
|
|
|
reset();
|
|
|
|
|
return;
|
2024-07-22 14:50:31 +05:30
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error) {
|
2024-07-26 12:10:47 +05:30
|
|
|
console.log(error);
|
2024-07-22 14:50:31 +05:30
|
|
|
toast({
|
2024-07-26 12:10:47 +05:30
|
|
|
render: () => (
|
|
|
|
|
<ToastBox
|
|
|
|
|
message={"Something went wrong, please try again!"}
|
|
|
|
|
status={"error"}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2024-07-22 14:50:31 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
setAlert(false);
|
|
|
|
|
onClose();
|
2024-07-26 12:10:47 +05:30
|
|
|
reset();
|
2024-07-22 14:50:31 +05:30
|
|
|
}
|
2024-07-26 12:10:47 +05:30
|
|
|
reset();
|
2024-07-22 14:50:31 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSave = () => {
|
|
|
|
|
handleSubmit(onSubmit)();
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-26 12:10:47 +05:30
|
|
|
console.log(errors);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleIconSelect = (id, iconName, iconFilePath) => {
|
|
|
|
|
setValue("icon_xid", id);
|
|
|
|
|
setSelectedIcon(iconName); // Update selected icon name
|
|
|
|
|
setSelectedImageIcon(iconFilePath);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 14:50:31 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Drawer
|
2024-07-26 12:10:47 +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-07-26 12:10:47 +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-07-26 12:10:47 +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
|
2024-07-26 12:10:47 +05:30
|
|
|
name="meritsHeaderArabic"
|
2024-07-22 14:50:31 +05:30
|
|
|
control={control}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<Input
|
2024-07-26 12:10:47 +05:30
|
|
|
textAlign={'right'}
|
|
|
|
|
{...field}
|
|
|
|
|
fontSize={"sm"}
|
|
|
|
|
type="textarea"
|
|
|
|
|
size={"sm"}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
|
|
|
|
|
{errors.meritsHeaderArabic?.message}
|
|
|
|
|
</FormErrorMessage>
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<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"}
|
2024-07-26 12:10:47 +05:30
|
|
|
rows={2}
|
2024-07-22 14:50:31 +05:30
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
|
|
|
|
|
{errors.meritsDescription?.message}
|
|
|
|
|
</FormErrorMessage>
|
|
|
|
|
</FormControl>
|
2024-07-26 12:10:47 +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
|
2024-07-26 12:10:47 +05:30
|
|
|
name="meritsDescriptionArabic"
|
2024-07-22 14:50:31 +05:30
|
|
|
control={control}
|
2024-07-26 12:10:47 +05:30
|
|
|
render={({ field }) => (
|
|
|
|
|
<Textarea
|
|
|
|
|
rows={2}
|
|
|
|
|
textAlign={'right'}
|
|
|
|
|
{...field}
|
2024-07-22 14:50:31 +05:30
|
|
|
fontSize={"sm"}
|
2024-07-26 12:10:47 +05:30
|
|
|
type="textarea"
|
2024-07-22 14:50:31 +05:30
|
|
|
size={"sm"}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2024-07-26 12:10:47 +05:30
|
|
|
<FormErrorMessage fontSize={"xs"} fontWeight={500}>
|
|
|
|
|
{errors.meritsDescriptionArabic?.message}
|
2024-07-22 14:50:31 +05:30
|
|
|
</FormErrorMessage>
|
|
|
|
|
</FormControl>
|
2024-07-26 12:10:47 +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
|
|
|
|
|
h={34}
|
|
|
|
|
className="border"
|
|
|
|
|
bg="transparent"
|
|
|
|
|
size={"sm"}
|
|
|
|
|
fontWeight={500}
|
|
|
|
|
rounded={"sm"}
|
|
|
|
|
w={"100%"}
|
|
|
|
|
textAlign={"left"}
|
|
|
|
|
as={Button}
|
|
|
|
|
rightIcon={<ChevronDownIcon />}
|
|
|
|
|
border={!errors.icon_xid?.message || !selectedImageIcon && "2px solid #e53e3e"}
|
|
|
|
|
>
|
|
|
|
|
<Box display="flex" alignItems="center">
|
|
|
|
|
<Image
|
|
|
|
|
src={`https://admin.tanami.betadelivery.com/${selectedImageIcon}`}
|
|
|
|
|
alt={selectedImageIcon}
|
|
|
|
|
boxSize="1rem"
|
|
|
|
|
mr="12px"
|
|
|
|
|
/>{" "}
|
|
|
|
|
<Text as={"span"} fontSize={"sm"} fontWeight={500}>
|
|
|
|
|
{selectedIcon}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
</MenuButton>
|
|
|
|
|
<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
|
|
|
|
|
src={`https://admin.tanami.betadelivery.com/${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}>
|
|
|
|
|
{!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-26 12:10:47 +05:30
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
message={"Are you sure you want to add this key merit?"}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default KeyMeritsEdit;
|