|
@@ -1,9 +1,9 @@
|
|
-import { useEffect } from 'react';
|
|
|
|
|
|
+import { useEffect, useState } from 'react';
|
|
import hookForm from 'hook-easy-form';
|
|
import hookForm from 'hook-easy-form';
|
|
import { useHistory } from 'react-router-dom';
|
|
import { useHistory } from 'react-router-dom';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
|
-import { runAuth } from 'store/auth/actions';
|
|
|
|
|
|
+import { runAuth, runRegister } from 'store/auth/actions';
|
|
import Input from 'components/inputs/text-input/text-input';
|
|
import Input from 'components/inputs/text-input/text-input';
|
|
|
|
|
|
import classes from './auth.module.scss';
|
|
import classes from './auth.module.scss';
|
|
@@ -30,21 +30,33 @@ const form = [
|
|
maxLength: (v) => (v.trim().length < 6 ? 'Invalid' : ''),
|
|
maxLength: (v) => (v.trim().length < 6 ? 'Invalid' : ''),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
-]
|
|
|
|
|
|
+];
|
|
|
|
|
|
const Auth = () => {
|
|
const Auth = () => {
|
|
const dispatch = useDispatch();
|
|
const dispatch = useDispatch();
|
|
const history = useHistory();
|
|
const history = useHistory();
|
|
|
|
|
|
- const user = useSelector((s) => s.auth.user)
|
|
|
|
- console.log('user', user);
|
|
|
|
|
|
+ const [auth, setAuth] = useState(true);
|
|
|
|
+ const { formArray, updateEvent, valid, disabled, submitEvent, setValueManually } = hookForm({ initialForm: form })
|
|
|
|
+ console.log('formArray', formArray);
|
|
|
|
+ const user = useSelector((s) => s.auth.user);
|
|
|
|
+ const emailFromRegister = useSelector((s) => s.auth.emailFromRegister);
|
|
|
|
+
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (user) history.push('/');
|
|
if (user) history.push('/');
|
|
}, [user, history]);
|
|
}, [user, history]);
|
|
|
|
|
|
- const { formArray, updateEvent, valid, disabled, submitEvent } = hookForm({ initialForm: form })
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (emailFromRegister) {
|
|
|
|
+ setValueManually('password', '');
|
|
|
|
+ setAuth(true);
|
|
|
|
+ }
|
|
|
|
+ }, [emailFromRegister, setValueManually])
|
|
|
|
|
|
- const submit = submitEvent((v) => dispatch(runAuth(v)));
|
|
|
|
|
|
+ const submit = submitEvent((v) => {
|
|
|
|
+ if (auth) dispatch(runAuth(v))
|
|
|
|
+ else dispatch(runRegister(v))
|
|
|
|
+ });
|
|
|
|
|
|
return (
|
|
return (
|
|
<div className={classes.container}>
|
|
<div className={classes.container}>
|
|
@@ -52,6 +64,7 @@ const Auth = () => {
|
|
{formArray.map(el => (
|
|
{formArray.map(el => (
|
|
<div className={classes.form} key={el.name}>
|
|
<div className={classes.form} key={el.name}>
|
|
<Input
|
|
<Input
|
|
|
|
+ value={el.value}
|
|
name={el.name}
|
|
name={el.name}
|
|
label={el.options.label}
|
|
label={el.options.label}
|
|
type={el.options.type}
|
|
type={el.options.type}
|
|
@@ -60,6 +73,8 @@ const Auth = () => {
|
|
))}
|
|
))}
|
|
<button type="submit" disabled={disabled || !valid}>Submit</button>
|
|
<button type="submit" disabled={disabled || !valid}>Submit</button>
|
|
</form>
|
|
</form>
|
|
|
|
+
|
|
|
|
+ <p className={classes.text} onClick={() => setAuth(p => !p)}>{auth ? 'Do you have a account ?' : 'Log In'}</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
)
|
|
}
|
|
}
|