import React, { useState, memo } from 'react'; 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<{ isOpen?: boolean }>` 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; ${({ isWrong }: InputProps) => 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; ${({ isFail }: IResultsMessageProps) => isFail && 'background-color: #ff3e00;'} ${({ isSuccess }: IResultsMessageProps) => isSuccess && 'background-color: #9acd46;'} `; type VARIANTS = 'email' | 'username'; interface IResultsMessageProps { isFail?: boolean; isSuccess?: boolean; } interface ICredentialsProps { email: string; pwd: string; } interface IFormProps { logoUrl?: string; headerText?: string; onLogin: (props: ICredentialsProps) => any; onRecovery?: (email: string) => any; variant?: VARIANTS; } interface IHTMLInputProps { disabled?: boolean; onChange?: (value: any) => any; } interface IRecoveryFormProps extends IFormProps {} interface ILoginFormProps extends IFormProps { showRecoveryPwd?: boolean; onShowRecoveryForm?: (showed: boolean) => any; } interface ILoginProps extends IFormProps { isOpen: boolean; onClose: () => any; showClose?: boolean; } interface InputProps { isWrong?: boolean; } interface IEmailInputProps extends InputProps, IHTMLInputProps { type?: string; } interface IPwdInputProps extends IHTMLInputProps {} function EmailInput({ isWrong, ...rest }: IEmailInputProps) { return ( ); } function PwdInput({ ...rest }: IPwdInputProps) { const [canShowPwd, changeCanShowPwd] = useState(false); return ( changeCanShowPwd(!canShowPwd)}>
); } function RecoveryForm({ logoUrl, onRecovery }: IRecoveryFormProps) { 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: any) => { e.preventDefault(); if (!onRecovery) { return; } changeIsProcessing(true); onRecovery(recoveryEmail) .then(() => { changeIsLoginSuccess(true); changeIsLoginFail(false); }) .catch(() => { changeIsLoginSuccess(false); changeIsLoginFail(true); }) .finally(() => changeIsProcessing(false)); }; return (
Reimposta la tua password
Email inesistente Email inviata correttamente changeRecoveryEmail(e.target.value)} type='email' />
{isProcessing ? ( ) : ( <> Invia email
)}
); } const DEFAULT_CREDENTIALS: ICredentialsProps = { email: '', pwd: '' }; function LoginForm({ logoUrl, headerText, showRecoveryPwd, onShowRecoveryForm, variant, onLogin }: ILoginFormProps) { const [credentials, changeCredentials] = useState(DEFAULT_CREDENTIALS); const [isProcessing, changeIsProcessing] = useState(false); const [isLoginFail, changeIsLoginFail] = useState(false); const [isLoginSuccess, changeIsLoginSuccess] = useState(false); const defVariant = variant ?? 'email'; const validCredentials = () => { let canSubmit = credentials.email.length > 0 && credentials.pwd.length > 0 && !isProcessing; let isWrongEmail = false; switch (defVariant) { 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: any) => { 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 (
{headerText ?? 'Powered by Sigeo'}
EMAIL O PASSWORD SBAGLIATA. Accesso effettuato con successo. changeCredentials({ ...credentials, email: e.target.value }) } type={defVariant === 'email' ? 'email' : 'text'} />
changeCredentials({ ...credentials, pwd: e.target.value }) } />
{showRecoveryPwd && (
{ e.preventDefault(); onShowRecoveryForm?.(true); }} > Non ricordi la password?
)}
{isProcessing ? ( ) : ( <> Accesso
)}
); } export function Login({ isOpen, onClose, showClose, ...rest }: ILoginProps) { const [showRecoveryForm, changeShowRecoveryForm] = useState(false); return ( {showClose && ( )} {showRecoveryForm && ( changeShowRecoveryForm(false)}> )} ); } export default memo(Login);