diff --git a/src/selectors.ts b/src/selectors.ts index ea291159..8321aa52 100644 --- a/src/selectors.ts +++ b/src/selectors.ts @@ -350,6 +350,13 @@ const matchIntervals = ( return intervals; }; +const defaultSoilData: SoilData = { + depthDependentData: [], + depthIntervals: [], +}; +export const selectSoilData = (siteId: string) => (state: SharedState) => + state.soilId.soilData[siteId] ?? defaultSoilData; + /** * Preset intervals can also have soil data intervals on the backend. * These soil data intervals are only used to store configuration values. @@ -358,7 +365,7 @@ const matchIntervals = ( */ export const selectSoilDataIntervals = createSelector( [ - (state: SharedState, siteId: string) => state.soilId.soilData[siteId], + (state: SharedState, siteId: string) => selectSoilData(siteId)(state), (state: SharedState, siteId: string) => { const projectId = state.site.sites[siteId]?.projectId; if (projectId === undefined) { @@ -413,6 +420,6 @@ export const selectDepthDependentData = depthInterval: { depthInterval: DepthInterval }; }) => (state: SharedState) => - state.soilId.soilData[siteId].depthDependentData.find( + selectSoilData(siteId)(state).depthDependentData.find( sameDepth(depthInterval), ); diff --git a/src/soilId/soilIdSlice.ts b/src/soilId/soilIdSlice.ts index a0f3eb86..6c87d776 100644 --- a/src/soilId/soilIdSlice.ts +++ b/src/soilId/soilIdSlice.ts @@ -43,7 +43,7 @@ export const methodRequired = ( ): `${T}Required` => `${method}Required`; export type SoilState = { - soilData: Record; + soilData: Record; projectSettings: Record; status: 'loading' | 'error' | 'ready'; }; @@ -111,57 +111,6 @@ const soilIdSlice = createSlice({ state.soilData[action.meta.arg.siteId] = action.payload; }); - builder.addCase( - updateSoilDataDepthIntervalAsync.fulfilled, - (state, action) => { - state.soilData[action.meta.arg.siteId] = action.payload; - }, - ); - - builder.addCase( - updateSoilDataDepthIntervalAsync.pending, - (state, action) => { - const currentState = state.soilData[action.meta.arg.siteId]; - const update = Object.fromEntries( - Object.entries(action.meta.arg).filter( - ([key, result]) => result !== null && key !== 'siteId', - ), - ); - 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, - ...update, - }; - }, - ); - - 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; }); @@ -225,11 +174,6 @@ export const updateSoilDataDepthInterval = createAsyncThunk( soilIdService.updateSoilDataDepthInterval, ); -export const updateSoilDataDepthIntervalAsync = createAsyncThunk( - 'soilId/updateSoilDataDepthIntervalAsync', - soilIdService.updateSoilDataDepthInterval, -); - export const deleteSoilDataDepthInterval = createAsyncThunk( 'soilId/deleteSoilDataDepthInterval', soilIdService.deleteSoilDataDepthInterval,