Skip to content

Commit

Permalink
Merge pull request #78 from Elizabethhub/feature/delete-water
Browse files Browse the repository at this point in the history
Feature/delete water
  • Loading branch information
Elizabethhub authored Mar 21, 2024
2 parents 0053638 + 729b653 commit 4a6ca81
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 43 deletions.
11 changes: 6 additions & 5 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,19 +17,20 @@ 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);
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 @@ -20,7 +20,6 @@ import {
} from '../../store/water/waterSlice';
import {
modalDeleteOpen,
modalId,
selectorWaterToday,
} from '../../store/water/selectors.js';
import ModalDeleteWater from '../ModalDeleteWater/ModalDeleteWater.jsx';
Expand All @@ -30,7 +29,6 @@ import { format } from 'date-fns';

const TodayElement = () => {
const isModalOpen = useSelector(modalDeleteOpen);
const id = useSelector(modalId);
const waterTodayList = useSelector(selectorWaterToday);
const dispatch = useDispatch();

Expand All @@ -50,7 +48,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 @@ -65,6 +62,7 @@ const TodayElement = () => {
onClick={() => {
dispatch(changeModalId(item._id));
dispatch(changeModalDeleteForm(true));
dispatch(changeModalId(item._id));
}}
>
<DeleteSvg />
Expand All @@ -85,11 +83,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
18 changes: 10 additions & 8 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,6 +55,14 @@ const waterSlice = createSlice({
(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 @@ -64,12 +71,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 @@ -84,4 +85,5 @@ export const {
changeTodayList,
changeModalId,
deleteWater,
editWater,
} = waterSlice.actions;

0 comments on commit 4a6ca81

Please sign in to comment.