Skip to content

Commit

Permalink
Merge pull request #66 from Elizabethhub/feature-todayList
Browse files Browse the repository at this point in the history
feature fetchAllWaterThunk in TodayElement
  • Loading branch information
Elizabethhub authored Mar 19, 2024
2 parents 223bd86 + b745456 commit 90377a3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 18 deletions.
16 changes: 14 additions & 2 deletions src/components/ModalAddWater/ModalAddWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import 'react-datepicker/dist/react-datepicker.css';
import useCounter from '../../hooks/modalHandleUpdate.js';
import { useDispatch } from 'react-redux';
import { addWaterThunk } from '../../store/water/operations.js';
import { changeModalClose } from '../../store/water/waterSlice.js';
import {
changeModalClose,
changeTodayList,
} from '../../store/water/waterSlice.js';
import { toast } from 'react-toastify';

const ModalAddWater = () => {
Expand All @@ -27,14 +30,23 @@ const ModalAddWater = () => {

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

// const currentTime = new Date();

const water = {
milliliters: e.target.elements.value.value,
time,
time: time,
};

// const water = {
// milliliters: e.target.elements.value.value,
// time,
// };
dispatch(addWaterThunk(water))
.unwrap()
.then(() => {
dispatch(changeModalClose(false));
dispatch(changeTodayList({ ...water, _id: crypto.randomUUID() }));
toast.success('Water note was successfuly added');
})
.catch((error) => {
Expand Down
40 changes: 29 additions & 11 deletions src/components/TodayElement/TodayElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,51 @@ import {
changeModalDeleteForm,
changeModalEditForm,
} from '../../store/water/waterSlice';
import { modalDeleteOpen } from '../../store/water/selectors';
import {
modalDeleteOpen,
selectorWaterToday,
} from '../../store/water/selectors.js';
import ModalDeleteWater from '../ModalDeleteWater/ModalDeleteWater.jsx';
import { useEffect } from 'react';
import { fetchAllWaterThunk } from '../../store/water/operations.js';
import { selectUser } from '../../store/auth/selectors.js';

const TodayElement = () => {
const isModalOpen = useSelector(modalDeleteOpen);
const isUser = useSelector(selectUser);

const waterTodayList = useSelector(selectorWaterToday);

const dispatch = useDispatch();

let waterArr = [
{ id: 1, amount: 250, hours: '11:00' },
{ id: 2, amount: 350, hours: '10:00' },
{ id: 3, amount: 200, hours: '11:00' },
{ id: 4, amount: 150, hours: '10:00' },
];
useEffect(() => {
if (isUser) {
dispatch(fetchAllWaterThunk());
}
}, [dispatch, isUser]);

function formatDate(date) {
if (date) {
const newDate = new Date(date);
const hour = newDate.getHours();
const minute = newDate.getMinutes();
return `${hour}:${minute}`;
}
}

return (
<>
<ListWrapper>
<h2>Today</h2>
<StyledList>
{waterArr.map((item) => {
{waterTodayList.map((item) => {
return (
<ListItem key={item.id}>
<ListItem id={item._id} key={item._id}>
<InfoWrapper>
<SvgGlass />
<Amount>{item.amount} ml</Amount>
<Time>{item.hours} PM</Time>
<Amount>{item.milliliters} ml</Amount>
<Time>{formatDate(item.time)} PM</Time>
{/* <Time>{console.log(item.time)} PM</Time> */}
</InfoWrapper>
<BtnWrapper>
<div
Expand Down
15 changes: 14 additions & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ export const store = configureStore({
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
ignoredActions: [
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
'waterSlice/changeTodayList',
],

// Ignore these field paths in all actions
ignoredActionPaths: ['meta.arg', 'payload.time'],
// Ignore these paths in the state
ignoredPaths: ['waterSlice.waterTodayList'],
},
}),
});
Expand Down
1 change: 0 additions & 1 deletion src/store/water/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const fetchAllWaterThunk = createAsyncThunk(
async (_, thunkApi) => {
try {
const { data } = await api.get('water/');
console.log(data);
return data;
} catch (error) {
return thunkApi.rejectWithValue(error.message);
Expand Down
2 changes: 2 additions & 0 deletions src/store/water/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const modalIsDelete = (state) => state.waterSlice.modal?.modalDeleteForm;

export const showDaysGenStats = (state) => state.waterSlice.daysGenStats;
export const dayNorma = (state) => state.waterSlice.dayNorma;

export const selectorWaterToday = (state) => state.waterSlice.waterTodayList;
18 changes: 15 additions & 3 deletions src/store/water/waterSlice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { fetchAllWaterThunk } from './operations';

const waterSlice = createSlice({
name: 'waterSlice',
Expand All @@ -15,6 +16,7 @@ const waterSlice = createSlice({
daysGenStats: false,
dayNorma: 1.5,
isLoading: false,
waterTodayList: [],
},
reducers: {
changeModalClose: (state, { payload }) => {
Expand Down Expand Up @@ -48,9 +50,18 @@ const waterSlice = createSlice({
changeDayNorma: (state, { payload }) => {
state.dayNorma = payload;
},
// extraReducers: (builder) => {
// // builder.addCase();
// },
changeTodayList: (state, { payload }) => {
state.waterTodayList.push(payload);
},
},
extraReducers: (builder) => {
builder
.addCase(fetchAllWaterThunk.fulfilled, (state, { payload }) => {
state.waterTodayList.push(...payload);
})
.addCase(fetchAllWaterThunk.rejected, (state, { payload }) => {
state.error = payload;
});
},
});
export const waterReducer = waterSlice.reducer;
Expand All @@ -62,4 +73,5 @@ export const {
changeModalDeleteForm,
changeShowDaysStats,
changeDayNorma,
changeTodayList,
} = waterSlice.actions;

0 comments on commit 90377a3

Please sign in to comment.