Skip to content

Commit

Permalink
fix edit and delete logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirra-acc committed Mar 21, 2024
1 parent 330faa0 commit 48a7b82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 45 deletions.
12 changes: 6 additions & 6 deletions src/components/ModalDeleteWater/ModalDeleteWater.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SvgCross from '../../images/svg/svgModal/SvgCross.jsx';
import { useDispatch, useSelector } from 'react-redux';
import { modalDeleteOpen } from '../../store/water/selectors.js';
import { modalDeleteOpen, modalId } from '../../store/water/selectors.js';
import { changeModalClose, deleteWater } from '../../store/water/waterSlice.js';
import { useEffect } from 'react';
import {
Expand All @@ -17,26 +17,26 @@ import useKeyDown from '../../hooks/modalCloseEsc.js';
import { deleteWaterThunk } from '../../store/water/operations.js';
import { toast } from 'react-toastify';

const ModalDeleteWater = ({waterItem}) => {
const ModalDeleteWater = () => {
const isModalOpen = useSelector(modalDeleteOpen);
const id = useSelector(modalId);

const dispatch = useDispatch();

const onSubmit = (e) => {
e.preventDefault();

dispatch(deleteWaterThunk(waterItem?._id))
dispatch(deleteWaterThunk(id))
.unwrap()
.then(() => {
// dispatch(deleteWater(waterItem._id))
dispatch(deleteWater(id));
dispatch(changeModalClose(false));
// toast.success('Water note was successfully deleted');
toast.success('Water note was successfully deleted');
})
.catch((error) => {
toast.error(error);
});
};
// console.log(waterItem);

const clickBackdrop = useClickBackdrop();

Expand Down
25 changes: 5 additions & 20 deletions src/components/ModalEditWater/ModalEditWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useCounter from '../../hooks/modalHandleUpdate.js';
import { format } from 'date-fns';
import { useDispatch } from 'react-redux';
import { editWaterThunk } from '../../store/water/operations.js';
import { changeModalClose } from '../../store/water/waterSlice.js';
import { changeModalClose, editWater } from '../../store/water/waterSlice.js';
import { toast } from 'react-toastify';

const ModalEditWater = ({ waterItem }) => {
Expand All @@ -38,13 +38,16 @@ const ModalEditWater = ({ waterItem }) => {
e.preventDefault();

const updatedWater = {
...waterItem,
milliliters: parseInt(manualValue),
time,
};
console.log(waterItem);

dispatch(editWaterThunk({ id: waterItem?._id, water: updatedWater }))
dispatch(editWaterThunk({ id: waterItem?._id, ...updatedWater }))
.unwrap()
.then(() => {
dispatch(editWater({ id: waterItem?._id, ...updatedWater }));
dispatch(changeModalClose(false));
toast.success('Water note was successfully edited');
})
Expand All @@ -65,24 +68,6 @@ const ModalEditWater = ({ waterItem }) => {
setManualValue(value);
};

// const getDisplayValue = () => {
// if (waterItem) {
// switch (valueSource) {
// case 'manual':
// return manualValue
// ? `${manualValue}ml`
// : `${waterItem?.milliliters}ml`;
// case 'counter':
// return `${counter}ml`;
// default:
// return `${waterItem?.milliliters}ml`;
// }
// } else {
// toast.error('This record does not exist');
// return `${counter}ml`;
// }
// };

const handleInputBlur = () => {
setInputFocused(false);
};
Expand Down
10 changes: 2 additions & 8 deletions src/components/TodayElement/TodayElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from '../../store/water/waterSlice';
import {
modalDeleteOpen,
modalId,
selectorWaterToday,
} from '../../store/water/selectors.js';
import ModalDeleteWater from '../ModalDeleteWater/ModalDeleteWater.jsx';
Expand All @@ -33,7 +32,6 @@ import { format } from 'date-fns';
const TodayElement = () => {
const isModalOpen = useSelector(modalDeleteOpen);
const isUser = useSelector(selectUser);
const id = useSelector(modalId);

const waterTodayList = useSelector(selectorWaterToday);

Expand All @@ -57,7 +55,6 @@ const TodayElement = () => {
<SvgGlass />
<Amount>{item.milliliters} ml</Amount>
<Time>{format(item.time, 'hh:mm a')}</Time>
{/* <Time>{console.log(item.time)} PM</Time> */}
</InfoWrapper>
<BtnWrapper>
<div
Expand All @@ -71,6 +68,7 @@ const TodayElement = () => {
<div
onClick={() => {
dispatch(changeModalDeleteForm(true));
dispatch(changeModalId(item._id));
}}
>
<DeleteSvg />
Expand All @@ -91,11 +89,7 @@ const TodayElement = () => {
<span>Add water</span>
</button>
</AddBtnWrapper>
{isModalOpen && (
<ModalDeleteWater
waterItem={waterTodayList.find((item) => item._id !== id)}
/>
)}
{isModalOpen && <ModalDeleteWater />}
</ListWrapper>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions src/store/water/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const fetchAllWaterThunk = createAsyncThunk(

export const editWaterThunk = createAsyncThunk(
'water/editWater',
async ({ id, water }, thunkApi) => {
async ({ id, milliliters, time }, thunkApi) => {
try {
const { data } = await api.put(`water/${id}`, water);
const { data } = await api.put(`water/${id}`, { milliliters, time });
return data;
} catch (error) {
return thunkApi.rejectWithValue(error.message);
Expand Down
20 changes: 11 additions & 9 deletions src/store/water/waterSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { deleteWaterThunk, fetchAllWaterThunk } from './operations';
import { toast } from 'react-toastify';
import { fetchAllWaterThunk } from './operations';

const waterSlice = createSlice({
name: 'waterSlice',
Expand Down Expand Up @@ -56,9 +55,17 @@ const waterSlice = createSlice({
},
deleteWater: (state, action) => {
state.waterTodayList = state.waterTodayList.filter(
(waterItem) => waterItem.id !== action.payload
(waterItem) => waterItem._id !== action.payload
);
},
editWater: (state, action) => {
const index = state.waterTodayList.findIndex(
(el) => el._id === action.payload.id
);
if (index !== -1) {
state.waterTodayList[index] = action.payload;
}
},
},
extraReducers: (builder) => {
builder
Expand All @@ -67,12 +74,6 @@ const waterSlice = createSlice({
})
.addCase(fetchAllWaterThunk.rejected, (state, { payload }) => {
state.error = payload;
})
.addCase(deleteWaterThunk.fulfilled, (state, { payload }) => {
state.waterTodayList = state.waterTodayList.filter(
(waterItem) => waterItem._id !== payload.id
);
toast.success('Water note was successfully deleted');
});
},
});
Expand All @@ -87,4 +88,5 @@ export const {
changeTodayList,
changeModalId,
deleteWater,
editWater,
} = waterSlice.actions;

0 comments on commit 48a7b82

Please sign in to comment.