webapp/src/components/Nav/AuthModal/LoginForm.tsx

233 lines
6.8 KiB
TypeScript
Raw Normal View History

import type { AuthModalSearchParams } from './types'
2022-10-21 18:17:04 +00:00
import { clsx } from 'clsx'
2022-10-31 16:40:55 +00:00
import { createSignal, Show } from 'solid-js'
import { useLocalize } from '../../../context/localize'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../../context/session'
import { useSnackbar } from '../../../context/snackbar'
2022-11-14 10:02:08 +00:00
import { signSendLink } from '../../../stores/auth'
import { useRouter } from '../../../stores/router'
import { hideModal } from '../../../stores/ui'
import { ApiError } from '../../../utils/apiClient'
import { validateEmail } from '../../../utils/validateEmail'
import { Icon } from '../../_shared/Icon'
import { AuthModalHeader } from './AuthModalHeader'
import { email, setEmail } from './sharedLogic'
import { SocialProviders } from './SocialProviders'
import styles from './AuthModal.module.scss'
2022-10-21 18:17:04 +00:00
type FormFields = {
email: string
password: string
}
type ValidationErrors = Partial<Record<keyof FormFields, string>>
export const LoginForm = () => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
2022-10-21 18:17:04 +00:00
const [submitError, setSubmitError] = createSignal('')
const [isSubmitting, setIsSubmitting] = createSignal(false)
const [validationErrors, setValidationErrors] = createSignal<ValidationErrors>({})
2022-10-25 16:25:42 +00:00
// TODO: better solution for interactive error messages
const [isEmailNotConfirmed, setIsEmailNotConfirmed] = createSignal(false)
const [isLinkSent, setIsLinkSent] = createSignal(false)
const [showPassword, setShowPassword] = createSignal(false)
2022-10-21 18:17:04 +00:00
const authFormRef: { current: HTMLFormElement } = { current: null }
2023-02-10 11:11:24 +00:00
const {
actions: { showSnackbar },
2023-02-10 11:11:24 +00:00
} = useSnackbar()
const {
actions: { signIn },
2022-11-14 10:02:08 +00:00
} = useSession()
2022-10-21 18:17:04 +00:00
const { changeSearchParam } = useRouter<AuthModalSearchParams>()
const [password, setPassword] = createSignal('')
const handleEmailInput = (newEmail: string) => {
setValidationErrors(({ email: _notNeeded, ...rest }) => rest)
setEmail(newEmail)
}
const handlePasswordInput = (newPassword: string) => {
setValidationErrors(({ password: _notNeeded, ...rest }) => rest)
setPassword(newPassword)
}
2022-11-02 07:25:36 +00:00
const handleSendLinkAgainClick = async (event: Event) => {
2022-11-01 22:25:18 +00:00
event.preventDefault()
setIsLinkSent(true)
2022-11-01 22:25:18 +00:00
setIsEmailNotConfirmed(false)
setSubmitError('')
2023-02-17 09:21:02 +00:00
const result = await signSendLink({ email: email(), lang: lang(), template: 'email_confirmation' })
2022-11-02 07:25:36 +00:00
if (result.error) setSubmitError(result.error)
2022-11-01 22:25:18 +00:00
}
2022-10-21 18:17:04 +00:00
const handleSubmit = async (event: Event) => {
event.preventDefault()
2022-10-25 16:25:42 +00:00
setIsLinkSent(false)
2022-11-14 01:17:12 +00:00
setIsEmailNotConfirmed(false)
2022-10-21 18:17:04 +00:00
setSubmitError('')
const newValidationErrors: ValidationErrors = {}
if (!email()) {
newValidationErrors.email = t('Please enter email')
} else if (!validateEmail(email())) {
2022-10-21 18:17:04 +00:00
newValidationErrors.email = t('Invalid email')
}
if (!password()) {
newValidationErrors.password = t('Please enter password')
}
if (Object.keys(newValidationErrors).length > 0) {
setValidationErrors(newValidationErrors)
authFormRef.current
.querySelector<HTMLInputElement>(`input[name="${Object.keys(newValidationErrors)[0]}"]`)
.focus()
2022-10-21 18:17:04 +00:00
return
}
setIsSubmitting(true)
try {
await signIn({ email: email(), password: password() })
2022-10-25 16:25:42 +00:00
hideModal()
2023-02-10 11:11:24 +00:00
showSnackbar({ body: t('Welcome!') })
2022-10-21 18:17:04 +00:00
} catch (error) {
if (error instanceof ApiError) {
if (error.code === 'email_not_confirmed') {
setSubmitError(t('Please, confirm email'))
2022-10-25 16:25:42 +00:00
setIsEmailNotConfirmed(true)
2022-10-21 18:17:04 +00:00
return
}
if (error.code === 'user_not_found') {
setSubmitError(t('Something went wrong, check email and password'))
2022-10-21 18:17:04 +00:00
return
}
}
setSubmitError(error.message)
} finally {
setIsSubmitting(false)
}
}
return (
<form onSubmit={handleSubmit} class={styles.authForm} ref={(el) => (authFormRef.current = el)}>
2023-05-18 20:02:19 +00:00
<div>
<AuthModalHeader modalType="login" />
2023-05-18 20:02:19 +00:00
<Show when={submitError()}>
<div class={styles.authInfo}>
<div class={styles.warn}>{submitError()}</div>
<Show when={isEmailNotConfirmed()}>
<a href="#" onClick={handleSendLinkAgainClick}>
{t('Send link again')}
</a>
</Show>
</div>
</Show>
<Show when={isLinkSent()}>
<div class={styles.authInfo}>{t('Link sent, check your email')}</div>
</Show>
<div
class={clsx('pretty-form__item', {
'pretty-form__item--error': validationErrors().email,
})}
>
2023-05-18 20:02:19 +00:00
<input
id="email"
name="email"
autocomplete="email"
type="email"
value={email()}
placeholder={t('Email')}
onInput={(event) => handleEmailInput(event.currentTarget.value)}
/>
<label for="email">{t('Email')}</label>
2023-08-30 21:30:15 +00:00
<Show when={validationErrors().email}>
<div class={styles.validationError}>{validationErrors().email}</div>
</Show>
2023-05-18 20:02:19 +00:00
</div>
<div
class={clsx('pretty-form__item', {
'pretty-form__item--error': validationErrors().password,
})}
>
2023-05-18 20:02:19 +00:00
<input
id="password"
name="password"
autocomplete="password"
type={showPassword() ? 'text' : 'password'}
placeholder={t('Password')}
onInput={(event) => handlePasswordInput(event.currentTarget.value)}
/>
<label for="password">{t('Password')}</label>
<button
type="button"
class={styles.passwordToggle}
onClick={() => setShowPassword(!showPassword())}
>
<Icon class={styles.passwordToggleIcon} name={showPassword() ? 'eye-off' : 'eye'} />
</button>
2023-08-30 21:30:15 +00:00
<Show when={validationErrors().password}>
<div class={styles.validationError}>{validationErrors().password}</div>
</Show>
2022-10-21 18:17:04 +00:00
</div>
2023-05-18 20:02:19 +00:00
<div>
<button class={clsx('button', styles.submitButton)} disabled={isSubmitting()} type="submit">
{isSubmitting() ? '...' : t('Enter')}
</button>
</div>
<div class={styles.authActions}>
<span
class="link"
onClick={() =>
changeSearchParam({
mode: 'forgot-password',
})
}
2023-05-18 20:02:19 +00:00
>
{t('Forgot password?')}
</span>
2023-05-18 20:02:19 +00:00
</div>
2022-10-21 18:17:04 +00:00
</div>
2023-05-18 20:02:19 +00:00
<div>
<SocialProviders />
2022-10-21 18:17:04 +00:00
2023-05-18 20:02:19 +00:00
<div class={styles.authControl}>
<span
class={styles.authLink}
onClick={() =>
changeSearchParam({
mode: 'register',
})
}
>
2023-05-18 20:02:19 +00:00
{t('I have no account yet')}
</span>
</div>
2022-10-21 18:17:04 +00:00
</div>
</form>
)
}