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

310 lines
8.9 KiB
React
Raw Normal View History

2024-07-05 12:07:52 +05:30
import {
Box,
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
FormControl,
2024-08-13 13:46:41 +05:30
FormHelperText,
2024-07-05 12:07:52 +05:30
FormLabel,
Input,
2024-07-08 20:39:43 +05:30
Text,
2024-07-23 16:31:21 +05:30
useToast,
2024-07-05 12:07:52 +05:30
} from "@chakra-ui/react";
import * as yup from "yup";
2024-07-08 20:39:43 +05:30
import React, { useContext, useState } from "react";
2024-07-05 12:07:52 +05:30
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
2024-07-05 15:28:02 +05:30
import CustomAlertDialog from "../../Components/CustomAlertDialog";
2024-07-24 19:58:15 +05:30
import { v4 as uuidv4 } from "uuid";
import { useNavigate, useParams } from "react-router-dom";
2024-07-08 20:39:43 +05:30
import { GrDocumentPdf } from "react-icons/gr";
import { FaRegFileAlt } from "react-icons/fa";
import { TbFileTypeDocx } from "react-icons/tb";
import { AiOutlineFileGif } from "react-icons/ai";
import GlobalStateContext from "../../Contexts/GlobalStateContext";
import InvestmentDocument from "./CreateIO/InvestmentDocument";
2024-07-23 16:31:21 +05:30
import { useCreateInvestmentDocumentsMutation } from "../../Services/io.service";
import ToastBox from "../../Components/ToastBox";
2024-07-05 12:07:52 +05:30
2024-07-08 20:39:43 +05:30
export const bytesToMB = (bytes) => {
if (bytes === 0) return "0 MB";
const bytesInMB = 1024 * 1024;
return (bytes / bytesInMB).toFixed(2) + " MB";
};
2024-07-10 12:02:35 +05:30
export const getFileIcon = (type) => {
switch (type) {
2024-07-24 19:58:15 +05:30
case "application/pdf":
2024-07-10 12:02:35 +05:30
return <GrDocumentPdf />;
2024-07-24 19:58:15 +05:30
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
case "application/msword":
return <TbFileTypeDocx fontSize={"20px"} />;
2024-07-24 19:58:15 +05:30
case "image/jpeg":
case "image/png":
case "image/gif":
2024-07-10 12:02:35 +05:30
return <AiOutlineFileGif fontSize={25} />;
default:
return <FaRegFileAlt />;
}
};
2024-07-08 20:39:43 +05:30
export const investmentDocSchema = yup.object().shape({
document: yup.mixed().required("Document is required"),
2024-07-24 19:58:15 +05:30
// .test("fileSize", "File size should be less than 2MB", value => {
// console.log("File size:", value ? value.size : "No file");
// return value && value.size <= 2 * 1024 * 1024; // 2MB in bytes
// })
2024-10-24 19:05:52 +05:30
fileName: yup.string().required("File name is required")
.max(25, "File name must be at most 25 characters"), // Maximum length validation,
2024-08-12 12:22:01 +05:30
documentNameArabic: yup.string().required("File name Arabic is required")
2024-10-24 19:05:52 +05:30
.max(25, "File name must be at most 30 characters"),
2024-07-05 12:07:52 +05:30
});
2024-07-24 19:58:15 +05:30
const InvestmentDocuments = ({
isOpen,
onClose,
firstField,
create,
setCreate,
}) => {
const toast = useToast();
const params = useParams();
2024-07-08 20:39:43 +05:30
const [file, setFile] = useState(null);
2024-07-05 15:28:02 +05:30
const [alert, setAlert] = useState(false);
2024-07-24 19:58:15 +05:30
const [formData, setFormData] = useState(null);
const [isLoading, setIsLoading] = useState(false);
2024-07-22 14:50:31 +05:30
// =====================[ variables ]
2024-07-24 19:58:15 +05:30
const id = params?.id;
2024-07-22 14:50:31 +05:30
// =====================[ RTK Api calls ]
2024-07-24 19:58:15 +05:30
const [createInvestmentDocument] = useCreateInvestmentDocumentsMutation();
2024-07-22 14:50:31 +05:30
2024-07-08 20:39:43 +05:30
const navigate = useNavigate();
2024-07-05 12:07:52 +05:30
const {
2024-07-08 20:39:43 +05:30
register,
2024-07-05 12:07:52 +05:30
handleSubmit,
2024-07-24 19:58:15 +05:30
reset,
2024-07-05 12:07:52 +05:30
formState: { errors },
} = useForm({
2024-07-08 20:39:43 +05:30
resolver: yupResolver(investmentDocSchema),
2024-07-05 12:07:52 +05:30
});
2024-07-22 14:50:31 +05:30
const onSubmit = async (data) => {
2024-07-24 19:58:15 +05:30
if (Object.keys(errors).length === 0) {
const formData = new FormData();
formData.append("documentName", data.fileName);
2024-08-12 12:22:01 +05:30
formData.append("documentNameArabic", data.documentNameArabic);
2024-07-24 19:58:15 +05:30
formData.append("document", data?.document[0]);
setFormData(formData);
setAlert(true);
}
};
const handleConfirm = async () => {
setIsLoading(true);
2024-07-22 14:50:31 +05:30
try {
2024-07-24 19:58:15 +05:30
const res = await createInvestmentDocument({ data: formData, id });
2024-08-12 12:22:01 +05:30
2024-07-23 16:31:21 +05:30
if (res?.error) {
toast({
render: () => (
2024-07-24 19:58:15 +05:30
<ToastBox
message={
res?.error?.data?.message
? res?.error?.data?.message
: "File size is too large"
}
status={"error"}
/>
2024-07-23 16:31:21 +05:30
),
});
2024-07-24 19:58:15 +05:30
setIsLoading(false);
} else if (res?.data?.statusCode === 201) {
2024-07-23 16:31:21 +05:30
toast({
2024-07-24 19:58:15 +05:30
render: () => <ToastBox message={res?.data?.message} />,
2024-07-23 16:31:21 +05:30
});
2024-07-24 19:58:15 +05:30
onClose();
setIsLoading(false);
setFile(null);
reset();
2024-07-23 16:31:21 +05:30
}
2024-07-22 14:50:31 +05:30
} catch (error) {
console.log(error);
2024-07-24 19:58:15 +05:30
if (error) {
toast({
render: () => (
<ToastBox
message={"Something went wrong please try again!"}
status={"error"}
/>
),
});
console.log(error);
onClose();
setIsLoading(false);
setFile(null);
reset();
}
2024-07-22 14:50:31 +05:30
}
2024-07-24 19:58:15 +05:30
setAlert(false);
2024-07-08 20:39:43 +05:30
};
const handleFileChange = (event) => {
const selectedFile = event.target.files[0];
setFile(selectedFile);
2024-07-05 12:07:52 +05:30
};
2024-07-08 20:39:43 +05:30
const getFileTitle = (type) => {
switch (type) {
2024-07-24 19:58:15 +05:30
case "application/pdf":
2024-07-08 20:39:43 +05:30
return "PDF";
2024-07-24 19:58:15 +05:30
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
case "application/msword":
2024-07-08 20:39:43 +05:30
return "DOCX";
2024-07-24 19:58:15 +05:30
case "image/jpeg":
case "image/png":
case "image/gif":
2024-07-08 20:39:43 +05:30
return "JPG";
default:
return "HH";
}
};
2024-07-05 15:28:02 +05:30
2024-07-24 19:58:15 +05:30
const handleClose = () => {
onClose();
setAlert(false);
reset();
setFile(null);
};
2024-07-05 12:07:52 +05:30
return (
<>
2024-07-08 20:39:43 +05:30
<Drawer
size={"md"}
2024-07-05 12:07:52 +05:30
isOpen={isOpen}
placement="right"
initialFocusRef={firstField}
onClose={onClose}
>
<DrawerOverlay />
<DrawerContent>
2024-11-05 13:25:39 +05:30
<DrawerCloseButton onClick={handleClose} />
2024-10-30 12:30:20 +05:30
<DrawerHeader fontSize="sm">Add Investment Documents</DrawerHeader>
2024-07-08 20:39:43 +05:30
<Box as="form" onSubmit={handleSubmit(onSubmit)}>
<DrawerBody>
2024-07-24 19:58:15 +05:30
<FormControl mb={4} isRequired>
2024-07-08 20:39:43 +05:30
<FormLabel fontSize="sm">File Name</FormLabel>
<Input
name="fileName"
2024-10-24 19:05:52 +05:30
{...register("fileName")}
2024-07-08 20:39:43 +05:30
fontSize="sm"
type="text"
size="sm"
2024-10-24 19:05:52 +05:30
maxLength={25} // Maximum length constraint in the input field
2024-07-08 20:39:43 +05:30
/>
{errors.fileName && (
2024-07-24 19:58:15 +05:30
<Text mt={1} fontSize="xs" fontWeight={500} color="red">
2024-07-08 20:39:43 +05:30
{errors.fileName.message}
</Text>
)}
</FormControl>
2024-08-12 12:22:01 +05:30
<FormControl mb={4} isRequired>
<FormLabel fontSize="sm">File Name ( Arabic )</FormLabel>
<Input
name="documentNameArabic"
{...register("documentNameArabic")}
fontSize="sm"
type="text"
size="sm"
textAlign={'right'}
2024-10-24 19:05:52 +05:30
maxLength={30} // Maximum length constraint in the input field
2024-08-12 12:22:01 +05:30
/>
{errors.documentNameArabic && (
<Text mt={1} fontSize="xs" fontWeight={500} color="red">
{errors.documentNameArabic.message}
</Text>
)}
</FormControl>
2024-07-24 19:58:15 +05:30
<FormControl mb={4} isRequired>
2024-07-08 20:39:43 +05:30
<FormLabel fontSize="sm">Document</FormLabel>
<Input
name="document"
{...register("document")}
fontSize="sm"
2024-07-24 19:58:15 +05:30
className="form-control"
2024-07-08 20:39:43 +05:30
type="file"
size="sm"
onChange={handleFileChange}
2024-07-24 19:58:15 +05:30
accept=".pdf, .doc, .docx"
2024-07-08 20:39:43 +05:30
/>
{errors.document && (
2024-07-24 19:58:15 +05:30
<Text mt={1} fontSize="xs" fontWeight={500} color="red">
2024-07-08 20:39:43 +05:30
{errors.document.message}
</Text>
)}
2024-08-13 13:46:41 +05:30
<FormHelperText mt={1} fontSize="xs" fontWeight={500} color="gray.500">File size should be max 2mb</FormHelperText>
2024-07-08 20:39:43 +05:30
</FormControl>
{file && (
<Box mt={4}>
<Box display="flex" mb={2}>
2024-07-09 12:35:24 +05:30
<Text>{getFileIcon(file.type)}</Text>
2024-07-24 19:58:15 +05:30
<Text fontSize="sm" ml={2} mb={0}>
2024-07-08 20:39:43 +05:30
{file.name}
</Text>
</Box>
<Text fontSize="sm">{bytesToMB(file.size)}</Text>
</Box>
)}
</DrawerBody>
<DrawerFooter>
<Button
variant="outline"
2024-07-24 19:58:15 +05:30
colorScheme="forestGreen"
2024-07-08 20:39:43 +05:30
rounded="sm"
size="sm"
mr={3}
2024-07-24 19:58:15 +05:30
onClick={handleClose}
2024-07-08 20:39:43 +05:30
>
Cancel
</Button>
<Button
colorScheme="forestGreen"
rounded="sm"
size="sm"
type="submit"
>
2024-07-08 20:39:43 +05:30
Save
</Button>
</DrawerFooter>
</Box>
2024-07-05 12:07:52 +05:30
</DrawerContent>
</Drawer>
2024-07-05 15:28:02 +05:30
<CustomAlertDialog
isOpen={alert}
onClose={() => setAlert(false)}
2024-07-24 19:58:15 +05:30
alertHandler={handleConfirm}
isLoading={isLoading}
2024-07-08 20:39:43 +05:30
message="Are you sure you want to add this document?"
2024-07-05 15:28:02 +05:30
/>
2024-07-05 12:07:52 +05:30
</>
);
};
export default InvestmentDocuments;