Files
tanami-admin-panel/src/Pages/IO_Management/CreateIO/InvestmentDocument.jsx

368 lines
9.7 KiB
React
Raw Normal View History

2024-07-08 20:39:43 +05:30
import {
Box,
Button,
HStack,
Input,
Text,
Tooltip,
useDisclosure,
useToast,
} from "@chakra-ui/react";
2024-07-05 15:28:02 +05:30
import React, { useContext, useEffect, useRef, useState } from "react";
2024-07-23 16:31:21 +05:30
import { useNavigate, useParams } from "react-router-dom";
2024-07-05 15:28:02 +05:30
import InvestmentDocuments from "../InvestmentDocuments";
import DataTable from "../../../Components/DataTable/DataTable";
import CustomAlertDialog from "../../../Components/CustomAlertDialog";
import GlobalStateContext from "../../../Contexts/GlobalStateContext";
import { debounce } from "../../Master/Sponser/AddSponser";
2024-07-08 20:39:43 +05:30
import {
AddIcon,
DeleteIcon,
DownloadIcon,
EditIcon,
ViewIcon,
} from "@chakra-ui/icons";
2024-07-09 12:35:24 +05:30
import { GrDocumentPdf } from "react-icons/gr";
2024-07-24 19:58:15 +05:30
import { AiOutlineFileGif, AiOutlineFileWord } from "react-icons/ai";
2024-07-09 18:56:11 +05:30
import InvestmentView from "../ViewIO/InvestmentView";
import InvestmentEdit from "../EditIO/InvestmentEdit";
2024-07-25 12:26:18 +05:30
import {
useDeleteIODocsMutation,
useGetInvestmentDocumentsQuery,
} from "../../../Services/io.service";
2024-07-23 16:31:21 +05:30
import ToastBox from "../../../Components/ToastBox";
import { getFileNameFromPath } from "../../../Constants/Constants";
2024-07-24 19:58:15 +05:30
import { TbFileTypeDocx } from "react-icons/tb";
import SetDisplayOrder from "./SetDisplayOrder";
2024-07-23 16:31:21 +05:30
const downloadFile = (filePath, fileName) => {
2024-08-02 16:56:53 +05:30
fetch("https://tanami.betadelivery.com/" + filePath)
2024-07-23 16:31:21 +05:30
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.blob();
})
.then((blob) => {
// Create a new Blob object with the correct MIME type
const fileBlob = new Blob([blob], { type: blob.type });
// Create a URL for the Blob object
const url = window.URL.createObjectURL(fileBlob);
// Create a link element to trigger the download
const link = document.createElement("a");
link.href = url;
// Set the download attribute with the specified file name
link.setAttribute("download", fileName);
// Append the link to the document and trigger the download
document.body.appendChild(link);
link.click();
// Clean up by revoking the URL and removing the link element
window.URL.revokeObjectURL(url);
document.body.removeChild(link);
})
.catch((error) => {
console.error("There was a problem with the file download:", error);
});
};
2024-08-12 12:22:01 +05:30
const InvestmentDocument = ({ control, errors, enableNextTab, index, }) => {
2024-07-25 12:26:18 +05:30
const params = useParams();
const id = params?.id;
2024-08-12 12:22:01 +05:30
const { slideFromRight, create, setCreate, IODetails } = useContext(GlobalStateContext);
2024-07-08 20:39:43 +05:30
const firstField = useRef();
2024-07-09 18:56:11 +05:30
const secondField = useRef();
const thirdField = useRef();
2024-07-08 20:39:43 +05:30
const [searchTerm, setSearchTerm] = useState("");
2024-07-23 16:31:21 +05:30
const [isLoading, setIsLoading] = useState(false);
2024-07-08 20:39:43 +05:30
const [deleteAlert, setDeleteAlert] = useState(false);
const [mouseEntered, setMouseEntered] = useState(false);
const [mouseEnteredId, setMouseEnteredId] = useState("");
const { isOpen, onOpen, onClose } = useDisclosure();
2024-07-09 18:56:11 +05:30
const {
isOpen: isViewOpen,
onOpen: onViewOpen,
onClose: onViewClose,
} = useDisclosure();
const {
isOpen: isEditOpen,
onOpen: onEditOpen,
onClose: onEditClose,
} = useDisclosure();
const [actionId, setActionId] = useState(null);
2024-07-08 20:39:43 +05:30
const navigate = useNavigate();
const toast = useToast();
2024-07-05 15:28:02 +05:30
2024-07-25 12:26:18 +05:30
const [deleteIODocs] = useDeleteIODocsMutation();
2024-07-23 16:31:21 +05:30
2024-08-12 12:22:01 +05:30
// const {
// data,
// error,
// isLoading: isIODocLoading,
// } = useGetInvestmentDocumentsQuery(id, {
// skip: !id,
// });
2024-07-05 15:28:02 +05:30
2024-07-08 20:39:43 +05:30
const tableHeadRow = ["Sr.no", "Type", "File Name", "Document", "Action"];
2024-07-05 15:28:02 +05:30
const handleUpdateStatus = debounce((id) => {
setCreate((prevCreate) =>
prevCreate.map((create) =>
create.id === id ? { ...create, status: !create.status } : create
)
);
toast({
2024-07-08 20:39:43 +05:30
render: () => (
<Box color="white" p={3} bg="green.500">
Status changed successfully!
</Box>
),
2024-07-05 15:28:02 +05:30
});
}, 300);
2024-08-12 12:22:01 +05:30
const filteredData = IODetails?.documents?.filter((item) =>
2024-07-23 16:31:21 +05:30
item?.documentName?.toLowerCase().includes(searchTerm.toLowerCase())
2024-07-08 20:39:43 +05:30
);
2024-07-05 15:28:02 +05:30
2024-07-09 18:56:11 +05:30
const handleView = (id) => {
2024-07-10 12:02:35 +05:30
setActionId(id);
2024-07-09 18:56:11 +05:30
onViewOpen();
};
const handleEdit = (id) => {
2024-07-10 12:02:35 +05:30
setActionId(id);
2024-07-09 18:56:11 +05:30
onEditOpen();
};
2024-07-23 16:31:21 +05:30
const handleDelete = async () => {
2024-07-25 12:26:18 +05:30
setIsLoading(true);
2024-07-23 16:31:21 +05:30
try {
2024-07-25 12:26:18 +05:30
const res = await deleteIODocs(actionId);
if (res?.data?.statusCode === 200) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
setIsLoading(false);
setDeleteAlert(false);
} else if (res?.error) {
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status={"error"} />
),
});
setIsLoading(false);
setDeleteAlert(false);
}
2024-07-23 16:31:21 +05:30
} catch (error) {
console.log(error);
}
2024-07-25 12:26:18 +05:30
};
2024-07-23 16:31:21 +05:30
const extractedArray = filteredData?.map((item, index) => ({
2024-07-05 20:04:32 +05:30
"Sr.no": (
2024-07-05 15:28:02 +05:30
<Text
2024-07-05 20:04:32 +05:30
justifyContent={slideFromRight ? "right" : "left"}
2024-07-08 20:39:43 +05:30
as="span"
color="teal.900"
fontWeight="500"
2024-07-05 15:28:02 +05:30
className="d-flex align-items-center web-text-small"
>
2024-07-05 20:04:32 +05:30
{index + 1}
2024-07-05 15:28:02 +05:30
</Text>
),
2024-07-08 20:39:43 +05:30
Type: (
2024-07-05 20:04:32 +05:30
<Text
justifyContent={slideFromRight ? "right" : "left"}
2024-07-08 20:39:43 +05:30
as="span"
color="teal.900"
fontWeight="500"
2024-07-09 12:35:24 +05:30
className="d-flex align-items-center"
2024-07-09 18:56:11 +05:30
fontSize={"xl"}
2024-07-05 20:04:32 +05:30
>
2024-07-25 12:26:18 +05:30
{item.documentType === "application/pdf" ? (
<GrDocumentPdf />
) : (
<TbFileTypeDocx fontSize={21} />
)}
2024-07-05 20:04:32 +05:30
</Text>
2024-07-05 15:28:02 +05:30
),
2024-07-05 20:04:32 +05:30
"File Name": (
2024-07-08 20:39:43 +05:30
<Box w="200px" isTruncated>
<Text as="span" color="teal.900" fontWeight="500">
2024-07-23 16:31:21 +05:30
{item.documentName}
2024-07-05 15:28:02 +05:30
</Text>
</Box>
),
2024-07-08 20:39:43 +05:30
Document: (
2024-07-05 20:04:32 +05:30
<Text
justifyContent={slideFromRight ? "right" : "left"}
2024-07-08 20:39:43 +05:30
as="span"
color="teal.900"
fontWeight="500"
2024-07-05 20:04:32 +05:30
className="d-flex align-items-center web-text-small"
>
{getFileNameFromPath(item?.documentPath)}
2024-07-05 20:04:32 +05:30
</Text>
),
Action: (
2024-07-10 12:02:35 +05:30
<Box display="flex" justifyContent="center" gap={2}>
2024-07-22 14:50:31 +05:30
{/* <Tooltip
2024-07-08 20:39:43 +05:30
rounded="sm"
fontSize="xs"
2024-07-05 20:04:32 +05:30
label="View"
bg="#fff"
2024-07-08 20:39:43 +05:30
color="green.500"
2024-07-05 20:04:32 +05:30
placement="top"
>
<Button
_hover={{ color: "green.500" }}
2024-07-09 18:56:11 +05:30
onClick={() => handleView(item.id)}
2024-07-05 20:04:32 +05:30
color="green.300"
2024-07-08 20:39:43 +05:30
rounded="sm"
size="xs"
2024-07-05 20:04:32 +05:30
>
<ViewIcon />
</Button>
2024-07-22 14:50:31 +05:30
</Tooltip> */}
2024-07-05 20:04:32 +05:30
<Tooltip
2024-07-08 20:39:43 +05:30
rounded="sm"
fontSize="xs"
2024-07-05 20:04:32 +05:30
label="Edit"
bg="#fff"
2024-07-08 20:39:43 +05:30
color="blue.500"
2024-07-05 20:04:32 +05:30
placement="top"
>
<Button
_hover={{ color: "blue.500" }}
2024-07-09 18:56:11 +05:30
onClick={() => handleEdit(item.id)}
2024-07-05 20:04:32 +05:30
color="blue.400"
2024-07-08 20:39:43 +05:30
rounded="sm"
size="xs"
2024-07-05 20:04:32 +05:30
>
<EditIcon />
</Button>
</Tooltip>
<Tooltip
2024-07-08 20:39:43 +05:30
rounded="sm"
fontSize="xs"
label="Download"
2024-07-05 20:04:32 +05:30
bg="#fff"
2024-07-08 20:39:43 +05:30
color="blue.500"
2024-07-05 20:04:32 +05:30
placement="top"
>
<Button
_hover={{ color: "blue.500" }}
color="blue.400"
2024-07-08 20:39:43 +05:30
rounded="sm"
size="xs"
2024-07-25 12:26:18 +05:30
onClick={() => downloadFile(item?.documentPath, item?.documentName)}
2024-07-05 20:04:32 +05:30
>
<DownloadIcon />
</Button>
</Tooltip>
<Tooltip
2024-07-08 20:39:43 +05:30
rounded="sm"
fontSize="xs"
2024-07-05 20:04:32 +05:30
label="Delete"
bg="#fff"
2024-07-08 20:39:43 +05:30
color="red.500"
2024-07-05 20:04:32 +05:30
placement="top"
>
<Button
onClick={() => {
2024-07-08 20:39:43 +05:30
setActionId(item.id);
2024-07-05 20:04:32 +05:30
setDeleteAlert(true);
}}
_hover={{ color: "red.500" }}
color="red.300"
2024-07-08 20:39:43 +05:30
rounded="sm"
size="xs"
2024-07-05 20:04:32 +05:30
>
<DeleteIcon />
</Button>
</Tooltip>
</Box>
2024-07-05 15:28:02 +05:30
),
}));
2024-07-24 19:58:15 +05:30
2024-07-05 15:28:02 +05:30
return (
<Box>
<Box display="flex" justifyContent="end" mb={4} gap={2}>
<SetDisplayOrder data={filteredData} />
2024-07-05 15:28:02 +05:30
<Button
leftIcon={<AddIcon />}
onClick={onOpen}
2024-07-08 20:39:43 +05:30
size="sm"
fontSize="xs"
rounded="sm"
2024-07-25 12:26:18 +05:30
colorScheme="forestGreen"
2024-07-05 15:28:02 +05:30
>
2024-07-09 18:56:11 +05:30
Add Document
2024-07-05 15:28:02 +05:30
</Button>
2024-07-25 12:26:18 +05:30
<InvestmentDocuments
2024-07-09 18:56:11 +05:30
create={create}
setCreate={setCreate}
2024-07-05 15:28:02 +05:30
isOpen={isOpen}
onClose={onClose}
firstField={firstField}
/>
2024-07-09 18:56:11 +05:30
<InvestmentView
2024-07-10 12:02:35 +05:30
id={actionId}
2024-07-09 18:56:11 +05:30
isOpen={isViewOpen}
onClose={onViewClose}
secondField={secondField}
/>
<InvestmentEdit
2024-08-12 12:22:01 +05:30
data={IODetails?.documents}
2024-07-10 12:02:35 +05:30
id={actionId}
2024-07-09 18:56:11 +05:30
isOpen={isEditOpen}
onClose={onEditClose}
thirdField={thirdField}
/>
2024-07-05 15:28:02 +05:30
</Box>
<DataTable
2024-07-24 19:58:15 +05:30
emptyMessage="We don't have any Documents"
2024-07-05 15:28:02 +05:30
tableHeadRow={tableHeadRow}
data={extractedArray}
2024-07-10 12:02:35 +05:30
isLoading={false}
2024-07-05 15:28:02 +05:30
viewActionId={actionId}
setViewActionId={setActionId}
setMouseEnteredId={setMouseEnteredId}
setMouseEntered={setMouseEntered}
/>
2024-07-24 19:58:15 +05:30
{/* <HStack justifyContent="flex-end">
2024-07-08 20:39:43 +05:30
<Button
ps={8}
pe={8}
2024-07-24 19:58:15 +05:30
colorScheme="forestGreen"
variant={'outline'}
2024-07-08 20:39:43 +05:30
size="sm"
rounded="sm"
onClick={() => enableNextTab(index)}
>
Next
</Button>
2024-07-24 19:58:15 +05:30
</HStack> */}
2024-07-05 15:28:02 +05:30
<CustomAlertDialog
onClose={() => setDeleteAlert(false)}
isOpen={deleteAlert}
2024-07-08 20:39:43 +05:30
message="Are you sure you want to delete the sponsor?"
2024-07-05 15:28:02 +05:30
alertHandler={handleDelete}
isLoading={isLoading}
/>
</Box>
);
};
export default InvestmentDocument;