Source: Login/Login.js

import React, { useState, memo } from 'react';
import { string, func, bool, oneOf } from 'prop-types';
import styled from 'styled-components';
import isEmail from 'validator/lib/isEmail';

import Fade from '@material-ui/core/Fade';
import Collapse from '@material-ui/core/Collapse';
import CssBaseline from '@material-ui/core/CssBaseline';
import CircularProgress from '@material-ui/core/CircularProgress';

import MailOutlineIcon from '@material-ui/icons/AlternateEmailOutlined';
import VpnKeyIcon from '@material-ui/icons/VpnKeyOutlined';
import ChevronRightOutlinedIcon from '@material-ui/icons/ChevronRightOutlined';
import ChevronLeftOutlinedIcon from '@material-ui/icons/ChevronLeftOutlined';
import CloseOutlinedIcon from '@material-ui/icons/CloseOutlined';
import VisibilityOutlinedIcon from '@material-ui/icons/VisibilityOutlined';

import './Login.css';

const Root = styled.div`
	text-rendering: optimizeLegibility;
	width: 100%;
	height: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 999999;

	&,
	&::before {
		position: fixed;
		bottom: 0;
		right: 0;
		left: 0;
	}

	&::before {
		top: 0;
		background: radial-gradient(#40404b, #111118) rgba(34, 34, 40, 0.94);
		z-index: -1;
		opacity: 0.9;
		content: '';
	}
`;

const Container = styled.div`
	width: 300px;
	border-radius: 5px;
`;

const Content = styled.div`
	background: #fff;
	border-radius: 6px;
	position: relative;
	display: flex;
	flex-direction: column;
	height: 100%;
	min-height: 100%;
	max-height: calc(100vh - 40px);
`;

const CloseButton = styled.div`
	box-sizing: content-box !important;
	background: #fff;
	border-radius: 100px;
	height: 14px;
	width: 14px;
	padding: 0;
	position: absolute;
	top: 14px;
	right: 14px;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
	color: #333;
	z-index: 100;
	border: 6px solid #fff;
	cursor: pointer;
	line-height: 0;

	& svg {
		width: auto;
		height: auto;
	}
`;

const BackButton = styled(CloseButton)`
	left: 14px;
`;

const Header = styled.div`
	text-align: center;
	padding: 11px;
	color: #333;
	position: relative;
	background: rgba(220, 220, 220, 0.5);
	border-radius: 5px 5px 0 0;
	overflow: hidden;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
`;

const Logo = styled.img`
	width: auto;
	height: 58px;
	margin: 0 0 11px;
	transition: margin-top 0.4s;
`;

const HeaderText = styled.div`
	font-size: 22px;
	padding: 0 10px;
	line-height: 30px;
`;

const Body = styled.div`
	padding: 20px;
	display: flex;
	flex-direction: column;
	justify-content: center;
`;

const InputWrapper = styled.div`
	border-radius: 3px;
	border: 1px solid #f1f1f1;
	position: relative;
	background: #f1f1f1;
	transition: border-color 0.8s;
	display: flex;
	justify-content: center;
	align-items: center;
	position: relative;

	${(props) => props.isWrong && `border-color: orange;`}
`;

const InputAction = styled.div`
	position: absolute;
	right: 8px;
`;

const InputIcon = styled.div`
	flex-basis: 20%;
	display: flex;
	justify-content: center;
	align-items: center;

	svg {
		width: 16px;
		height: 16px;
		fill: #a7a7a7;
	}
`;

const Input = styled.input`
	border: 0;
	padding: 0 14px;
	right: 0;
	height: 40px;
	font-size: 13px;
	width: 100%;
	border-radius: 0 2px 2px 0;
	color: rgba(0, 0, 0, 0.87);

	&:focus {
		outline: none;
	}
`;

const RecoveryPwdText = styled.a`
	font-size: 14px;
	color: rgba(0, 0, 0, 0.87);
	cursor: pointer;
	margin-bottom: 0;
	text-decoration: none;
	text-align: center;

	&:focus,
	&:active {
		outline: none;
	}
`;

const Footer = styled.button`
	background-color: rgb(234, 83, 35);
	border: 0;
	padding: 28px 14px;
	width: 100%;
	overflow: hidden;
	border-radius: 0 0 5px 5px;
	transition: 0.2s ease-in-out;
	color: #fff;
	letter-spacing: 1px;
	font-size: 14px;
	text-transform: uppercase;
	display: flex;
	align-items: center;
	justify-content: center;

	&:disabled {
		background-color: rgba(128, 128, 128, 0.8);
	}
`;

const ResultsMessage = styled.div`
	color: #fff;
	text-align: center;
	padding: 10px;
	line-height: 1.8;
	font-size: 11px;
	font-weight: 600;
	text-transform: uppercase;

	${(props) => props.isFail && `background-color: #ff3e00;`}
	${(props) => props.isSuccess && `background-color: #9acd46;`}
`;

/**
 * @param {Object} props
 * @param {boolean} props.isWrong
 */
function EmailInput({ isWrong, ...rest }) {
	return (
		<InputWrapper isWrong={isWrong}>
			<InputIcon>
				<MailOutlineIcon />
			</InputIcon>
			<Input type='text' placeholder='email@esempio.com' autoComplete='on' autoCapitalize='off' autoFocus {...rest} />
		</InputWrapper>
	);
}

EmailInput.propTypes = {
	isWrong: bool
};

/**
 * @param {Object} props
 */
function PwdInput({ ...rest }) {
	const [canShowPwd, changeCanShowPwd] = useState(false);

	return (
		<InputWrapper>
			<InputIcon>
				<VpnKeyIcon />
			</InputIcon>
			<Input type={canShowPwd ? 'text' : 'password'} placeholder='la tua password' autoComplete='off' autoCapitalize='off' {...rest} />
			<InputAction onClick={() => changeCanShowPwd(!canShowPwd)}>
				<div className='flex'>
					<VisibilityOutlinedIcon className='pointer' style={{ fill: '#a7a7a7', width: 18, height: 18 }} />
				</div>
			</InputAction>
		</InputWrapper>
	);
}

/**
 * @param {Object} props
 * @param {string} props.logoUrl
 * @param {string} props.headerText
 * @param {boolean} props.showRecoveryPwd
 * @param {string} props.variant
 * @param {boolean} props.isOpen
 * @param {boolean} props.showClose
 * @param {boolean} props.onLogin
 * @param {function} props.onClose
 * @param {function} props.onRecovery
 */
function RecoveryForm({ logoUrl, onRecovery }) {
	const [recoveryEmail, changeRecoveryEmail] = useState('');
	const [isProcessing, changeIsProcessing] = useState(false);
	const [isLoginFail, changeIsLoginFail] = useState(false);
	const [isLoginSuccess, changeIsLoginSuccess] = useState(false);

	const canSubmit = isEmail(recoveryEmail) && !isProcessing;
	const isWrongEmail = recoveryEmail.length > 0 && !isEmail(recoveryEmail);

	const handleRecovery = (e) => {
		e.preventDefault();

		if (!onRecovery) {
			return;
		}

		changeIsProcessing(true);

		onRecovery(recoveryEmail)
			.then(() => {
				changeIsLoginSuccess(true);
				changeIsLoginFail(false);
			})
			.catch(() => {
				changeIsLoginSuccess(false);
				changeIsLoginFail(true);
			})
			.finally(() => changeIsProcessing(false));
	};

	return (
		<form id='recovery-form'>
			<Header>
				<Fade in timeout={800}>
					<Logo src={logoUrl} />
				</Fade>
				<Fade in timeout={1000}>
					<HeaderText>Reimposta la tua password</HeaderText>
				</Fade>
			</Header>
			<Collapse in={isLoginFail}>
				<ResultsMessage isFail>Email inesistente</ResultsMessage>
			</Collapse>
			<Collapse in={isLoginSuccess}>
				<ResultsMessage isSuccess>Email inviata correttamente</ResultsMessage>
			</Collapse>
			<Collapse in={!isLoginSuccess} timeout={800}>
				<Body>
					<EmailInput isWrong={isWrongEmail} disabled={isProcessing} onChange={(e) => changeRecoveryEmail(e.target.value)} type='email' />
				</Body>
			</Collapse>
			<Collapse in={!isLoginSuccess} timeout={500}>
				<Footer type='submit' onClick={handleRecovery} disabled={!canSubmit}>
					{isProcessing ? (
						<CircularProgress color='secondary' size={21} />
					) : (
						<>
							<span>Invia email</span>
							<div style={{ marginLeft: 4 }}>
								<ChevronRightOutlinedIcon />
							</div>
						</>
					)}
				</Footer>
			</Collapse>
		</form>
	);
}

RecoveryForm.propTypes = {
	logoUrl: string,
	headerText: string,
	showRecoveryPwd: bool,
	showClose: bool,
	variant: oneOf(['email', 'username']),
	onClose: func,
	onLogin: func,
	onRecovery: func
};

const DEFAULT_CREDENTIALS = {
	email: '',
	pwd: ''
};

/**
 * LoginForm component
 *
 * @param {Object} props
 * @param {string} props.logoUrl
 * @param {string} props.headerText
 * @param {boolean} props.showRecoveryPwd
 * @param {string} props.variant
 * @param {boolean} props.isOpen
 * @param {boolean} props.showClose
 * @param {boolean} props.onLogin
 * @param {function} props.onClose
 * @param {function} props.onRecovery
 * @param {function} props.onShowRecoveryForm
 */
function LoginForm({ logoUrl, headerText, showRecoveryPwd, onShowRecoveryForm, variant, onLogin }) {
	const [credentials, changeCredentials] = useState(DEFAULT_CREDENTIALS);
	const [isProcessing, changeIsProcessing] = useState(false);
	const [isLoginFail, changeIsLoginFail] = useState(false);
	const [isLoginSuccess, changeIsLoginSuccess] = useState(false);

	const validCredentials = () => {
		let canSubmit = credentials.email.length > 0 && credentials.pwd.length > 0 && !isProcessing;
		let isWrongEmail = false;

		switch (variant) {
			case 'email': {
				canSubmit = isEmail(credentials.email) && credentials.pwd.length > 0 && !isProcessing;
				isWrongEmail = credentials.email.length > 0 && !isEmail(credentials.email);
				break;
			}
			default:
				break;
		}

		return {
			canSubmit,
			isWrongEmail
		};
	};

	const handleLogin = (e) => {
		e.preventDefault();

		if (!onLogin) {
			return;
		}

		changeIsProcessing(true);

		onLogin(credentials)
			.then(() => {
				changeIsLoginSuccess(true);
				changeIsLoginFail(false);
			})
			.catch(() => {
				changeIsLoginSuccess(false);
				changeIsLoginFail(true);
			})
			.finally(() => changeIsProcessing(false));
	};

	const credentialsCheck = validCredentials();

	return (
		<form id='login-form'>
			<Header>
				<Fade in timeout={800}>
					<Logo src={logoUrl} />
				</Fade>
				<Fade in timeout={1000}>
					<HeaderText>{headerText}</HeaderText>
				</Fade>
			</Header>
			<Collapse in={isLoginFail}>
				<ResultsMessage isFail>EMAIL O PASSWORD SBAGLIATA.</ResultsMessage>
			</Collapse>
			<Collapse in={isLoginSuccess}>
				<ResultsMessage isSuccess>Accesso effettuato con successo.</ResultsMessage>
			</Collapse>
			<Collapse in={!isLoginSuccess} timeout={800}>
				<Body>
					<EmailInput
						isWrong={credentialsCheck.isWrongEmail}
						disabled={isProcessing}
						onChange={(e) => changeCredentials({ ...credentials, email: e.target.value })}
						type={variant === 'email' ? 'email' : 'text'}
					/>
					<div style={{ marginTop: 12 }}>
						<PwdInput disabled={isProcessing} onChange={(e) => changeCredentials({ ...credentials, pwd: e.target.value })} />
					</div>
					{showRecoveryPwd && (
						<div style={{ marginTop: 16, textAlign: 'center' }}>
							<RecoveryPwdText
								onClick={(e) => {
									e.preventDefault();
									onShowRecoveryForm(true);
								}}
							>
								Non ricordi la password?
							</RecoveryPwdText>
						</div>
					)}
				</Body>
			</Collapse>
			<Collapse in={!isLoginSuccess} timeout={500}>
				<Footer type='submit' onClick={handleLogin} disabled={!credentialsCheck.canSubmit}>
					{isProcessing ? (
						<CircularProgress color='secondary' size={21} />
					) : (
						<>
							<span>Accesso</span>
							<div style={{ marginLeft: 4 }}>
								<ChevronRightOutlinedIcon />
							</div>
						</>
					)}
				</Footer>
			</Collapse>
		</form>
	);
}

LoginForm.propTypes = {
	onShowRecoveryForm: func,
	logoUrl: string,
	headerText: string,
	showRecoveryPwd: bool,
	showClose: bool,
	variant: oneOf(['email', 'username']),
	onClose: func,
	onLogin: func,
	onRecovery: func
};

/**
 * Login component
 *
 * @param {Object} props
 * @param {string} props.logoUrl
 * @param {string} props.headerText
 * @param {boolean} props.showRecoveryPwd
 * @param {string} props.variant
 * @param {boolean} props.isOpen
 * @param {boolean} props.showClose
 * @param {boolean} props.onLogin
 * @param {function} props.onClose
 * @param {function} props.onRecovery
 */
export function Login({ isOpen, onClose, showClose, ...rest }) {
	const [showRecoveryForm, changeShowRecoveryForm] = useState(false);

	return (
		<CssBaseline>
			<Root isOpen={isOpen}>
				<Fade in timeout={500}>
					<Container>
						<Content>
							{showClose && (
								<Fade in timeout={1000}>
									<CloseButton onClick={onClose}>
										<CloseOutlinedIcon />
									</CloseButton>
								</Fade>
							)}
							{showRecoveryForm && (
								<Fade in timeout={1000}>
									<BackButton onClick={() => changeShowRecoveryForm(false)}>
										<ChevronLeftOutlinedIcon />
									</BackButton>
								</Fade>
							)}
							<Collapse in={showRecoveryForm}>
								<RecoveryForm {...rest} />
							</Collapse>
							<Collapse in={!showRecoveryForm}>
								<LoginForm onShowRecoveryForm={changeShowRecoveryForm} {...rest} />
							</Collapse>
						</Content>
					</Container>
				</Fade>
			</Root>
		</CssBaseline>
	);
}

Login.propTypes = {
	logoUrl: string,
	headerText: string,
	showRecoveryPwd: bool,
	showClose: bool,
	variant: oneOf(['email', 'username']),
	onClose: func,
	onLogin: func,
	onRecovery: func
};

Login.defaultProps = {
	logoUrl: 'https://sigeosrl.com/wp-content/uploads/2018/07/sigeo-dark-simple.png',
	headerText: 'Powered by Sigeo',
	showRecoveryPwd: true,
	showClose: true,
	variant: 'email'
};

export default memo(Login);