Skip to content

Commit

Permalink
feat: soil data selector (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm authored Feb 22, 2024
1 parent 665afe2 commit 45ae34e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 59 deletions.
11 changes: 9 additions & 2 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
);
58 changes: 1 addition & 57 deletions src/soilId/soilIdSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const methodRequired = <T extends CollectionMethod>(
): `${T}Required` => `${method}Required`;

export type SoilState = {
soilData: Record<string, SoilData>;
soilData: Record<string, SoilData | undefined>;
projectSettings: Record<string, ProjectSoilSettings>;
status: 'loading' | 'error' | 'ready';
};
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 45ae34e

Please sign in to comment.