Files
tanami-admin-panel/src/Pages/IO_Management/ViewIO/HeaderModal/UpdateIOStatus.jsx

192 lines
5.4 KiB
React
Raw Normal View History

import { ChevronDownIcon } from "@chakra-ui/icons";
2024-08-12 12:22:01 +05:30
import React, { useEffect, useState } from "react";
import {
Badge,
Button,
2024-08-14 12:19:27 +05:30
FormControl,
2024-07-09 19:05:08 +05:30
FormLabel,
Menu,
MenuButton,
MenuItem,
MenuList,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
2024-08-14 12:19:27 +05:30
FormErrorMessage
} from "@chakra-ui/react";
import {
useGetIOprepopulateDataQuery,
useUpdateStatusIoMutation,
} from "../../../../Services/io.service";
import { useParams } from "react-router-dom";
2024-08-14 12:19:27 +05:30
const UpdateIOStatus = ({ isOpen, onClose, status }) => {
const params = useParams();
const id = params?.id;
2024-08-14 12:19:27 +05:30
const [selectedItem, setSelectedItem] = useState();
const [isLoadingg, setIsLoading] = useState(false);
2024-08-14 12:19:27 +05:30
const [error, setError] = useState("");
2024-08-16 15:02:02 +05:30
const [selectedStatusId, setSelectedStatusId] = useState('');
2024-08-14 12:19:27 +05:30
const { data } = useGetIOprepopulateDataQuery();
2024-08-12 12:22:01 +05:30
const [updateStatusIo] = useUpdateStatusIoMutation();
2024-08-16 15:02:02 +05:30
// useEffect(() => {
// setSelectedStatusId(status?.[0]?.id);
// }, [status]);
2024-08-14 12:19:27 +05:30
const handleMenuItemClick = (item, id) => {
setSelectedItem(item);
setSelectedStatusId(id);
};
const handleSubmit = async () => {
2024-08-14 12:19:27 +05:30
if (!selectedStatusId) {
2024-08-16 15:02:02 +05:30
setError("Please select status");
2024-08-14 12:19:27 +05:30
return;
}
setError("");
setIsLoading(true);
try {
const res = await updateStatusIo({
data: {
ioStatus_xid: selectedStatusId,
},
id,
});
console.log(res);
2024-08-14 12:19:27 +05:30
setIsLoading(false);
handleClose();
} catch (error) {
setIsLoading(false);
}
};
2024-08-14 12:19:27 +05:30
const handleClose = () => {
2024-08-16 15:02:02 +05:30
setSelectedItem(null)
setSelectedStatusId(null)
2024-08-14 12:19:27 +05:30
onClose()
2024-08-16 18:23:55 +05:30
setError("")
2024-08-14 12:19:27 +05:30
}
return (
2024-08-14 12:19:27 +05:30
<Modal isOpen={isOpen} onClose={handleClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader fontSize={"md"}>Update IO Status Transaction</ModalHeader>
<ModalCloseButton />
<ModalBody>
2024-08-14 12:19:27 +05:30
<FormControl isInvalid={!!error}>
<FormLabel as={"label"} fontSize={"sm"} fontWeight={500}>
Status
</FormLabel>
<Menu>
<MenuButton
as={Button}
rightIcon={<ChevronDownIcon />}
fontSize={"sm"}
fontWeight={500}
w={"100%"}
textAlign={"left"}
>
{selectedItem ? (
2024-08-02 20:19:41 +05:30
<Badge
2024-08-14 12:19:27 +05:30
rounded={"full"}
pt={1.5}
pb={1.5}
ps={4}
pe={4}
mt={1.5}
mb={1.5}
textTransform={"none"}
colorScheme={
selectedItem === "Draft"
? "gray"
: selectedItem === "Processing"
? "yellow"
: selectedItem === "Open"
? "blue"
: selectedItem === "Closed"
? "green"
: selectedItem === "Exited"
? "red"
: selectedItem === "Canclled"
? "orange"
: "purple"
}
py={"3px"} px={"8px"}
>
{selectedItem}
</Badge>
2024-08-14 12:19:27 +05:30
) : "Select Item"}
</MenuButton>
{status?.length > 0 ?<MenuList w={"400px"}>
{status?.map(({ id, statusAdmin }) => (
<MenuItem
key={id}
fontSize={"sm"}
onClick={() => handleMenuItemClick(statusAdmin, id)}
>
<Badge
rounded={"full"}
pt={1.5}
pb={1.5}
ps={4}
pe={4}
mt={1.5}
mb={1.5}
textTransform={"none"}
colorScheme={
statusAdmin === "Draft"
? "gray"
: statusAdmin === "Processing"
? "yellow"
: statusAdmin === "Open"
? "blue"
: statusAdmin === "Closed"
? "green"
: statusAdmin === "Exited"
? "red"
: statusAdmin === "Canclled"
? "orange"
: "purple"
}
py={"1px"} px={"8px"}
>
{statusAdmin}
</Badge>
</MenuItem>
))}
</MenuList>:""}
</Menu>
<FormErrorMessage fontSize={'xs'} fontWeight={600} >{error}</FormErrorMessage>
</FormControl>
</ModalBody>
<ModalFooter>
<Button
2024-08-08 19:38:17 +05:30
colorScheme="forestGreen"
mr={3}
color={"#fff"}
size={"sm"}
2024-07-09 19:05:08 +05:30
rounded={"sm"}
onClick={handleSubmit}
isLoading={isLoadingg}
>
Save
</Button>
2024-08-14 12:19:27 +05:30
<Button size={"sm"} rounded={"sm"} mr={3} onClick={handleClose}>
Close
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
};
export default UpdateIOStatus;