From e232fffedd65ce4f238f7c3d09ca5d064fe3652c Mon Sep 17 00:00:00 2001 From: Arif Date: Tue, 3 Oct 2023 15:02:18 +0530 Subject: [PATCH] Batching fix for records less than 100 or more than 100 --- .../admin/src/api/StudentResetPasswordAPI.jsx | 15 ++- packages/admin/src/components/StudentCSV.jsx | 18 ++- .../admin/src/components/StudentListView.jsx | 125 +++++++++++------- 3 files changed, 104 insertions(+), 54 deletions(-) diff --git a/packages/admin/src/api/StudentResetPasswordAPI.jsx b/packages/admin/src/api/StudentResetPasswordAPI.jsx index a17907bf..55b8368f 100644 --- a/packages/admin/src/api/StudentResetPasswordAPI.jsx +++ b/packages/admin/src/api/StudentResetPasswordAPI.jsx @@ -1,7 +1,10 @@ import axios from "axios"; import { studentReset } from "../routes/links"; -const studentAPI = async (data) => { +const studentResetPasswordAPI = async (username, password) => { + console.log("INSIDE RESET PASS API"); + console.log(username, password); + const token = localStorage.getItem("token"); const headers = { @@ -12,8 +15,8 @@ const studentAPI = async (data) => { }; const jsonData = { - username: data.username, - newPassword: data.newPassword, + username: username, + newPassword: password, }; let result; @@ -26,8 +29,8 @@ const studentAPI = async (data) => { .then((res) => { console.log(res); console.log(res.data); - console.log(res.status); - if (res.status === 201) { + console.log(res.data.statusCode); + if (res.data.statusCode === 200) { result = true; } else { result = false; @@ -41,4 +44,4 @@ const studentAPI = async (data) => { return result; }; -export default studentAPI; +export default studentResetPasswordAPI; diff --git a/packages/admin/src/components/StudentCSV.jsx b/packages/admin/src/components/StudentCSV.jsx index c895dc81..02a96119 100644 --- a/packages/admin/src/components/StudentCSV.jsx +++ b/packages/admin/src/components/StudentCSV.jsx @@ -142,7 +142,23 @@ function CSVImportForm() { return; } - sendBatch(currentIndex, currentIndex + batchSize); + // Determine the number of full batches and the number of remaining records + const numFullBatches = Math.floor(csvData.length / batchSize); + const remainingRecords = csvData.length % batchSize; + + // Send full batches + for (let i = 0; i < numFullBatches; i++) { + const startIndex = i * batchSize; + const endIndex = startIndex + batchSize; + await sendBatch(startIndex, endIndex); + } + + // Send remaining records if there are any + if (remainingRecords > 0) { + const startIndex = numFullBatches * batchSize; + const endIndex = startIndex + remainingRecords; + await sendBatch(startIndex, endIndex); + } }; function downloadCSV(data, filename) { diff --git a/packages/admin/src/components/StudentListView.jsx b/packages/admin/src/components/StudentListView.jsx index 7c5a6900..a40d9c87 100644 --- a/packages/admin/src/components/StudentListView.jsx +++ b/packages/admin/src/components/StudentListView.jsx @@ -16,6 +16,8 @@ import axios from "axios"; import { Button } from "native-base"; import Modal from "react-modal"; import StudentResetPassword from "./StudentResetPassword"; +import studentResetPasswordAPI from "api/StudentResetPasswordAPI"; +import SyncLockIcon from "@mui/icons-material/SyncLock"; const customStyles = { content: { @@ -36,42 +38,51 @@ function StudentListView() { const navigate = useNavigate(); const gridRef = useRef(); const [rowData, setRowData] = useState([]); - const [isOpen, setIsOpen] = useState(false); - useEffect(() => { - Modal.setAppElement("#root"); // Set the app element for modal - }, []); - - const openModal = () => { - setIsOpen(true); - }; - - const closeModal = () => { - setIsOpen(false); + const openPrompt = async (data) => { + console.log(data); + let person = window.prompt( + `Enter a new password for user ${data.username}` + ); + if (person == null || person == "") { + alert("Please enter a valid password"); + } else { + const result = await studentResetPasswordAPI(data.username, person); + if (result == true) { + alert("Password reset Successful."); + window.location.reload(); + } else { + alert("Password reset failed"); + } + } }; const [columnDefs] = useState([ { width: 150, - cellRenderer: function () { + cellRenderer: function (params) { // Replace with your desired label - const combinedFunction = () => { - // openModal(); - alert("Work in progress"); + const combinedFunction = (rowData) => { + openPrompt(rowData); }; return (
- - combinedFunction(params.data)} + style={{ + background: "none", + border: "none", + color: "#6461D2", + textDecoration: "underline", + display: "flex", // Center align vertically + alignItems: "center", + marginTop: "10px", + }} > - Hello - + {" "} + Password +
); }, @@ -192,6 +203,10 @@ function StudentListView() { // document.body.removeChild(link); // }, [rowData]); + const cellClickedListener = useCallback((event) => { + console.log("cellClicked", event); + }, []); + const onBtnExportFields = useCallback(() => { // Get the visible (filtered) rows from the grid const filteredData = gridRef.current.api.getModel().rowsToDisplay; @@ -256,35 +271,51 @@ function StudentListView() { return (
- - +
+ + +
{" "}
);