Skip to content

Commit fc6ae89

Browse files
committed
fix: incorrect emhttp state merging in redux store
omit stale config entries while merging in new config state
1 parent a7ef06e commit fc6ae89

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

api/src/store/modules/emhttp.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { join } from 'path';
22

33
import type { PayloadAction } from '@reduxjs/toolkit';
44
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
5-
import { merge } from 'lodash-es';
5+
import { mergeWith } from 'lodash-es';
66

77
import type { RootState } from '@app/store/index.js';
88
import type { StateFileToIniParserMap } from '@app/store/types.js';
@@ -164,6 +164,15 @@ export const loadStateFiles = createAsyncThunk<
164164
return state;
165165
});
166166

167+
// Custom merge function that replaces arrays instead of merging them element-wise
168+
const mergeReplaceArrays = (object: any, source: any, ...rest: any[]) =>
169+
mergeWith(object, source, ...rest, (objValue: unknown, srcValue: unknown) => {
170+
if (Array.isArray(srcValue)) {
171+
return srcValue;
172+
}
173+
return undefined;
174+
});
175+
167176
export const emhttp = createSlice({
168177
name: 'emhttp',
169178
initialState,
@@ -176,7 +185,7 @@ export const emhttp = createSlice({
176185
}>
177186
) {
178187
const { field } = action.payload;
179-
return merge(state, { [field]: action.payload.state });
188+
return mergeReplaceArrays(state, { [field]: action.payload.state });
180189
},
181190
},
182191
extraReducers(builder) {
@@ -185,18 +194,16 @@ export const emhttp = createSlice({
185194
});
186195

187196
builder.addCase(loadStateFiles.fulfilled, (state, action) => {
188-
merge(state, action.payload, { status: FileLoadStatus.LOADED });
197+
mergeReplaceArrays(state, action.payload, { status: FileLoadStatus.LOADED });
189198
});
190199

191200
builder.addCase(loadStateFiles.rejected, (state, action) => {
192-
merge(state, action.payload, { status: FileLoadStatus.FAILED_LOADING });
201+
mergeReplaceArrays(state, action.payload, { status: FileLoadStatus.FAILED_LOADING });
193202
});
194203

195204
builder.addCase(loadSingleStateFile.fulfilled, (state, action) => {
196205
if (action.payload) {
197-
// const changedKey = Object.keys(action.payload)[0]
198-
// emhttpLogger.debug('Key', changedKey, 'Difference in changes', getDiff(action.payload, { [changedKey]: state[changedKey] } ))
199-
merge(state, action.payload);
206+
mergeReplaceArrays(state, action.payload);
200207
} else {
201208
emhttpLogger.warn('Invalid payload returned from loadSingleStateFile()');
202209
}

0 commit comments

Comments
 (0)