-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { yupResolver } from '@hookform/resolvers/yup'; | ||
import React, { useContext } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { FaEnvelope, FaEye, FaEyeSlash } from 'react-icons/fa'; | ||
import { Redirect, RouteComponentProps } from 'react-router-dom'; | ||
import * as yup from 'yup'; | ||
import UserContext from '../../hooks/UserContext'; | ||
import paralinkApi from '../../services/interceptor'; | ||
import './Login.scss'; | ||
|
@@ -9,24 +12,36 @@ interface LoginProps { | |
from: string; | ||
} | ||
|
||
type LoginFormData = { | ||
email: string; | ||
password: string; | ||
}; | ||
|
||
const loginSchema = yup.object().shape({ | ||
email: yup.string().email().required(), | ||
password: yup.string().required(), // We could define rules here for password if there are | ||
}); | ||
|
||
const Login = (props: RouteComponentProps<{}, {}, LoginProps>): JSX.Element => { | ||
const userContext = useContext(UserContext); | ||
const { register, handleSubmit, errors } = useForm<LoginFormData>({ resolver: yupResolver(loginSchema) }); | ||
|
||
const [state, setState] = React.useState({ | ||
email: '', | ||
password: '', | ||
// email: '', | ||
// password: '', | ||
seePassword: false, | ||
}); | ||
|
||
const { from } = props.location.state || { from: { pathname: '/' } }; | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
const { id, value } = e.target; | ||
setState((prevState) => ({ | ||
...prevState, | ||
[id]: value, | ||
})); | ||
}; | ||
// const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
// const { id, value } = e.target; | ||
// setState((prevState) => ({ | ||
// ...prevState, | ||
// [id]: value, | ||
// })); | ||
// console.log('errors', errors); | ||
// }; | ||
|
||
const togglePassword = (): void => { | ||
setState((prevState) => ({ | ||
|
@@ -35,11 +50,14 @@ const Login = (props: RouteComponentProps<{}, {}, LoginProps>): JSX.Element => { | |
})); | ||
}; | ||
|
||
const login = async (): Promise<any> => { | ||
const login = async (data: LoginFormData): Promise<any> => { | ||
// Avoid the page to refresh from submitting the form | ||
// event.preventDefault(); | ||
// TODO: put like a loading icon on the login button | ||
|
||
// TODO: do like a safe check on the email & password here | ||
|
||
console.log('data', data); | ||
// Just to check | ||
await paralinkApi | ||
// For now this is going to be the placeholder | ||
|
@@ -66,41 +84,46 @@ const Login = (props: RouteComponentProps<{}, {}, LoginProps>): JSX.Element => { | |
return ( | ||
<div className="login__card"> | ||
<div className="login__title">Login into your account</div> | ||
<div className="login__input-container"> | ||
<input | ||
type="email" | ||
className="login__email login__input" | ||
id="email" | ||
aria-describedby="emailHelp" | ||
placeholder="[email protected]" | ||
value={state.email} | ||
onChange={handleChange} | ||
/> | ||
<div className="login__input-icon"> | ||
<FaEnvelope className="login__email-icon" /> | ||
<form onSubmit={handleSubmit(login)}> | ||
<div className="login__input-container"> | ||
<input | ||
type="email" | ||
className={`login__email login__input ${errors.email?.message ? 'login__input--error' : ''}`} | ||
ref={register({ required: true })} | ||
id="email" | ||
aria-describedby="emailHelp" | ||
placeholder="[email protected]" | ||
name="email" | ||
/> | ||
<div className="login__input-icon"> | ||
<FaEnvelope className="login__email-icon" /> | ||
</div> | ||
</div> | ||
{errors.email?.message ? <span className="login__input-error-msg">{errors.email?.message}</span> : ''} | ||
|
||
<div className="login__input-container"> | ||
<input | ||
type={state.seePassword ? 'text' : 'password'} | ||
className={`login__password login__input ${errors.email?.message ? 'login__input--error' : ''}`} | ||
ref={register({ required: true })} | ||
id="password" | ||
placeholder="Password" | ||
name="password" | ||
/> | ||
|
||
<button className="login__input-icon" type="button" onClick={togglePassword}> | ||
{state.seePassword ? ( | ||
<FaEyeSlash className="login__password-icon" /> | ||
) : ( | ||
<FaEye className="login__password-icon" /> | ||
)} | ||
</button> | ||
</div> | ||
</div> | ||
<div className="login__input-container"> | ||
<input | ||
type={state.seePassword ? 'text' : 'password'} | ||
className="login__password login__input" | ||
id="password" | ||
placeholder="Password" | ||
value={state.password} | ||
onChange={handleChange} | ||
/> | ||
|
||
<button className="login__input-icon" type="button" onClick={togglePassword}> | ||
{state.seePassword ? ( | ||
<FaEyeSlash className="login__password-icon" /> | ||
) : ( | ||
<FaEye className="login__password-icon" /> | ||
)} | ||
{errors.password?.message ? <span className="login__input-error-msg">{errors.password?.message}</span> : ''} | ||
<button className="login__button" type="submit"> | ||
Login | ||
</button> | ||
</div> | ||
<button className="login__button" type="submit" onClick={login}> | ||
Login | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters