From 2121d997cb23c8559a1420be98e989103f3f5e5a Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Thu, 7 Dec 2023 13:07:43 -0500 Subject: [PATCH] feat: Reverse boolean soil ID updates --- src/soilId/soilIdSlice.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/soilId/soilIdSlice.ts b/src/soilId/soilIdSlice.ts index 7f922c60..80bf7622 100644 --- a/src/soilId/soilIdSlice.ts +++ b/src/soilId/soilIdSlice.ts @@ -129,7 +129,7 @@ const soilIdSlice = createSlice({ ) => { Object.assign(state.projectSettings, action.payload); }, - setSoilDataStatus: ( + setSoilIdStatus: ( state, action: PayloadAction<'loading' | 'error' | 'ready'>, ) => { @@ -170,9 +170,27 @@ const soilIdSlice = createSlice({ }, ); - builder.addCase(updateSoilDataDepthIntervalAsync.rejected, state => { - state.status = 'error'; - }); + builder.addCase( + updateSoilDataDepthIntervalAsync.rejected, + (state, action) => { + state.status = 'error'; + const currentState = state.soilData[action.meta.arg.siteId]; + const reverseUpdate = Object.fromEntries( + Object.entries(action.meta.arg) + .filter(([, result]) => typeof result === 'boolean') + .map(([key, result]) => [key, !result]), + ); + const index = currentState.depthIntervals.findIndex( + a => a.depthInterval.start === action.meta.arg.depthInterval.start, + ); + const interval = + state.soilData[action.meta.arg.siteId].depthIntervals[index]; + state.soilData[action.meta.arg.siteId].depthIntervals[index] = { + ...interval, + ...reverseUpdate, + }; + }, + ); builder.addCase(deleteSoilDataDepthInterval.fulfilled, (state, action) => { state.soilData[action.meta.arg.siteId] = action.payload; @@ -204,7 +222,8 @@ const soilIdSlice = createSlice({ }, }); -export const { setProjectSettings, setSoilData } = soilIdSlice.actions; +export const { setProjectSettings, setSoilData, setSoilIdStatus } = + soilIdSlice.actions; export const fetchSoilDataForUser = createAsyncThunk( 'soilId/fetchSoilDataForUser',