temp/src/Components/DummyDuplicatesPreview.jsx

470 lines
15 KiB
React
Raw Normal View History

2024-08-01 16:02:22 +05:30
import * as React from "react";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import AppBar from "@mui/material/AppBar";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import CloseIcon from "@mui/icons-material/Close";
import Slide from "@mui/material/Slide";
import ZoomInIcon from "@mui/icons-material/ZoomIn";
import ZoomOutIcon from "@mui/icons-material/ZoomOut";
import RotateRightIcon from "@mui/icons-material/RotateRight";
import { Box } from "@mui/material";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import ArrowForwardIcon from "@mui/icons-material/ArrowForward";
import TextInputField from "./TextInputField";
2024-08-02 11:17:07 +05:30
import { notification, Space } from "antd";
import { toast, ToastContainer } from "react-toastify";
2024-08-01 16:02:22 +05:30
import { useEffect, useState } from "react";
import LoadingContainer from "./LoadingContainer";
const Transition = React.forwardRef(function Transition(props, ref) {
return <Slide direction="up" ref={ref} {...props} />;
});
export default function DummyDuplicatesPreview({
type,
barcode,
dummyDuplicatesData,
setIsDialogOpen,
}) {
const [open, setOpen] = React.useState(false);
2024-08-02 11:17:07 +05:30
const [api, contextHolder] = notification.useNotification();
2024-08-01 16:02:22 +05:30
const [scaleWidthValue, setScaleWidthValue] = React.useState(80);
const [rotateValue, setRotateValue] = React.useState(0);
const [partAResults, setPartAResults] = React.useState([]);
const [partCResults, setPartCResults] = React.useState([]);
const [isLoading, setIsLoading] = React.useState(false);
const [partAImageIndex, setPartAImageIndex] = React.useState(0);
const [partCImageIndex, setPartCImageIndex] = React.useState(0);
const [inputBarcode, setInputBarcode] = useState(null);
const [inputSerialNo, setInputSerialNo] = useState(null);
2024-08-02 11:17:07 +05:30
const [inputSubjectCode, setInputSubjectCode] = useState(null);
const [inputRegisterNumber, setInputRegisterNumber] = useState(null);
const [partAImageS3Path, setPartAImageS3Path] = useState(null);
const [partCImageS3Path, setPartCImageS3Path] = useState(null);
const [loadingText, setLoadingText] = useState(null);
const openNotification = (pauseOnHover) => () => {
api.open({
message: "Notification Title",
description: "Record Updated Successfully ....",
showProgress: true,
pauseOnHover,
});
};
2024-08-01 16:02:22 +05:30
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
setIsDialogOpen(false);
2024-08-02 11:17:07 +05:30
setTimeout(() => {
document.body.style.overflow = "auto";
}, 100);
2024-08-01 16:02:22 +05:30
};
React.useEffect(() => {
handleClickOpen();
}, []);
2024-08-02 11:17:07 +05:30
useEffect(() => {
if (partAResults.length > 0 && partAImageIndex <= partAResults.length) {
setPartAImageS3Path(partAResults[partAImageIndex]?.s3_path);
}
}, [partAResults]);
2024-08-01 16:02:22 +05:30
useEffect(() => {
console.log("fetchDuplicateBarcodeInfo .........");
const fetchDuplicateBarcodeInfo = async () => {
setIsLoading(true);
2024-08-02 11:17:07 +05:30
console.log("fetching barcode info 12.....");
setLoadingText(`Fetching Barcode Info : ${barcode}`);
2024-08-01 16:02:22 +05:30
try {
const response = await fetch(
`${
import.meta.env.VITE_REACT_APP_BACKEND_URL
}/fetchDummyDuplicateBarcodeInfo`,
{
method: "POST",
body: JSON.stringify({
type,
barcode,
}),
headers: {
"Content-Type": "application/json",
},
}
);
const responseData = await response.json();
setIsLoading(false);
2024-08-02 11:17:07 +05:30
setLoadingText(null);
2024-08-01 16:02:22 +05:30
console.log("Barcode info Response Data ==== ", responseData);
if (responseData?.status == "success") {
console.log("success");
setIsLoading(false);
setPartAResults(responseData?.part_a_results);
setPartCResults(responseData?.part_c_results);
}
} catch (error) {
setIsLoading(false);
2024-08-02 11:17:07 +05:30
setLoadingText(null);
2024-08-01 16:02:22 +05:30
throw new Error(error);
}
};
fetchDuplicateBarcodeInfo();
}, []);
const ZoomInImage = () => {
console.log("Zooming In Image ....");
const elements = document.getElementsByClassName("scanned-img");
for (var ele of elements) {
console.log("Ele is : ", ele);
const newScaleWidthValue = scaleWidthValue + 10;
setScaleWidthValue(newScaleWidthValue);
// ele.style.transform = `scale(${newScaleValue})`;
ele.style.width = `${newScaleWidthValue}%`;
}
};
const ZoomOutImage = () => {
console.log("Zooming Out Image ....");
const elements = document.getElementsByClassName("scanned-img");
for (var ele of elements) {
console.log("Ele is : ", ele);
const newScaleWidthValue = scaleWidthValue - 10;
setScaleWidthValue(newScaleWidthValue);
// ele.style.transform = `scale(${newScaleValue})`;
ele.style.width = `${newScaleWidthValue}%`;
}
};
const RotateImageLeft = () => {
const elements = document.getElementsByClassName("scanned-img");
for (var ele of elements) {
console.log("Ele is : ", ele);
const newRotateValue = rotateValue - 90;
setRotateValue(newRotateValue);
ele.style.transform = `rotate(${newRotateValue}deg)`;
}
};
const RotateImageRight = () => {
const elements = document.getElementsByClassName("scanned-img");
for (var ele of elements) {
console.log("Ele is : ", ele);
const newRotateValue = rotateValue + 90;
setRotateValue(newRotateValue);
ele.style.transform = `rotate(${newRotateValue}deg)`;
}
};
2024-08-02 11:17:07 +05:30
useEffect(() => {
if (partAResults.length > 0 && partAImageIndex <= partAResults.length) {
setLoadingText(null);
setIsLoading(true);
setInputBarcode(null);
setInputRegisterNumber(null);
setInputSubjectCode(null);
setPartAImageS3Path(null);
setTimeout(() => {
setInputBarcode(partAResults[partAImageIndex]?.barcode);
setInputRegisterNumber(partAResults[partAImageIndex]?.register_number);
setInputSubjectCode(partAResults[partAImageIndex]?.subject_code);
setPartAImageS3Path(partAResults[partAImageIndex]?.s3_path);
setIsLoading(false);
}, 500);
}
}, [dummyDuplicatesData, partAImageIndex]);
useEffect(()=>{
console.log("part c image index changing ....",partCImageIndex)
if(partCResults.length > 0 && partCImageIndex <= partCResults.length){
console.log("into if ..")
setPartCImageS3Path(null)
setTimeout(()=>{
setPartCImageS3Path(partCResults[partCImageIndex]?.s3_path)
},5000)
}
},[partCImageIndex])
useEffect(() => {
if (partAImageS3Path) {
const fetchPartAImageData = async () => {
setIsLoading(true);
console.log("fetching Image info .....");
try {
const response = await fetch(
`${
import.meta.env.VITE_REACT_APP_BACKEND_URL
}/fetchPartAS3PathInfo`,
{
method: "POST",
body: JSON.stringify({
partAImageS3Path,
}),
headers: {
"Content-Type": "application/json",
},
}
);
const responseData = await response.json();
setIsLoading(false);
if (responseData?.status === "success") {
const imageInfo = responseData?.image_info;
console.log("Image info ====== ", imageInfo);
if (imageInfo && imageInfo.length > 0) {
setInputBarcode(imageInfo[0]?.barcode);
setInputRegisterNumber(imageInfo[0]?.register_number);
setInputSubjectCode(imageInfo[0]?.subject_code);
}
}
} catch (error) {
setIsLoading(false);
console.error("Error fetching barcode info:", error);
} finally {
setIsLoading(false);
}
};
fetchPartAImageData();
}
}, [partAImageS3Path]);
const updateDuplicateRecordData = async () => {
const regex = /^\d+(_\d+)?$/;
console.log("Regex ===== ", regex);
if (!regex.test(inputBarcode)) {
alert("Please enter valid Barcode");
return;
}
try {
setIsLoading(true);
const response = await fetch(
`${
import.meta.env.VITE_REACT_APP_BACKEND_URL
}/updateDuplicateRecordData`,
{
method: "POST",
body: JSON.stringify({
DuplicatePartAbarcode: barcode,
inputBarcode,
inputRegisterNumber,
inputSerialNo,
inputSubjectCode,
partAS3Path: partAResults[partAImageIndex]?.s3_path,
partCS3Path: partCResults[partCImageIndex]?.s3_path,
}),
headers: {
"Content-Type": "application/json",
},
}
);
const responseData = await response.json();
setIsLoading(false);
console.log("Response Data ==== ", responseData);
if (responseData?.status == "success") {
console.log("Updated successfully ....");
if (responseData?.duplicate_record) {
alert(`Already a record with barcode is present : ${inputBarcode}`);
} else {
console.log("showing notification ...");
toast.success("Record updated Successfully ..");
}
}
} catch (error) {
setIsLoading(false);
throw new Error(error);
}
};
2024-08-01 16:02:22 +05:30
return (
<React.Fragment>
<Dialog
fullScreen
open={open}
onClose={handleClose}
TransitionComponent={Transition}
style={{
zIndex: "100",
}}
>
<AppBar sx={{ position: "relative" }}>
2024-08-02 11:17:07 +05:30
{contextHolder}
<ToastContainer />
2024-08-01 16:02:22 +05:30
<Toolbar>
<IconButton
edge="start"
color="inherit"
onClick={handleClose}
aria-label="close"
>
<CloseIcon />
</IconButton>
<IconButton
color="inherit"
onClick={ZoomInImage}
aria-label="zoom in"
>
<ZoomInIcon />
</IconButton>
<IconButton
color="inherit"
onClick={ZoomOutImage}
aria-label="zoom out"
>
<ZoomOutIcon />
</IconButton>
<IconButton
color="inherit"
onClick={RotateImageLeft}
aria-label="rotate"
>
<RotateRightIcon />
</IconButton>
</Toolbar>
</AppBar>
<div
className="overflow-auto"
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100%",
overflow: "auto",
}}
>
{/* <img
className="scanned-img"
style={{ marginTop: "60%" }}
width={`${scaleWidthValue}%`}
// src={`https://docs.exampaper.vidh.ai/${imagePath}`}
alt="S3 Image"
/> */}
<Box className="d-flex flex-row">
<Box className="rounded bg-white d-flex flex-column align-items-center">
<Box>
2024-08-02 11:17:07 +05:30
{partAResults.length > 0 && (
<>
<Box className="text-center">
{partAImageIndex + 1 + "/" + partAResults.length}
</Box>
<Button
className="bg-primary text-white m-3"
onClick={() => {
if (partAImageIndex !== 0) {
setPartAImageIndex((prev) => prev - 1);
}
}}
>
<ArrowBackIcon />
</Button>
<Button
className="bg-primary text-white m-3"
onClick={() => {
console.log("partAimage Index === ", partAImageIndex);
if (partAImageIndex < partAResults.length - 1) {
setPartAImageIndex((prev) => prev + 1);
}
}}
>
<ArrowForwardIcon />
</Button>
</>
)}
2024-08-01 16:02:22 +05:30
</Box>
{partAResults.length > 0 && (
<img
2024-08-02 11:17:07 +05:30
alt="Part-A Image"
src={`https://docs.exampaper.vidh.ai/${partAImageS3Path}`}
2024-08-01 16:02:22 +05:30
width={"900px"}
/>
)}
</Box>
<Box className="rounded bg-white d-flex flex-column align-items-center">
<Box>
2024-08-02 11:17:07 +05:30
{partCResults.length > 0 && (
<>
<Box className="text-center">
{partCImageIndex + 1 + "/" + partCResults.length}
</Box>
<Button
className="bg-primary text-white m-3"
onClick={() => {
if (partCImageIndex !== 0) {
setPartCImageIndex((prev) => prev - 1);
}
}}
>
<ArrowBackIcon />
</Button>
<Button
className="bg-primary text-white m-3"
onClick={() => {
console.log("partCimage Index === ", partCImageIndex);
if (partCImageIndex < partCResults.length - 1) {
setPartCImageIndex((prev) => prev + 1);
}
}}
>
<ArrowForwardIcon />
</Button>
</>
)}
2024-08-01 16:02:22 +05:30
</Box>
{partCResults.length > 0 && (
<img
2024-08-02 11:17:07 +05:30
alt="Part-C Image"
2024-08-01 16:02:22 +05:30
src={`https://docs.exampaper.vidh.ai/${partCResults[partCImageIndex]?.s3_path}`}
width={"900px"}
/>
)}
</Box>
</Box>
</div>
2024-08-02 11:17:07 +05:30
{partAResults.length > 0 && (
<Box className="d-flex justify-content-center align-items-center gap-3 m-3">
<TextInputField
value={inputBarcode}
setValue={setInputBarcode}
placeholder={"Barcode"}
/>
<TextInputField
value={inputSerialNo}
setValue={setInputSerialNo}
placeholder={"Serial No"}
/>
<TextInputField
value={inputSubjectCode}
setValue={setInputSubjectCode}
placeholder={"Subject Code"}
/>
<TextInputField
value={inputRegisterNumber}
setValue={setInputRegisterNumber}
placeholder={"Register Number"}
/>
<Button
className="bg-primary rounded text-white px-5"
style={{ height: "50px" }}
onClick={updateDuplicateRecordData}
>
Update
</Button>
</Box>
)}
2024-08-01 16:02:22 +05:30
</Dialog>
2024-08-02 11:17:07 +05:30
{isLoading && <LoadingContainer loadingText={loadingText} />}
2024-08-01 16:02:22 +05:30
</React.Fragment>
);
}