From 28f77947ef67affa71e671bf2ba57ea87300c6e6 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Sat, 11 Jun 2022 00:03:06 +0200 Subject: [PATCH] feat: added initial login form state --- src/features/login/pages/LoginPage/index.tsx | 115 ++++++++++++++----- 1 file changed, 87 insertions(+), 28 deletions(-) diff --git a/src/features/login/pages/LoginPage/index.tsx b/src/features/login/pages/LoginPage/index.tsx index 6e14109..0ecd12f 100644 --- a/src/features/login/pages/LoginPage/index.tsx +++ b/src/features/login/pages/LoginPage/index.tsx @@ -1,8 +1,7 @@ import type { NextPage } from 'next' import type { FormEvent } from 'react' -import { useState } from 'react' +import { useState, useCallback } from 'react' -import { Button } from '~/features/ui/components/Button' import { Container } from '~/features/ui/components/Container' import { Input } from '~/features/ui/components/Input' import { LayoutExternal } from '~/features/ui/components/LayoutExternal' @@ -15,50 +14,110 @@ import { ErrorMessage, } from './styled' +const validators = { + email: (value: string) => { + if (typeof value !== 'string') return 'Invalid e-mail value type' + if (!value) return 'E-mail is required' + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/u.test(value)) return 'Invalid e-mail' + }, + + password: (value: string) => { + if (typeof value !== 'string') return 'Invalid password value type' + if (!value) return 'Password is required' + }, +} + export const LoginPage: NextPage = () => { - const [error, setError] = useState('') + const [email, setEmail] = useState('') + const [password, setPassword] = useState('') + + const [emailError, setEmailError] = useState(null) + const [passwordError, setPasswordError] = useState(null) + const [submitError, setSubmitError] = useState(null) + + const [isSubmitting, setIsSubmitting] = useState(false) + + /** + * Login handler. + */ + const login = useCallback( + (e: FormEvent) => { + e.preventDefault() + + const errors = { + email: validators.email(email), + password: validators.password(password), + } + + if (errors.email) { + setEmailError(errors.email) + } + + if (errors.password) { + setPasswordError(errors.password) + } + + // Only submit in case of no errors. + if (!errors.email && !errors.password) { + setIsSubmitting(true) - const onSubmit = (event: FormEvent) => { - event.preventDefault() + setTimeout(() => { + // Mocking to represent login submit outcome. + const shouldFail = Math.random() < 0.5 - alert('TODO') - } + if (shouldFail) { + setSubmitError('Something went terribly wrong!') + } else { + alert('Success!') + } + + setIsSubmitting(false) + }, 1000) + } + }, + [email, password] + ) return ( Sign in to Eventio. - {error ? ( - {error} + + {submitError ? ( + {submitError} ) : ( Enter your details below. )} -
- + + + { + setEmailError(null) + setEmail(e.target.value) + }} + /> + { + setPasswordError(null) + setPassword(e.target.value) + }} />

- Sign In -

- - {/* - Created just to showcase CSS animations. - To be removed. Please do not use style attribute. - */} -

- + + {isSubmitting ? 'Submitting' : 'Sign In'} +