From bd4dd9597cdbdcfe7a4b03a0f3823b1e64bb288b Mon Sep 17 00:00:00 2001 From: Pranshu Aggarwal <70687348+Pranshu1902@users.noreply.github.com> Date: Wed, 13 Dec 2023 19:36:28 +0530 Subject: [PATCH] Show error for invalid records of import external results page (#6832) * show error for invalid records * refactor filter logic * add warning icons --- .../ExternalResult/ExternalResultUpload.tsx | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Components/ExternalResult/ExternalResultUpload.tsx b/src/Components/ExternalResult/ExternalResultUpload.tsx index 5a9262b2990..a8f9e339254 100644 --- a/src/Components/ExternalResult/ExternalResultUpload.tsx +++ b/src/Components/ExternalResult/ExternalResultUpload.tsx @@ -1,6 +1,6 @@ import _ from "lodash-es"; import { navigate } from "raviger"; -import { lazy, useState } from "react"; +import { lazy, useEffect, useState } from "react"; import CSVReader from "react-csv-reader"; import useConfig from "../../Common/hooks/useConfig"; import * as Notification from "../../Utils/Notifications.js"; @@ -11,6 +11,7 @@ import useAppHistory from "../../Common/hooks/useAppHistory"; import request from "../../Utils/request/request"; import routes from "../../Redux/api"; import { IExternalResult } from "./models"; +import CareIcon from "../../CAREUI/icons/CareIcon"; export default function ExternalResultUpload() { const { sample_format_external_result_import } = useConfig(); @@ -18,12 +19,31 @@ export default function ExternalResultUpload() { const [loading, setLoading] = useState(false); const [csvData, setCsvData] = useState(new Array()); const [errors, setErrors] = useState([]); + const [validationErrorCount, setValidationErrorCount] = useState(0); + const [user, setUser] = useState({}); const handleForce = (data: any) => { setCsvData(data); + setValidationErrorCount( + data.filter( + (result: IExternalResult) => + result.district !== user.district_object.name + ).length + ); }; const { t } = useTranslation(); const { goBack } = useAppHistory(); + const fetchUser = async () => { + const { data: userData } = await request(routes.currentUser, { + pathParams: {}, + }); + setUser(userData); + }; + + useEffect(() => { + fetchUser(); + }, []); + const papaparseOptions = { header: true, dynamicTyping: true, @@ -44,13 +64,21 @@ export default function ExternalResultUpload() { try { const { res, data } = await request(routes.externalResultUploadCsv, { body: { - sample_tests: csvData, + sample_tests: validationErrorCount + ? csvData.filter( + (data: IExternalResult) => + data.district === user.district_object.name + ) + : csvData, }, }); if (res && res.status === 202) { setLoading(false); navigate("/external_results"); + Notification.Success({ + msg: "External Results imported successfully", + }); } else { if (data) { setErrors(data.map((err: any) => Object.entries(err))); @@ -59,6 +87,9 @@ export default function ExternalResultUpload() { } } catch (error) { console.error("An error occurred:", error); + Notification.Error({ + msg: "Something went wrong: " + error, + }); setLoading(false); } } else { @@ -121,6 +152,9 @@ export default function ExternalResultUpload() { + {csvData.length > 0 && ( +

Total: {csvData.length}

+ )}
{csvData.map((data: any, index: number) => { return ( @@ -140,6 +174,14 @@ export default function ExternalResultUpload() { }) : null}
+
+ {data.district !== user.district_object.name && ( +

+ Different + districts +

+ )} +
); })} @@ -149,8 +191,14 @@ export default function ExternalResultUpload() { goBack()} />