From 41d0eff45f61dcd4a782f065b28a91b7e5529ee3 Mon Sep 17 00:00:00 2001 From: NyashaMuusha Date: Thu, 4 Jul 2024 11:05:59 +0200 Subject: [PATCH] refactored handle update configuration --- .../JeMPI_UI/src/pages/settings/Settings.tsx | 3 +- .../src/pages/settings/common/Common.tsx | 104 ++++++++---------- 2 files changed, 45 insertions(+), 62 deletions(-) diff --git a/JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx b/JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx index 14869cf7d..46a3fdd88 100644 --- a/JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx +++ b/JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx @@ -110,8 +110,7 @@ const Settings = () => { Setup properties for Golden record lists - + { - return str - .trim() - .replace(/\s+/g, '_') - .replace(/([a-z])([A-Z])/g, '$1_$2') - .toLowerCase() -} - const CommonSettings = () => { const [rows, setRows] = useState([]) const { configuration, setConfiguration } = useConfiguration() const [rowModesModel, setRowModesModel] = useState({}) useEffect(() => { - if(configuration && configuration.demographicFields){ - const rowData = configuration?.demographicFields.map((row: any, rowIndex: number) => ({ - id: rowIndex + 1, - ...row, - rowIndex - })) + if (configuration && configuration.demographicFields) { + const rowData = configuration?.demographicFields.map( + (row: any, rowIndex: number) => ({ + id: rowIndex + 1, + ...row, + rowIndex + }) + ) setRows(rowData) } - }, [configuration]) const handleEditClick = (id: GridRowId) => () => { @@ -50,26 +43,18 @@ const CommonSettings = () => { } const handleSaveClick = (id: GridRowId) => () => { - const updatedRow = rows.find((row: { id: GridRowId }) => row.id === id) - if (updatedRow) { - setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.View } }) } + setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.View } }) } const handleUpdateConfiguration = (updatedRow: any, rowIndex: number) => { - setConfiguration(previousConfiguration => { - if (!previousConfiguration) return previousConfiguration - const updatedConfiguration = getUpdatedConfiguration( - updatedRow, - rowIndex, - previousConfiguration - ) - localStorage.setItem( - 'configuration', - JSON.stringify(updatedConfiguration) - ) - setConfiguration(updatedConfiguration) - return updatedConfiguration - }) + if (!configuration) return + const updatedConfiguration = getUpdatedConfiguration( + updatedRow, + rowIndex, + configuration + ) + localStorage.setItem('configuration', JSON.stringify(updatedConfiguration)) + setConfiguration(updatedConfiguration) } const getUpdatedConfiguration = ( @@ -77,44 +62,41 @@ const CommonSettings = () => { rowIndex: number, currentConfiguration: Configuration ): Configuration => { - const fieldName = toSnakeCase(updatedRow.fieldName); + const fieldName = toSnakeCase(updatedRow.fieldName) if (!currentConfiguration.demographicFields) { - console.error('demographicFields is undefined'); - return currentConfiguration; + return currentConfiguration } - - const fieldToUpdate = currentConfiguration.demographicFields[rowIndex]; - + + const fieldToUpdate = currentConfiguration.demographicFields[rowIndex] + if (!fieldToUpdate) { - console.error(`No field found at row index ${rowIndex}`); - return currentConfiguration; + return currentConfiguration } - - fieldToUpdate.fieldName = fieldName; - + + fieldToUpdate.fieldName = fieldName + if (updatedRow?.indexGoldenRecord) { - fieldToUpdate.indexGoldenRecord = `@index(${updatedRow.indexGoldenRecord})`; + fieldToUpdate.indexGoldenRecord = `@index(${updatedRow.indexGoldenRecord})` } - + if (updatedRow?.m) { fieldToUpdate.linkMetaData = { ...fieldToUpdate.linkMetaData, m: Number(updatedRow.m) - } as LinkMetaData; + } as LinkMetaData } - + if (updatedRow?.u) { fieldToUpdate.linkMetaData = { ...fieldToUpdate.linkMetaData, u: Number(updatedRow.u) - } as LinkMetaData; + } as LinkMetaData } - - currentConfiguration.demographicFields[rowIndex] = fieldToUpdate; - - return currentConfiguration; + + currentConfiguration.demographicFields[rowIndex] = fieldToUpdate + + return currentConfiguration } - const handleCancelClick = (id: GridRowId) => () => { setRowModesModel(prevRowModesModel => { @@ -132,12 +114,14 @@ const CommonSettings = () => { } const processRowUpdate = (newRow: GridRowModel) => { - const { id, ...updatedRow } = newRow; - const updatedRows = rows.map((row: { id: any }) => (row.id === id ? ({ ...updatedRow, id } as RowData) : row)); - setRows(updatedRows); + const { id, ...updatedRow } = newRow + const updatedRows = rows.map((row: { id: any }) => + row.id === id ? ({ ...updatedRow, id } as RowData) : row + ) + setRows(updatedRows) handleUpdateConfiguration(updatedRow, updatedRow.rowIndex) - return { ...updatedRow, id } as RowData; - }; + return { ...updatedRow, id } as RowData + } const columns: GridColDef[] = [ {