temp/src/Components/QueryCardEditor.jsx

377 lines
12 KiB
React
Raw Normal View History

2024-07-06 13:08:43 +05:30
import { useState, useEffect, useRef } from "react";
2024-07-03 07:46:03 +05:30
import { useSelector } from "react-redux";
2024-06-25 14:41:11 +05:30
import { useSearchParams } from "react-router-dom";
import { Box, Button } from "@mui/material";
2024-07-03 07:46:03 +05:30
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
2024-06-25 14:41:11 +05:30
import AntdesignLayout from "./AntdesignLayout";
import TextInputField from "./TextInputField";
import { toast } from "react-toastify";
import LoadingContainer from "./LoadingContainer";
import SimpleDialog from "./SimpleDialog";
import RotateLeftIcon from "@mui/icons-material/RotateLeft";
import RotateRightIcon from "@mui/icons-material/RotateRight";
2024-07-03 07:46:03 +05:30
import { updatePartCErrorData, updateSystemNo } from "../redux/actions/actions";
import { updatePartCErrorList } from "../redux/actions/actions";
import { DiscFullTwoTone } from "@mui/icons-material";
2025-01-11 14:07:29 +05:30
import { useParams } from "react-router-dom";
2024-06-25 14:41:11 +05:30
const QueryCardEditor = () => {
2025-01-11 14:07:29 +05:30
const {year} = useParams()
2024-06-25 14:41:11 +05:30
const [searchParams, setSearchParams] = useSearchParams();
const [barcode, setBarcode] = useState();
const [qrcode, setQrcode] = useState();
const [marks, setMarks] = useState(null);
const [subjectCode, setSubjectCode] = useState(null);
const [imageName, setImageName] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [recordData, setRecordData] = useState([]);
const [s3Path, setS3Path] = useState(null);
const [showDialog, setShowDialog] = useState(false);
const [rotationResults, setRotationResults] = useState([]);
2024-07-03 07:46:03 +05:30
const [rotateAngle, setRotateAngle] = useState(0);
const evErrorsList = useSelector((state) => state?.partCErrorList);
2025-01-11 14:07:29 +05:30
const table = year === "april2024" ? "ocr_scanned_part_c_v1" : year === "november2024" ? "ocr_scanned_part_c_v2" : "ocr_scanned_part_c_v1"
const imageDomain = (year === "april2024" ? "https://docs.exampaper.vidh.ai" : (year === "november2024" ? 'https://images.exampaper.usln.in' : 'https://docs.exampaper.vidh.ai'))
2024-06-25 14:41:11 +05:30
const image_name = searchParams.get("image_name");
2024-07-03 07:46:03 +05:30
const paramsError = searchParams.get("error");
const paramsErrorReason = searchParams.get("error_reason");
const paramsSysNo = searchParams.get("sysNo");
const paramsDegreeType = searchParams.get("degreeType");
const [items, setItems] = useState([]);
2024-07-06 13:08:43 +05:30
const [evErrorsData, setEvErrorsData] = useState([]);
const barcodeInputRef = useRef(null);
const qrInputRef = useRef(null);
const marksInputRef = useRef(null);
const subjectCodeInputRef = useRef(null);
2024-07-03 07:46:03 +05:30
const navigate = useNavigate();
const dispatch = useDispatch();
console.log("Ev errors list ==== ", evErrorsList);
2024-06-25 14:41:11 +05:30
console.log("table is : ", table);
2024-07-03 07:46:03 +05:30
// const evErrorsData = useSelector((state) => state?.partCErrorData);
// console.log("evErrorData: ", evErrorsData);
2024-06-25 14:41:11 +05:30
const fetchPrimaryKeyData = async () => {
try {
const payload = {
image_name,
table,
};
const response = await fetch(
`${import.meta.env.VITE_REACT_APP_BACKEND_URL}/fetchPrimaryKeyData`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
return await response.json();
} catch (error) {
throw new Error(error);
return null;
}
};
2024-07-03 07:46:03 +05:30
useEffect(() => {
const marksLocalData = localStorage.getItem("marks_manual_data");
2024-07-06 13:08:43 +05:30
console.log("Marks local data 123 ========= ", marksLocalData);
2024-07-03 07:46:03 +05:30
if (marksLocalData) {
2024-07-06 13:08:43 +05:30
console.log("Into if and updating .......");
console.log("marks local data ==== ", marksLocalData);
2024-07-03 07:46:03 +05:30
setEvErrorsData(JSON.parse(marksLocalData));
}
}, []);
2024-07-06 13:08:43 +05:30
useEffect(() => {
dispatch(updateSystemNo(paramsSysNo));
}, [paramsSysNo]);
2024-07-03 07:46:03 +05:30
2024-07-06 13:08:43 +05:30
useEffect(() => {
console.log("Ev error data =============== ", evErrorsData);
}, [evErrorsData]);
2024-07-03 07:46:03 +05:30
2024-06-25 14:41:11 +05:30
useEffect(() => {
const fetchData = async () => {
if (table && image_name) {
setIsLoading(true);
const response = await fetchPrimaryKeyData();
setIsLoading(false);
console.log("Response is : ", response);
if (response?.status === "success") {
console.log("=========== Success ============");
const data = response?.data;
if (data.length > 0) {
setRecordData(data[0]);
}
}
}
};
fetchData();
2025-01-11 14:07:29 +05:30
marksInputRef.current.focus()
2024-06-25 14:41:11 +05:30
}, []);
useEffect(() => {
console.log("=========== Use effect triggered ===========");
setBarcode(recordData?.barcode);
setMarks(recordData?.marks);
setQrcode(recordData?.qrcode);
setImageName(recordData?.image_name);
setSubjectCode(recordData?.subject_code);
setS3Path(recordData?.s3_path);
}, [recordData]);
const updateRecord = async () => {
2024-07-06 13:08:43 +05:30
if (!marks) {
return;
2024-07-03 07:46:03 +05:30
}
2024-06-25 14:41:11 +05:30
setIsLoading(true);
try {
const payload = {
qrcode,
barcode,
table,
s3Path,
subjectCode,
marks,
imageName,
2024-07-06 13:08:43 +05:30
rotateAngle,
2025-01-11 14:07:29 +05:30
year
2024-06-25 14:41:11 +05:30
};
const response = await fetch(
`${import.meta.env.VITE_REACT_APP_BACKEND_URL}/editPartCdata`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
const responseData = await response.json();
setIsLoading(false);
console.log("response data ========= ", responseData);
if (responseData?.status === "success") {
2024-07-11 12:48:01 +05:30
// toast.success("Record Updated Successfully ...");
2024-07-03 07:46:03 +05:30
var currentIndex = null;
2024-07-06 13:08:43 +05:30
console.log(
"Ev errors data before filter ============= ",
evErrorsData
);
2024-07-03 07:46:03 +05:30
var newRecords = evErrorsData.filter((data, index) => {
if (data?.image_name === imageName) {
currentIndex = index;
return false;
}
return true;
});
2024-07-06 13:08:43 +05:30
if (!currentIndex) {
currentIndex = 0;
2024-07-03 07:46:03 +05:30
}
console.log("new records ======1 ", newRecords);
console.log("Current Index ===== ", currentIndex);
dispatch(updatePartCErrorData(newRecords));
if (newRecords.length > 0) {
console.log("Has to navigte 12 .....");
localStorage.setItem("marks_manual_data", JSON.stringify(newRecords));
2025-01-11 14:07:29 +05:30
const newUrl = `/sections/${year}/sqlPlayground/edit?image_name=${newRecords[currentIndex]?.image_name}&error=${paramsError}&error_reason=${paramsErrorReason}&degreeType=${paramsDegreeType}&sysNo=${paramsSysNo}`;
2024-07-03 07:46:03 +05:30
console.log("new url ==== ", newUrl);
window.location.href = newUrl;
} else {
navigate("/");
}
2024-06-25 14:41:11 +05:30
}
} catch (error) {
setIsLoading(false);
throw new Error(error);
}
};
2024-07-03 07:46:03 +05:30
useEffect(() => {
console.log("Rotate angle changed to ========== ", rotateAngle);
const imageContainer = document.getElementById("img-container");
console.log("Imge container ===== ", imageContainer);
if (imageContainer) {
imageContainer.style.transform = `rotate(${rotateAngle}deg)`;
}
}, [rotateAngle]);
2024-06-25 14:41:11 +05:30
const initateRotation = async () => {
console.log("Rotating initiated ...");
setIsLoading(true);
const payload = {
imageName,
};
try {
const response = await fetch(
`${import.meta.env.VITE_REACT_APP_BACKEND_URL}/initateRotation`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
setIsLoading(false);
const responseData = await response.json();
console.log("Response data ======= ", responseData);
if (responseData?.status === "success") {
setShowDialog(true);
setRotationResults(responseData?.result);
}
} catch (error) {
setIsLoading(false);
throw new Error(error);
}
};
2024-07-06 13:08:43 +05:30
const skipPage = () => {
try {
if (evErrorsData) {
var currentIndex = null;
2024-07-03 07:46:03 +05:30
var newRecords = evErrorsData.filter((data, index) => {
if (data?.image_name === imageName) {
currentIndex = index;
return false;
}
return true;
});
2024-07-06 13:08:43 +05:30
if (!currentIndex) {
currentIndex = 0;
2024-07-03 07:46:03 +05:30
}
console.log("Current Index ===== ", currentIndex);
2024-07-06 13:08:43 +05:30
const newIndex = currentIndex + 1;
console.log("new index ===== ", newIndex);
2024-07-03 07:46:03 +05:30
if (newRecords.length > 0) {
console.log("Has to navigte 12 .....");
2025-01-11 14:07:29 +05:30
const newUrl = `/sections/${year}/sqlPlayground/edit?image_name=${evErrorsData[newIndex]?.image_name}&table=ocr_scanned_part_c_v1&error=${paramsError}&error_reason=${paramsErrorReason}&degreeType=${paramsDegreeType}&sysNo=${paramsSysNo}`;
2024-07-03 07:46:03 +05:30
console.log("new url ==== ", newUrl);
window.location.href = newUrl;
2024-07-06 13:08:43 +05:30
}
2024-07-03 07:46:03 +05:30
}
2024-07-06 13:08:43 +05:30
} catch (error) {
throw new Error(error);
2024-07-03 07:46:03 +05:30
}
2024-07-06 13:08:43 +05:30
};
const handleKeyDown = (e) => {
try {
console.log("Handle key down clicked ...", e);
console.log("event target ..... ", e.target);
console.log("barcode targed .....", barcodeInputRef.current);
if (e.key === "Enter") {
if (e.target === barcodeInputRef.current) {
qrInputRef.current.focus();
} else if (e.target === qrInputRef.current) {
marksInputRef.current.focus();
} else if (e.target === marksInputRef.current) {
subjectCodeInputRef.current.focus();
} else if (e.target === subjectCodeInputRef.current) {
updateRecord();
}
}
} catch (error) {}
};
2024-07-03 07:46:03 +05:30
2024-06-25 14:41:11 +05:30
return (
<AntdesignLayout>
2025-01-11 14:07:29 +05:30
<Box className="d-flex justify-content-between align-items-start">
<Box className="w-75">
<Box className="px-5" id="img-container">
<img
src={`${imageDomain}/${recordData?.s3_path}`}
width="100%"
height="auto"
/>
</Box>
</Box>
2024-06-25 14:41:11 +05:30
<Box className="d-flex flex-column gap-3 w-25">
2025-01-11 14:07:29 +05:30
{/* {imageName && <h5 className="text-left">ID : {imageName}</h5>}
2024-07-06 13:08:43 +05:30
{paramsError && (
<h5 className="text-left">Error Code : {paramsError}</h5>
)}
2024-07-03 18:42:11 +05:30
{paramsDegreeType ? (
2024-07-06 13:08:43 +05:30
paramsDegreeType === 0 ? (
<h5 className="text-left">Degree Type : UG</h5>
) : (
<h5 className="text-left">Degree Type : PG</h5>
)
2025-01-11 14:07:29 +05:30
) : null} */}
<h5 style={{textAlign:"left",marginTop:'10px',marginBottom:'0px'}}><strong>COVER CODE : </strong>{recordData?.new_cover_barcode}</h5>
2024-06-25 14:41:11 +05:30
<TextInputField
2024-07-06 13:08:43 +05:30
placeholder="Barcode"
2024-06-25 14:41:11 +05:30
value={barcode}
setValue={setBarcode}
2024-07-06 13:08:43 +05:30
onKeyDown={handleKeyDown}
ref={barcodeInputRef}
2024-06-25 14:41:11 +05:30
/>
<TextInputField
2024-07-06 13:08:43 +05:30
placeholder="QR Code"
2024-06-25 14:41:11 +05:30
value={qrcode}
setValue={setQrcode}
2024-07-06 13:08:43 +05:30
onKeyDown={handleKeyDown}
ref={qrInputRef}
2024-06-25 14:41:11 +05:30
/>
<TextInputField
2024-07-06 13:08:43 +05:30
placeholder="Marks"
2024-06-25 14:41:11 +05:30
value={marks}
setValue={setMarks}
2024-07-06 13:08:43 +05:30
onKeyDown={handleKeyDown}
ref={marksInputRef}
2024-06-25 14:41:11 +05:30
/>
<TextInputField
2024-07-06 13:08:43 +05:30
placeholder="Subject Code"
2024-06-25 14:41:11 +05:30
value={subjectCode}
setValue={setSubjectCode}
2024-07-06 13:08:43 +05:30
onKeyDown={handleKeyDown}
ref={subjectCodeInputRef}
2024-06-25 14:41:11 +05:30
/>
<Button
className="bg-primary text-white rounded p-3"
onClick={() => {
updateRecord();
}}
>
Update
</Button>
2024-07-03 07:46:03 +05:30
<Button
className="bg-primary text-white rounded p-3"
onClick={() => {
2024-07-06 13:08:43 +05:30
skipPage();
2024-07-03 07:46:03 +05:30
}}
>
Skip
</Button>
<Box className="d-flex justify-content-between">
2024-06-25 14:41:11 +05:30
<Button
className="bg-primary text-white rounded p-3"
2024-07-03 07:46:03 +05:30
onClick={() => setRotateAngle((prev) => prev - 90)}
2024-06-25 14:41:11 +05:30
>
Rotate <RotateLeftIcon />
</Button>
<Button
className="bg-primary text-white rounded p-3"
2024-07-03 07:46:03 +05:30
onClick={() => setRotateAngle((prev) => prev + 90)}
2024-06-25 14:41:11 +05:30
>
Rotate <RotateRightIcon />
</Button>
2024-07-03 07:46:03 +05:30
</Box>
2024-06-25 14:41:11 +05:30
</Box>
</Box>
{isLoading && <LoadingContainer />}
{showDialog && (
<SimpleDialog
type="rotation_results"
rotationResults={rotationResults}
setShowDialog={setShowDialog}
/>
)}
</AntdesignLayout>
);
};
export default QueryCardEditor;