Skip to content

Commit

Permalink
Merge pull request #83 from MargoMarm/fix/auth
Browse files Browse the repository at this point in the history
fix auth body params adding
  • Loading branch information
nzend committed Sep 24, 2023
2 parents fed4ddd + 80f0f62 commit 725867b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/components/ParamsForm/ParamsForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Formik, Form } from 'formik';
import PropTypes from 'prop-types';
import * as Yup from 'yup';
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { Swiper, SwiperSlide } from 'swiper/react';
import Notiflix from 'notiflix';
import 'swiper/css';
import {
FormControl,
Expand All @@ -13,9 +15,8 @@ import {
colors,
} from '@mui/material';
import { sub } from 'date-fns';
import { format } from 'date-fns';
import axios from 'axios';

import { updateBodyParts } from '../../redux/auth/operation';
import {
FormikField,
InputGroup,
Expand All @@ -36,6 +37,7 @@ import Calendar from '../Calendar/Calendar';

const ParamsForm = ({ setSteps, setSwiperRef }) => {
const navigate = useNavigate();
const dispatch = useDispatch();

const maxDate = sub(new Date(), { years: 18 });
const minDate = sub(new Date(), { years: 70 });
Expand All @@ -52,19 +54,19 @@ const ParamsForm = ({ setSteps, setSwiperRef }) => {
levelActivity: '',
}}
onSubmit={async (values, Formik) => {
const newParamsUser = {
if (Object.values(values).includes('')) {
Notiflix.Notify.warning('PLEASE, FILL ALL FIELDS');
return;
}

const newParams = {
...values,
blood: Number(values.blood),
levelActivity: Number(values.levelActivity),
birthday: values.birthday,
};

console.log(newParamsUser);

await axios.post(
'https://power-pulse-rest-api.onrender.com/api/users/create',
newParamsUser,
);
dispatch(updateBodyParts(newParams));

navigate('/diary');

Expand Down Expand Up @@ -447,13 +449,13 @@ ParamsForm.propTypes = {
};

const paramsSchema = Yup.object({
height: Yup.number().min(150).required(),
currentWeight: Yup.number().min(35).required(),
desiredWeight: Yup.number().min(35).required(),
birthday: Yup.string().required(),
blood: Yup.string().required(),
sex: Yup.string().required(),
levelActivity: Yup.number().required(),
height: Yup.number().min(150).max(250),
currentWeight: Yup.number().min(35).max(400),
desiredWeight: Yup.number().min(35).max(400),
birthday: Yup.string(),
blood: Yup.string(),
sex: Yup.string(),
levelActivity: Yup.number(),
});

export default ParamsForm;
12 changes: 12 additions & 0 deletions src/redux/auth/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@ export const updateUserData = createAsyncThunk(
}
},
);

export const updateBodyParts = createAsyncThunk(
'auth/updateBodyParts',
async (userData, { rejectWithValue }) => {
try {
const { data } = await axios.post('/api/users/create', userData);
return data;
} catch (e) {
return rejectWithValue(e.message);
}
},
);
7 changes: 7 additions & 0 deletions src/redux/auth/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
logInUser,
logOutUser,
updateUserData,
updateBodyParts,
} from './operation';

const initialState = {
Expand Down Expand Up @@ -41,13 +42,19 @@ export const authSlice = createSlice({
extraReducers: builder => {
builder.addCase(authUser.fulfilled, (state, action) => {
state.token = action.payload.token;
state.user.name = action.payload.name;
state.user.email = action.payload.email;
state.isLoggedIn = true;
state.error = null;
});
builder.addCase(authUser.rejected, (state, action) => {
state.error = action.payload;
});

builder.addCase(updateBodyParts.fulfilled, (state, { payload }) => {
replaceUserState(state, payload);
});

builder.addCase(logInUser.fulfilled, (state, { payload }) => {
replaceUserState(state, payload);
state.token = payload.token;
Expand Down

0 comments on commit 725867b

Please sign in to comment.