diff --git a/cope2n-fe/src/pages/inference/index.tsx b/cope2n-fe/src/pages/inference/index.tsx index 0cd53cb..b4c4d6c 100644 --- a/cope2n-fe/src/pages/inference/index.tsx +++ b/cope2n-fe/src/pages/inference/index.tsx @@ -23,6 +23,7 @@ const ENABLE_REVIEW = true; // Import the styles import '@react-pdf-viewer/core/lib/styles/index.css'; +import { normalizeData } from 'utils/field-value-process'; const siderStyle: React.CSSProperties = { @@ -329,7 +330,7 @@ const InferencePage = () => { const updateRevisedData = async (newRevisedData: any) => { const requestID = currentRequest.RequestID; const token = localStorage.getItem('sbt-token') || ''; - await fetch(`${baseURL}/ctel/request/${requestID}/`, { + const result = await fetch(`${baseURL}/ctel/request/${requestID}/`, { method: 'POST', headers: { "Authorization": `${JSON.parse(token)}`, @@ -340,8 +341,11 @@ const InferencePage = () => { }), }).catch((error) => { console.log(error); - message.error("Could not update revised data"); + throw error; }); + if (result.status != 200) { + throw new Error("Could not update revised data"); + } }; const handleSave = (row: DataType) => { @@ -352,9 +356,9 @@ const InferencePage = () => { ...item, ...row, }); - setDataSource(newData); const newRevisedData = {}; for (let i = 0; i < newData.length; i++) { + newData[i].revised = normalizeData(newData[i].key, newData[i].revised); newRevisedData[newData[i].key] = newData[i].revised; } updateRevisedData(newRevisedData).then(() => { @@ -363,37 +367,18 @@ const InferencePage = () => { ...currentRequest, ["Is Reviewed"]: true, }) - }) + }).then(() => { + setDataSource(newData); + }).catch((error) => { + message.error("Could not update revised data. Please check the format."); + }); }; const submitRevisedData = async () => { const newData = [...dataSource]; const newRevisedData = {}; for (let i = 0; i < newData.length; i++) { - if (newData[i].revised === "") { - newData[i].revised = null; - } - if (typeof(newData[i].revised) === "string") { - newData[i].revised = newData[i].revised.trim(); - } - if (newData[i].revised === "" || newData[i].revised === null || newData[i].revised === undefined) { - newData[i].revised = null; - } - if ((newData[i].key === "imei_number" || newData[i].key === "purchase_date") && typeof(newData[i].revised) === "string") { - // Convert to list - newData[i].revised = new Array(newData[i].revised.split(",")); - } - if (Array.isArray(newData[i].revised)) { - // Trim all empty strings - for (let j = 0; j < newData[i].revised.length; j++) { - if (typeof(newData[i].revised[j]) === "string") { - newData[i].revised[j] = newData[i].revised[j].trim(); - } - if (newData[i].revised[j] === "") { - newData[i].revised[j] = null; - } - } - } + newData[i].revised = normalizeData(newData[i].key, newData[i].revised); newRevisedData[newData[i].key] = newData[i].revised; } updateRevisedData(newRevisedData).then(() => { diff --git a/cope2n-fe/src/pages/reviews/index.tsx b/cope2n-fe/src/pages/reviews/index.tsx index 6f71652..5dd649c 100644 --- a/cope2n-fe/src/pages/reviews/index.tsx +++ b/cope2n-fe/src/pages/reviews/index.tsx @@ -21,6 +21,8 @@ import { Viewer } from '@react-pdf-viewer/core'; // Import the styles import '@react-pdf-viewer/core/lib/styles/index.css'; +import { normalizeData } from 'utils/field-value-process'; + const siderStyle: React.CSSProperties = { backgroundColor: '#fafafa', @@ -115,12 +117,6 @@ const EditableCell: React.FC = ({ @@ -387,9 +383,9 @@ const ReviewPage = () => { }; const updateRevisedData = async (newRevisedData: any) => { - const requestID = newRevisedData.request_id; + const requestID = currentRequest.RequestID; const token = localStorage.getItem('sbt-token') || ''; - await fetch(`${baseURL}/ctel/request/${requestID}/`, { + const result = await fetch(`${baseURL}/ctel/request/${requestID}/`, { method: 'POST', headers: { "Authorization": `${JSON.parse(token)}`, @@ -400,8 +396,11 @@ const ReviewPage = () => { }), }).catch((error) => { console.log(error); - message.error("Could not update revised data"); + throw error; }); + if (result.status != 200) { + throw new Error("Could not update revised data"); + } }; const handleSave = (row: DataType) => { @@ -412,9 +411,9 @@ const ReviewPage = () => { ...item, ...row, }); - setDataSource(newData); const newRevisedData = {}; for (let i = 0; i < newData.length; i++) { + newData[i].revised = normalizeData(newData[i].key, newData[i].revised); newRevisedData[newData[i].key] = newData[i].revised; } updateRevisedData(newRevisedData).then(() => { @@ -423,37 +422,18 @@ const ReviewPage = () => { ...currentRequest, ["Is Reviewed"]: true, }) - }) + }).then(() => { + setDataSource(newData); + }).catch((error) => { + message.error("Could not update revised data. Please check the format."); + }); }; const submitRevisedData = async () => { const newData = [...dataSource]; const newRevisedData = {}; for (let i = 0; i < newData.length; i++) { - if (newData[i].revised === "") { - newData[i].revised = null; - } - if (typeof(newData[i].revised) === "string") { - newData[i].revised = newData[i].revised.trim(); - } - if (newData[i].revised === "" || newData[i].revised === null || newData[i].revised === undefined) { - newData[i].revised = null; - } - if ((newData[i].key === "imei_number" || newData[i].key === "purchase_date") && typeof(newData[i].revised) === "string") { - // Convert to list - newData[i].revised = new Array(newData[i].revised.split(",")); - } - if (Array.isArray(newData[i].revised)) { - // Trim all empty strings - for (let j = 0; j < newData[i].revised.length; j++) { - if (typeof(newData[i].revised[j]) === "string") { - newData[i].revised[j] = newData[i].revised[j].trim(); - } - if (newData[i].revised[j] === "") { - newData[i].revised[j] = null; - } - } - } + newData[i].revised = normalizeData(newData[i].key, newData[i].revised); newRevisedData[newData[i].key] = newData[i].revised; } updateRevisedData(newRevisedData).then(() => { diff --git a/cope2n-fe/src/utils/field-value-process.ts b/cope2n-fe/src/utils/field-value-process.ts new file mode 100644 index 0000000..69ab9d5 --- /dev/null +++ b/cope2n-fe/src/utils/field-value-process.ts @@ -0,0 +1,32 @@ +const isAllSpaces = (text) => { + if (typeof (text) != "string") { + return false; + } + return text.trim().length === 0 || text === ""; +}; + +const normalizeData = (key, value) => { + if (value === "") { + value = null; + } + if (typeof (value) === "string") { + value = value.trim(); + } + if (value === "" || value === null || value === undefined) { + value = null; + } + if (isAllSpaces(value)) { + value = null; + } + if (["imei_number", "purchase_date"].includes(key) && typeof(value) === "string") { + value = value.split(","); + } + if (typeof (value) === "object" && value?.length > 0) { + for (let i = 0; i < value.length; i++) { + value[i] = normalizeData("text", value[i]); + } + } + return value; +} + +export { normalizeData }; \ No newline at end of file