Skip to content

Commit

Permalink
Merge pull request #50 from Tasitaforme/check-del-user,-food
Browse files Browse the repository at this point in the history
checked
  • Loading branch information
Tasitaforme authored Dec 26, 2023
2 parents e295a39 + 84e1ca5 commit f168dd9
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/components/ChangePasswordForm/ChangePasswordForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default function ChangePasswordForm() {
placeholder="Password"
autoComplete="off"
className={
touched.quantity
? errors.quantity
touched.password
? errors.password
? 'input-error'
: 'input-success'
: 'input-normal'
Expand All @@ -61,8 +61,8 @@ export default function ChangePasswordForm() {
placeholder="New password"
autoComplete="off"
className={
touched.quantity
? errors.quantity
touched.newPassword
? errors.newPassword
? 'input-error'
: 'input-success'
: 'input-normal'
Expand Down
15 changes: 7 additions & 8 deletions src/components/DeleteUser/DeleteUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
Button,
HeadlineSecond,
} from '../StyledComponents/Components.styled';
import { useDispatch, useSelector } from 'react-redux';
import { selectUserInfo } from '../../redux/auth/selectors';
import { useDispatch } from 'react-redux';
import toast from 'react-hot-toast';

import { deleteUserSchema } from '../../schemas/formik';
Expand All @@ -21,17 +20,17 @@ import { logOut } from '../../redux/auth/operations';
export default function DeleteUser() {
const dispatch = useDispatch();
const navigate = useNavigate();
const { email } = useSelector(selectUserInfo);

const onSubmit = async (values, actions) => {
try {
await removeUser({ email, ...values });
dispatch(logOut()).unwrap();
await removeUser(values.password);
await dispatch(logOut()).unwrap();
toast.success('You have successfully deleted your account!');
navigate('/welcome');
} catch (error) {
console.log(error);
toast.error(`Something went wrong! \n ${error.message}`);
toast.error(`Something went wrong! \n ${error.message}!`);
return;
}
actions.resetForm();
};
Expand Down Expand Up @@ -60,8 +59,8 @@ export default function DeleteUser() {
placeholder="Password"
autoComplete="off"
className={
touched.quantity
? errors.quantity
touched.password
? errors.password
? 'input-error'
: 'input-success'
: 'input-normal'
Expand Down
2 changes: 1 addition & 1 deletion src/components/Diary/DiaryMainModal/DiaryMainModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function DiaryMainModal({ modalData, setModalData }) {
<ButtonIcon
type="button"
onClick={() => {
handleDelete(item.id);
handleDelete({ id: item.id, diary: type });
}}
>
<IconWrap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const MealsList = styled.ul`
display: flex;
flex-direction: column;
row-gap: 18px;
align-items: center;
align-items: flex-start;
justify-content: flex-start;
`;

Expand Down
6 changes: 3 additions & 3 deletions src/components/Food/Food.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default function Food() {
const { meals, isLoading } = useSelector(selectDiaryInfo);
const { fat, protein, carbohydrate, calories } = getTotalNutrients(meals);

useEffect(() => {
dispatch(getFoodDiaryToday()).unwrap();
}, [fat, protein, carbohydrate, calories]);
// useEffect(() => {
// dispatch(getFoodDiaryToday()).unwrap();
// }, [fat, protein, carbohydrate, calories]);

const nutrientsTitles = ['Carbonohidrates', 'Protein', 'Fat'];

Expand Down
2 changes: 1 addition & 1 deletion src/components/UserInfoModal/UserInfoModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function UserInfoModal({ onCloseModal }) {
toast.success('You have successfully logged out!');
onCloseModal();
} catch (error) {
toast.error(`Something went wrong! ${error.message}`);
toast.error(`Something went wrong! \n ${error}`);
}
};

Expand Down
29 changes: 3 additions & 26 deletions src/pages/MainPage/MainPage.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import { FeatureWrap, TitleWrap } from './MainPage.styled';
import { FeatureWrap, MainWrap, TitleWrap } from './MainPage.styled';

import { Container } from 'components/StyledComponents/Container';
import {
Expand All @@ -14,14 +14,9 @@ import Water from 'components/Water/Water';
import Food from 'components/Food/Food';
import Diary from 'components/Diary/Diary';
import RecommendedFood from 'components/RecommendedFood/RecommendedFood';
import { Button } from '../../components/StyledComponents/Components.styled';
import { useDispatch, useSelector } from 'react-redux';
import { toast } from 'react-hot-toast';
import { logOut } from '../../redux/auth/operations';
import { selectIsLogin } from '../../redux/auth/selectors';
import { getDailyWater } from '../../redux/water/operations';
import ChangePasswordForm from '../../components/ChangePasswordForm/ChangePasswordForm';
import DeleteUser from '../../components/DeleteUser/DeleteUser';
import { getFoodDiaryToday } from '../../redux/diary/operations';

export default function MainPage() {
Expand All @@ -48,18 +43,8 @@ export default function MainPage() {
document.body.style.overflow = 'auto';
}

// TODO видалити потім, коли буде можливість вийти в хедері
const handleOut = async () => {
try {
await dispatch(logOut()).unwrap();
toast.success('You have successfully logged out!');
} catch (error) {
toast.error(`Something went wrong! \n ${error.message}`);
}
};

return (
<main>
<MainWrap>
<Container>
<TitleWrap>
<HeadlineFirst>Today</HeadlineFirst>
Expand All @@ -80,15 +65,7 @@ export default function MainPage() {
<Diary></Diary>
<RecommendedFood></RecommendedFood>
</FeatureWrap>
{/* // TODO видалити потім, коли буде можливість вийти в хедері */}
<Button type="submit" onClick={() => handleOut()}>
Sign out
</Button>
{/* // TODO видалити потім, коли буде додано в Профайл */}
<ChangePasswordForm />
{/* // TODO видалити потім, коли буде додано в Профайл */}
<DeleteUser />
</Container>
</main>
</MainWrap>
);
}
17 changes: 14 additions & 3 deletions src/pages/MainPage/MainPage.styled.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import styled from 'styled-components';

export const MainWrap = styled.main`
padding-top: 16px;
padding-bottom: 80px;
@media screen and (min-width: 834px) {
padding-top: 24px;
padding-bottom: 40px;
}
@media screen and (min-width: 1440px) {
padding-top: 20px;
padding-bottom: 54px;
}
`;

export const TitleWrap = styled.div`
display: flex;
flex-wrap: nowrap;
align-items: center;
justify-content: space-between;
margin-top: 16px;
margin-bottom: 20px;
@media screen and (min-width: 834px) {
margin-top: 24px;
margin-bottom: 24px;
}
@media screen and (min-width: 1440px) {
margin-top: 20px;
margin-bottom: 16px;
}
`;
Expand Down
3 changes: 1 addition & 2 deletions src/redux/auth/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ export const logOut = createAsyncThunk(
try {
const { data } = await instance.post('/api/auth/logout');
store.dispatch(resetWater());
store.dispatch(resetStatistics());
store.dispatch(resetDiary());
setToken();
return data;
} catch (error) {
return rejectWithValue(error.response.data);
return rejectWithValue(error.message);
}
}
);
Expand Down
1 change: 0 additions & 1 deletion src/redux/diary/diarySlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const initialState = {
carbohydrate: 0,
isLoading: false,
error: null,
firstLoad: false,
};

const handlePending = (state) => {
Expand Down
14 changes: 10 additions & 4 deletions src/requests/deleteUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import instance from '../redux/auth/operations';
* headers: Authorization: Bearer token
* body: { email, password }
*/
export const removeUser = async (body) => {

export const removeUser = async (password) => {
try {
const { data } = await instance.delete('/api/auth/delete', { data: body });
return data;
const response = await instance.delete(`/api/auth/delete/${password}`);
if (response && response.data) {
const { data } = response;
return data;
}
return null;
} catch (error) {
return error.response.data;
console.log(error);
return error;
}
};
20 changes: 10 additions & 10 deletions src/schemas/formik.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const loginSchema = Yup.object().shape({
.matches(EMAIL_RULE, 'Invalid email address'),
password: Yup.string()
.required('Password is required')
.min(7, 'The password must be at least 7 characters long')
.matches(
PASSWORD_RULE,
`Your password is at least 7 characters, contain 1 uppercase letter, 1 lowercase letter, 1 number. The letters is in English.`
),
.min(7, 'The password must be at least 7 characters long'),
// .matches(
// PASSWORD_RULE,
// `Your password is at least 7 characters, contain 1 uppercase letter, 1 lowercase letter, 1 number. The letters is in English.`
// ),
});

export const forgotPasswordSchema = Yup.object().shape({
Expand All @@ -28,11 +28,11 @@ export const forgotPasswordSchema = Yup.object().shape({
export const changePasswordSchema = Yup.object().shape({
password: Yup.string()
.required('Password is required')
.min(7, 'The password must be at least 7 characters long')
.matches(
PASSWORD_RULE,
`Your password is at least 7 characters, contain 1 uppercase letter, 1 lowercase letter, 1 number. The letters is in English.`
),
.min(7, 'The password must be at least 7 characters long'),
// .matches(
// PASSWORD_RULE,
// `Your password is at least 7 characters, contain 1 uppercase letter, 1 lowercase letter, 1 number. The letters is in English.`
// ),
newPassword: Yup.string()
.required('Password is required')
.min(7, 'The password must be at least 7 characters long')
Expand Down

0 comments on commit f168dd9

Please sign in to comment.