Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: site soil async #224

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions src/soilId/soilIdSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ export type ProjectSoilSettings = Omit<
depthIntervals: ProjectDepthInterval[];
};

const initialState = {
soilData: {} as Record<string, SoilData>,
projectSettings: {} as Record<string, ProjectSoilSettings>,
loading: false,
type SoilState = {
soilData: Record<string, SoilData>;
projectSettings: Record<string, ProjectSoilSettings>;
status: 'loading' | 'error' | 'ready';
};

const initialState: SoilState = {
soilData: {},
projectSettings: {},
status: 'loading',
};

export const sameDepth =
Expand Down Expand Up @@ -123,6 +129,12 @@ const soilIdSlice = createSlice({
) => {
Object.assign(state.projectSettings, action.payload);
},
setSoilIdStatus: (
state,
action: PayloadAction<'loading' | 'error' | 'ready'>,
) => {
state.status = action.payload;
},
},
extraReducers: builder => {
builder.addCase(updateSoilData.fulfilled, (state, action) => {
Expand All @@ -137,6 +149,49 @@ const soilIdSlice = createSlice({
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 All @@ -154,20 +209,21 @@ const soilIdSlice = createSlice({
});

builder.addCase(fetchSoilDataForUser.pending, state => {
state.loading = true;
state.status = 'loading';
});

builder.addCase(fetchSoilDataForUser.rejected, state => {
state.loading = false;
state.status = 'error';
});

builder.addCase(fetchSoilDataForUser.fulfilled, state => {
state.loading = false;
state.status = 'ready';
});
},
});

export const { setProjectSettings, setSoilData } = soilIdSlice.actions;
export const { setProjectSettings, setSoilData, setSoilIdStatus } =
soilIdSlice.actions;

export const fetchSoilDataForUser = createAsyncThunk(
'soilId/fetchSoilDataForUser',
Expand Down Expand Up @@ -195,6 +251,11 @@ export const updateSoilDataDepthInterval = createAsyncThunk(
soilIdService.updateSoilDataDepthInterval,
);

export const updateSoilDataDepthIntervalAsync = createAsyncThunk(
'soilId/updateSoilDataDepthIntervalAsync',
soilIdService.updateSoilDataDepthInterval,
);

export const deleteSoilDataDepthInterval = createAsyncThunk(
'soilId/deleteSoilDataDepthInterval',
soilIdService.deleteSoilDataDepthInterval,
Expand Down