Skip to content

Commit

Permalink
feat: Reverse boolean soil ID updates
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Dec 7, 2023
1 parent 3086a1c commit 2121d99
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/soilId/soilIdSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const soilIdSlice = createSlice({
) => {
Object.assign(state.projectSettings, action.payload);
},
setSoilDataStatus: (
setSoilIdStatus: (
state,
action: PayloadAction<'loading' | 'error' | 'ready'>,
) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 2121d99

Please sign in to comment.