Skip to content

Commit

Permalink
forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia-front committed May 17, 2023
1 parent 16f08e5 commit 33574e7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/components/LoginForm/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Form } from './LoginForm.styled';
import { Button, TextField, Box, Container } from '@mui/material';
import { Puff } from 'react-loading-icons';
import { useSelector } from 'react-redux';
import { selectAuthIsLoading } from 'redux/auth/selectors';
import { selectAuthIsLoading, selectAuthError } from 'redux/auth/selectors';

export const LoginForm = ({ onData }) => {
const isLoading = useSelector(selectAuthIsLoading);
const status = useSelector(selectAuthError);

const handleSubmit = e => {
e.preventDefault();
Expand All @@ -14,8 +15,9 @@ export const LoginForm = ({ onData }) => {
email: form.elements.email.value,
password: form.elements.password.value,
});

form.reset();
if (!isLoading && status !== null) {
form.reset();
}
};

return (
Expand Down
8 changes: 5 additions & 3 deletions src/components/RegisterForm/RegisterForm.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Puff } from 'react-loading-icons';
import { useSelector } from 'react-redux';
import { selectAuthIsLoading } from 'redux/auth/selectors';
import { selectAuthIsLoading, selectAuthError } from 'redux/auth/selectors';

import { Form } from './RegisterForm.styled';
import { Button, TextField, Box, Container } from '@mui/material';

export const RegisterForm = ({ onData }) => {
const isLoading = useSelector(selectAuthIsLoading);
const status = useSelector(selectAuthError);

const handleSubmit = e => {
e.preventDefault();
Expand All @@ -16,8 +17,9 @@ export const RegisterForm = ({ onData }) => {
email: form.elements.email.value,
password: form.elements.password.value,
});

form.reset();
if (!isLoading && status !== null) {
form.reset();
}
};

return (
Expand Down
9 changes: 7 additions & 2 deletions src/pages/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import toast from 'react-hot-toast';
import { useSelector } from 'react-redux';
import { selectAuthError } from 'redux/auth/selectors';
import { useDispatch } from 'react-redux';
import { logIn } from 'redux/auth/operations';
import { logIn, clearAuthError } from 'redux/auth/operations';
import { useEffect } from 'react';

export default function Login() {
const dispatch = useDispatch();
const status = useSelector(selectAuthError);

const onRegister = data => {
useEffect(() => {
if (status === 'Request failed with status code 400') {
toast.error(
'You have entered an incorrect email address or password, or you have not yet registered!'
);
dispatch(clearAuthError());
}
}, [status, dispatch]);

const onRegister = data => {
dispatch(logIn(data));
};

Expand Down
9 changes: 7 additions & 2 deletions src/pages/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import toast from 'react-hot-toast';
import { useSelector } from 'react-redux';
import { selectAuthError } from 'redux/auth/selectors';
import { useDispatch } from 'react-redux';
import { register } from 'redux/auth/operations';
import { register, clearAuthError } from 'redux/auth/operations';
import { useEffect } from 'react';

export default function Register() {
const dispatch = useDispatch();
const status = useSelector(selectAuthError);

const onRegister = data => {
useEffect(() => {
if (status === 'Request failed with status code 400') {
toast.success(
'You or someone else is already registered with such data!'
);
dispatch(clearAuthError());
}
}, [status, dispatch]);

const onRegister = data => {
dispatch(register(data));
};

Expand Down
4 changes: 4 additions & 0 deletions src/redux/auth/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ export const refreshUser = createAsyncThunk(
}
}
);

export const clearAuthError = () => ({
type: 'auth/clearError',
});
14 changes: 13 additions & 1 deletion src/redux/auth/slice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createSlice } from '@reduxjs/toolkit';
import { register, logIn, logOut, refreshUser } from './operations';
import {
register,
logIn,
logOut,
refreshUser,
// clearAuthError,
} from './operations';

export const initialState = {
user: { name: null, email: null },
Expand Down Expand Up @@ -65,6 +71,12 @@ const authSlice = createSlice({
state.isRefreshing = false;
},
},
reducers: {
clearError: state => {
state.error = null;
},
},
});
export const { clearError } = authSlice.actions;

export const authReducer = authSlice.reducer;

0 comments on commit 33574e7

Please sign in to comment.