From 044cb563a6488c16916ea7617d1f91404330b06f Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 7 Feb 2021 11:50:37 -0800 Subject: [PATCH] Fixed: Table column order resetting after refresh #4297 --- .../Store/Middleware/createPersistState.js | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/frontend/src/Store/Middleware/createPersistState.js b/frontend/src/Store/Middleware/createPersistState.js index ffb2aa0d0..2862132f6 100644 --- a/frontend/src/Store/Middleware/createPersistState.js +++ b/frontend/src/Store/Middleware/createPersistState.js @@ -29,27 +29,29 @@ function mergeColumns(path, initialState, persistedState, computedState) { const columns = []; - initialColumns.forEach((initialColumn) => { - const persistedColumnIndex = _.findIndex(persistedColumns, { name: initialColumn.name }); - const column = Object.assign({}, initialColumn); - const persistedColumn = persistedColumnIndex > -1 ? persistedColumns[persistedColumnIndex] : undefined; + // Add persisted columns in the same order they're currently in + // as long as they haven't been removed. + + persistedColumns.forEach((persistedColumn) => { + const columnIndex = initialColumns.findIndex((i) => i.name === persistedColumn.name); - if (persistedColumn) { - column.isVisible = persistedColumn.isVisible; + if (columnIndex >= 0) { + columns.push({ ...persistedColumn }); } + }); - // If there is a persisted column, it's index doesn't exceed the column list - // and it's modifiable, insert it in the proper position. + // Add any columns added to the app in the initial position. + initialColumns.forEach((initialColumn, index) => { + const persistedColumnIndex = persistedColumns.findIndex((i) => i.name === initialColumn.name); + const column = Object.assign({}, initialColumn); - if (persistedColumn && columns.length - 1 > persistedColumnIndex && persistedColumn.isModifiable !== false) { - columns.splice(persistedColumnIndex, 0, column); - } else { - columns.push(column); + if (persistedColumnIndex === -1) { + columns.splice(index, 0, column); } - - // Set the columns in the persisted state - _.set(computedState, path, columns); }); + + // Set the columns in the persisted state + _.set(computedState, path, columns); } function slicer(paths_) {