Skip to content

Commit

Permalink
Merge pull request #113 from Elizabethhub/feature/extrareducers
Browse files Browse the repository at this point in the history
add extrareducer add edit delete
  • Loading branch information
Elizabethhub authored Mar 26, 2024
2 parents 2607844 + 0af7984 commit 669d3fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/components/ModalAddWater/ModalAddWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const ModalAddWater = () => {
dispatch(changeModalClose(false));
dispatch(fetchTodayWaterThunk({ date }));
dispatch(fetchMonthWaterThunk({ year, month }));
toast.success('Water note was successfully added');
})
.catch((error) => {
toast.error(error);
Expand Down
1 change: 0 additions & 1 deletion src/components/ModalDeleteWater/ModalDeleteWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const ModalDeleteWater = () => {
dispatch(fetchTodayWaterThunk({ date }));
dispatch(changeModalClose(false));
dispatch(fetchMonthWaterThunk({ year, month }));
toast.success('Water note was successfully deleted');
})
.catch((error) => {
toast.error(error);
Expand Down
1 change: 0 additions & 1 deletion src/components/ModalEditWater/ModalEditWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const ModalEditWater = ({ waterItem }) => {
dispatch(fetchTodayWaterThunk({ date }));
dispatch(changeModalClose(false));
dispatch(fetchMonthWaterThunk({ year, month }));
toast.success('Water note was successfully edited');
})
.catch((error) => {
toast.error(error);
Expand Down
49 changes: 49 additions & 0 deletions src/store/water/waterSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
fetchMonthWaterThunk,
editDailyNormaThunk,
fetchTodayWaterThunk,
addWaterThunk,
editWaterThunk,
deleteWaterThunk,
} from './operations';
import { toast } from 'react-toastify';

const waterSlice = createSlice({
name: 'waterSlice',
Expand Down Expand Up @@ -88,6 +92,51 @@ const waterSlice = createSlice({
})
.addCase(fetchTodayWaterThunk.rejected, (state, { payload }) => {
state.error = payload;
})
.addCase(addWaterThunk.fulfilled, (state, { payload }) => {
state.waterTodayList.push(payload);
state.isLoading = false;
toast.success('Water note was successfully added');
})
.addCase(addWaterThunk.pending, (state) => {
state.isLoading = true;
})
.addCase(addWaterThunk.rejected, (state, action) => {
toast.error(`Failed to add water: ${action.payload}`);
state.isLoading = false;
})
.addCase(editWaterThunk.fulfilled, (state, { payload }) => {
const index = state.waterTodayList.findIndex(
(water) => water._id === payload.id
);
if (index !== -1) {
state.waterTodayList[index] = payload;
} else {
state.waterTodayList.push(payload);
}
state.isLoading = false;
toast.success('Water note was successfully edited');
})
.addCase(editWaterThunk.pending, (state) => {
state.isLoading = true;
})
.addCase(editWaterThunk.rejected, (state, action) => {
toast.error(`Failed to edit water: ${action.payload}`);
state.isLoading = false;
})
.addCase(deleteWaterThunk.fulfilled, (state, { payload }) => {
state.waterTodayList = state.waterTodayList.filter(
(water) => water._id !== payload
);
state.isLoading = false;
toast.success('Water note was successfully deleted!');
})
.addCase(deleteWaterThunk.pending, (state) => {
state.isLoading = true;
})
.addCase(deleteWaterThunk.rejected, (state, action) => {
state.isLoading = false;
toast.error(`Failed to delete water: ${action.payload}`);
});
},
});
Expand Down

0 comments on commit 669d3fd

Please sign in to comment.