From 0af798432577e37309ecc4bb91c6d969bb0ba0fc Mon Sep 17 00:00:00 2001 From: Kirra-Potiekhina Date: Tue, 26 Mar 2024 13:16:53 +0200 Subject: [PATCH] add extrareducer add edit delete --- .../ModalAddWater/ModalAddWater.jsx | 1 - .../ModalDeleteWater/ModalDeleteWater.jsx | 1 - .../ModalEditWater/ModalEditWater.jsx | 1 - src/store/water/waterSlice.js | 49 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/components/ModalAddWater/ModalAddWater.jsx b/src/components/ModalAddWater/ModalAddWater.jsx index 7cd3418..ae8df0c 100644 --- a/src/components/ModalAddWater/ModalAddWater.jsx +++ b/src/components/ModalAddWater/ModalAddWater.jsx @@ -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); diff --git a/src/components/ModalDeleteWater/ModalDeleteWater.jsx b/src/components/ModalDeleteWater/ModalDeleteWater.jsx index b0ba3eb..514501e 100644 --- a/src/components/ModalDeleteWater/ModalDeleteWater.jsx +++ b/src/components/ModalDeleteWater/ModalDeleteWater.jsx @@ -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); diff --git a/src/components/ModalEditWater/ModalEditWater.jsx b/src/components/ModalEditWater/ModalEditWater.jsx index 32d95ba..0420ee5 100644 --- a/src/components/ModalEditWater/ModalEditWater.jsx +++ b/src/components/ModalEditWater/ModalEditWater.jsx @@ -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); diff --git a/src/store/water/waterSlice.js b/src/store/water/waterSlice.js index 66ca4b5..4ad3468 100644 --- a/src/store/water/waterSlice.js +++ b/src/store/water/waterSlice.js @@ -4,7 +4,11 @@ import { fetchMonthWaterThunk, editDailyNormaThunk, fetchTodayWaterThunk, + addWaterThunk, + editWaterThunk, + deleteWaterThunk, } from './operations'; +import { toast } from 'react-toastify'; const waterSlice = createSlice({ name: 'waterSlice', @@ -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}`); }); }, });