Merge pull request #90 from SDSRV-IDP/fix/field-editor

Fix empty field handling
This commit is contained in:
Đỗ Xuân Tân 2024-03-14 18:49:40 +07:00 committed by GitHub Enterprise
commit cfdced2618
3 changed files with 60 additions and 63 deletions

View File

@ -23,6 +23,7 @@ const ENABLE_REVIEW = true;
// Import the styles // Import the styles
import '@react-pdf-viewer/core/lib/styles/index.css'; import '@react-pdf-viewer/core/lib/styles/index.css';
import { normalizeData } from 'utils/field-value-process';
const siderStyle: React.CSSProperties = { const siderStyle: React.CSSProperties = {
@ -329,7 +330,7 @@ const InferencePage = () => {
const updateRevisedData = async (newRevisedData: any) => { const updateRevisedData = async (newRevisedData: any) => {
const requestID = currentRequest.RequestID; const requestID = currentRequest.RequestID;
const token = localStorage.getItem('sbt-token') || ''; const token = localStorage.getItem('sbt-token') || '';
await fetch(`${baseURL}/ctel/request/${requestID}/`, { const result = await fetch(`${baseURL}/ctel/request/${requestID}/`, {
method: 'POST', method: 'POST',
headers: { headers: {
"Authorization": `${JSON.parse(token)}`, "Authorization": `${JSON.parse(token)}`,
@ -340,8 +341,11 @@ const InferencePage = () => {
}), }),
}).catch((error) => { }).catch((error) => {
console.log(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) => { const handleSave = (row: DataType) => {
@ -352,9 +356,9 @@ const InferencePage = () => {
...item, ...item,
...row, ...row,
}); });
setDataSource(newData);
const newRevisedData = {}; const newRevisedData = {};
for (let i = 0; i < newData.length; i++) { 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; newRevisedData[newData[i].key] = newData[i].revised;
} }
updateRevisedData(newRevisedData).then(() => { updateRevisedData(newRevisedData).then(() => {
@ -363,37 +367,18 @@ const InferencePage = () => {
...currentRequest, ...currentRequest,
["Is Reviewed"]: true, ["Is Reviewed"]: true,
}) })
}) }).then(() => {
setDataSource(newData);
}).catch((error) => {
message.error("Could not update revised data. Please check the format.");
});
}; };
const submitRevisedData = async () => { const submitRevisedData = async () => {
const newData = [...dataSource]; const newData = [...dataSource];
const newRevisedData = {}; const newRevisedData = {};
for (let i = 0; i < newData.length; i++) { for (let i = 0; i < newData.length; i++) {
if (newData[i].revised === "<empty>") { newData[i].revised = normalizeData(newData[i].key, 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] === "<empty>") {
newData[i].revised[j] = null;
}
}
}
newRevisedData[newData[i].key] = newData[i].revised; newRevisedData[newData[i].key] = newData[i].revised;
} }
updateRevisedData(newRevisedData).then(() => { updateRevisedData(newRevisedData).then(() => {

View File

@ -21,6 +21,8 @@ import { Viewer } from '@react-pdf-viewer/core';
// Import the styles // Import the styles
import '@react-pdf-viewer/core/lib/styles/index.css'; import '@react-pdf-viewer/core/lib/styles/index.css';
import { normalizeData } from 'utils/field-value-process';
const siderStyle: React.CSSProperties = { const siderStyle: React.CSSProperties = {
backgroundColor: '#fafafa', backgroundColor: '#fafafa',
@ -115,12 +117,6 @@ const EditableCell: React.FC<EditableCellProps> = ({
<Form.Item <Form.Item
style={{ margin: 0 }} style={{ margin: 0 }}
name={dataIndex} name={dataIndex}
rules={[
{
required: true,
message: `${title} is required.`,
},
]}
> >
<Input ref={inputRef} onPressEnter={save} onBlur={save} /> <Input ref={inputRef} onPressEnter={save} onBlur={save} />
</Form.Item> </Form.Item>
@ -387,9 +383,9 @@ const ReviewPage = () => {
}; };
const updateRevisedData = async (newRevisedData: any) => { const updateRevisedData = async (newRevisedData: any) => {
const requestID = newRevisedData.request_id; const requestID = currentRequest.RequestID;
const token = localStorage.getItem('sbt-token') || ''; const token = localStorage.getItem('sbt-token') || '';
await fetch(`${baseURL}/ctel/request/${requestID}/`, { const result = await fetch(`${baseURL}/ctel/request/${requestID}/`, {
method: 'POST', method: 'POST',
headers: { headers: {
"Authorization": `${JSON.parse(token)}`, "Authorization": `${JSON.parse(token)}`,
@ -400,8 +396,11 @@ const ReviewPage = () => {
}), }),
}).catch((error) => { }).catch((error) => {
console.log(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) => { const handleSave = (row: DataType) => {
@ -412,9 +411,9 @@ const ReviewPage = () => {
...item, ...item,
...row, ...row,
}); });
setDataSource(newData);
const newRevisedData = {}; const newRevisedData = {};
for (let i = 0; i < newData.length; i++) { 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; newRevisedData[newData[i].key] = newData[i].revised;
} }
updateRevisedData(newRevisedData).then(() => { updateRevisedData(newRevisedData).then(() => {
@ -423,37 +422,18 @@ const ReviewPage = () => {
...currentRequest, ...currentRequest,
["Is Reviewed"]: true, ["Is Reviewed"]: true,
}) })
}) }).then(() => {
setDataSource(newData);
}).catch((error) => {
message.error("Could not update revised data. Please check the format.");
});
}; };
const submitRevisedData = async () => { const submitRevisedData = async () => {
const newData = [...dataSource]; const newData = [...dataSource];
const newRevisedData = {}; const newRevisedData = {};
for (let i = 0; i < newData.length; i++) { for (let i = 0; i < newData.length; i++) {
if (newData[i].revised === "<empty>") { newData[i].revised = normalizeData(newData[i].key, 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] === "<empty>") {
newData[i].revised[j] = null;
}
}
}
newRevisedData[newData[i].key] = newData[i].revised; newRevisedData[newData[i].key] = newData[i].revised;
} }
updateRevisedData(newRevisedData).then(() => { updateRevisedData(newRevisedData).then(() => {

View File

@ -0,0 +1,32 @@
const isAllSpaces = (text) => {
if (typeof (text) != "string") {
return false;
}
return text.trim().length === 0 || text === "<empty>";
};
const normalizeData = (key, value) => {
if (value === "<empty>") {
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 };