diff --git a/cope2n-fe/src/pages/inference/index.tsx b/cope2n-fe/src/pages/inference/index.tsx index 0cd53cb..2433be2 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 = { @@ -355,6 +356,7 @@ const InferencePage = () => { 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(() => { @@ -370,30 +372,7 @@ const InferencePage = () => { 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..6302b5c 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', @@ -415,6 +417,7 @@ const ReviewPage = () => { 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(() => { @@ -430,30 +433,7 @@ const ReviewPage = () => { 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..ee24573 --- /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