import { Box, Button, Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerFooter, DrawerHeader, DrawerOverlay, FormControl, FormErrorMessage, FormLabel, Icon, Image, Input, Stack, Text, useToast, } from "@chakra-ui/react"; import * as yup from "yup"; 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 { useCreateImageArtifactsMutation, useUpdateImageArtifactsMutation, } from "../../Services/io.service"; import { useParams } from "react-router-dom"; import ToastBox from "../../Components/ToastBox"; import { bytesToMB } from "./InvestmentDocuments"; import { formatTimestampInGulfTimezone } from "../../Constants/Constants"; import { IoMdRemoveCircleOutline } from "react-icons/io"; const investmentImageSchema = yup.object().shape({ artifactName: yup.string().required("Artifact image name is required"), artifactPathName: yup.mixed().required("Artifact image is required"), }); const IOArtifactsAdd = ({ isOpen, onClose, firstField, actionId, setActionId, data, }) => { const params = useParams(); const id = params?.id; const [file, setFile] = useState(null); const [preview, setPreview] = useState(null); const [alert, setAlert] = useState(false); const [loading, setIsLoading] = useState(false); const toast = useToast(); const [createImageArtifacts] = useCreateImageArtifactsMutation(); const [updateImageArtifacts] = useUpdateImageArtifactsMutation(); const found = data?.find((item) => item?.id === actionId); const fileInputRef = useRef(null); const { control, handleSubmit, reset, setValue, watch, formState: { errors }, } = useForm({ resolver: yupResolver(investmentImageSchema), }); // useEffect to reset the form when `found` changes useEffect(() => { if (found) { reset({ artifactName: found?.artifactName, artifactPathName: found?.artifactPathName, }); } }, [found, reset]); const onSubmit = async (data) => { console.log("hit"); setIsLoading(true); const formData = new FormData(); formData.append("artifactName", data.artifactName); file && formData.append("artifactPathName", file); // Assuming artifactPathName is an array of files try { if (found) { const res = await updateImageArtifacts({ data: formData, id: found?.id, }); if (res?.data?.statusCode === 200) { toast({ render: () => , }); setFile(null); setIsLoading(false); setAlert(false); setPreview(null); onClose(); reset({ artifactName: "", artifactPathName: "", }); } else { toast({ render: () => ( ), }); setFile(null); setIsLoading(false); setAlert(false); setPreview(null); onClose(); reset({ artifactName: "", artifactPathName: "", }); } if (res?.error) { toast({ render: () => ( ), }); reset(); setIsLoading(false); } } else { const res = await createImageArtifacts({ data: formData, id }); if (res?.data?.statusCode === 200) { toast({ render: () => , }); reset({ artifactName: "", artifactPathName: "", }); setFile(null); setIsLoading(false); setAlert(false); setPreview(null); onClose(); } } } catch (error) { console.log(error); } // Close the form/modal // onClose(); }; const handleFileChange = (e) => { const file = e.target.files[0]; setFile(file); setValue("artifactPathName", file); const reader = new FileReader(); reader.onloadend = () => { setPreview(reader.result); }; reader.readAsDataURL(file); }; const handleSave = () => { handleSubmit(onSubmit)(); }; const handleClose = () => { setFile(null); setPreview(null); reset({ artifactName: "", artifactPathName: "", }); onClose(); setActionId(false); }; const handleRemove = () => { setFile(null); setPreview(null); // Reset the file input value if (fileInputRef.current) { fileInputRef.current.value = ""; } }; const handleAlertClose = () => { setAlert(false); setIsLoading(false); }; return ( <> IO Artifacts Image Artifact Name ( )} /> {errors.artifactName?.message} Artifact Image {!preview && errors.artifactPathName?.message && errors.artifactPathName?.message} {preview && ( <> Image Preview Name: {file?.name} File size: {bytesToMB(file?.size)} Last update:{" "} {formatTimestampInGulfTimezone(file?.lastModified)} handleRemove()} _hover={{ bg: "gray.100" }} transition={"all 0-5s"} cursor={"pointer"} position={"absolute"} right={0} p={1} bottom={0} rounded={"lg"} boxSize={7} as={IoMdRemoveCircleOutline} /> )} {found && !preview && ( Image Preview )} ); }; export default IOArtifactsAdd;