2022-10-31 16:40:55 +00:00
|
|
|
import { Show, createSignal } from 'solid-js'
|
2022-10-25 16:25:42 +00:00
|
|
|
import type { JSX } from 'solid-js'
|
|
|
|
import styles from './AuthModal.module.scss'
|
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import { SocialProviders } from './SocialProviders'
|
|
|
|
import { ApiError } from '../../../utils/apiClient'
|
|
|
|
import { email, setEmail } from './sharedLogic'
|
|
|
|
import { useRouter } from '../../../stores/router'
|
|
|
|
import type { AuthModalSearchParams } from './types'
|
|
|
|
import { hideModal } from '../../../stores/ui'
|
2022-11-13 19:35:57 +00:00
|
|
|
import { checkEmail, useEmailChecks } from '../../../stores/emailChecks'
|
2022-11-14 10:02:08 +00:00
|
|
|
import { register } from '../../../stores/auth'
|
2023-02-17 09:21:02 +00:00
|
|
|
import { useLocalize } from '../../../context/localize'
|
2023-07-02 05:08:42 +00:00
|
|
|
import { validateEmail } from '../../../utils/validateEmail'
|
2023-07-30 12:31:54 +00:00
|
|
|
import { AuthModalHeader } from './AuthModalHeader'
|
2023-08-21 11:11:18 +00:00
|
|
|
import { Icon } from '../../_shared/Icon'
|
2022-10-25 16:25:42 +00:00
|
|
|
|
|
|
|
type FormFields = {
|
2023-08-13 13:36:18 +00:00
|
|
|
fullName: string
|
2022-10-25 16:25:42 +00:00
|
|
|
email: string
|
|
|
|
password: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValidationErrors = Partial<Record<keyof FormFields, string | JSX.Element>>
|
|
|
|
|
|
|
|
export const RegisterForm = () => {
|
|
|
|
const { changeSearchParam } = useRouter<AuthModalSearchParams>()
|
2023-02-17 09:21:02 +00:00
|
|
|
const { t } = useLocalize()
|
2022-11-13 19:35:57 +00:00
|
|
|
const { emailChecks } = useEmailChecks()
|
2022-10-25 16:25:42 +00:00
|
|
|
|
|
|
|
const [submitError, setSubmitError] = createSignal('')
|
2023-08-13 13:36:18 +00:00
|
|
|
const [fullName, setFullName] = createSignal('')
|
2022-10-25 16:25:42 +00:00
|
|
|
const [password, setPassword] = createSignal('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = createSignal(false)
|
2023-08-21 11:11:18 +00:00
|
|
|
const [showPassword, setShowPassword] = createSignal(false)
|
2022-10-25 16:25:42 +00:00
|
|
|
const [isSuccess, setIsSuccess] = createSignal(false)
|
|
|
|
const [validationErrors, setValidationErrors] = createSignal<ValidationErrors>({})
|
|
|
|
|
2023-08-13 13:36:18 +00:00
|
|
|
const authFormRef: { current: HTMLFormElement } = { current: null }
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
const handleEmailInput = (newEmail: string) => {
|
|
|
|
setEmail(newEmail)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleEmailBlur = () => {
|
2023-07-02 05:08:42 +00:00
|
|
|
if (validateEmail(email())) {
|
2022-10-25 16:25:42 +00:00
|
|
|
checkEmail(email())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:03:46 +00:00
|
|
|
function isValidPassword(passwordToCheck) {
|
2023-05-11 17:20:40 +00:00
|
|
|
const minLength = 8
|
|
|
|
const hasNumber = /\d/
|
2023-05-12 13:03:46 +00:00
|
|
|
const hasSpecial = /[!#$%&*@^]/
|
2023-05-11 17:20:40 +00:00
|
|
|
|
2023-05-12 13:03:46 +00:00
|
|
|
if (passwordToCheck.length < minLength) {
|
2023-05-11 17:20:40 +00:00
|
|
|
return t('Password should be at least 8 characters')
|
|
|
|
}
|
2023-05-12 13:03:46 +00:00
|
|
|
if (!hasNumber.test(passwordToCheck)) {
|
2023-05-11 17:20:40 +00:00
|
|
|
return t('Password should contain at least one number')
|
|
|
|
}
|
2023-05-12 13:03:46 +00:00
|
|
|
if (!hasSpecial.test(passwordToCheck)) {
|
2023-05-11 17:20:40 +00:00
|
|
|
return t('Password should contain at least one special character: !@#$%^&*')
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
const handlePasswordInput = (newPassword: string) => {
|
|
|
|
setPassword(newPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNameInput = (newPasswordCopy: string) => {
|
2023-08-13 13:36:18 +00:00
|
|
|
setFullName(newPasswordCopy)
|
2022-10-25 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleSubmit = async (event: Event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2023-08-21 11:11:18 +00:00
|
|
|
const passwordError = isValidPassword(password())
|
|
|
|
if (passwordError) {
|
|
|
|
setValidationErrors((errors) => ({ ...errors, password: passwordError }))
|
|
|
|
} else {
|
|
|
|
setValidationErrors(({ password: _notNeeded, ...rest }) => rest)
|
|
|
|
}
|
|
|
|
setValidationErrors(({ email: _notNeeded, ...rest }) => rest)
|
|
|
|
setValidationErrors(({ fullName: _notNeeded, ...rest }) => rest)
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
setSubmitError('')
|
|
|
|
|
|
|
|
const newValidationErrors: ValidationErrors = {}
|
|
|
|
|
2023-08-13 13:36:18 +00:00
|
|
|
const cleanName = fullName().trim()
|
2022-12-01 18:45:35 +00:00
|
|
|
const cleanEmail = email().trim()
|
2022-11-13 19:35:57 +00:00
|
|
|
|
2022-12-01 18:45:35 +00:00
|
|
|
if (!cleanName) {
|
2023-08-13 13:36:18 +00:00
|
|
|
newValidationErrors.fullName = t('Please enter a name to sign your comments and publication')
|
2022-10-25 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 18:45:35 +00:00
|
|
|
if (!cleanEmail) {
|
2022-10-25 16:25:42 +00:00
|
|
|
newValidationErrors.email = t('Please enter email')
|
2023-07-02 05:08:42 +00:00
|
|
|
} else if (!validateEmail(email())) {
|
2022-10-25 16:25:42 +00:00
|
|
|
newValidationErrors.email = t('Invalid email')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password()) {
|
|
|
|
newValidationErrors.password = t('Please enter password')
|
|
|
|
}
|
|
|
|
|
|
|
|
setValidationErrors(newValidationErrors)
|
|
|
|
|
2022-12-01 18:45:35 +00:00
|
|
|
const emailCheckResult = await checkEmail(cleanEmail)
|
2022-10-25 16:25:42 +00:00
|
|
|
|
|
|
|
const isValid = Object.keys(newValidationErrors).length === 0 && !emailCheckResult
|
|
|
|
|
|
|
|
if (!isValid) {
|
2023-08-13 13:36:18 +00:00
|
|
|
authFormRef.current
|
|
|
|
.querySelector<HTMLInputElement>(`input[name="${Object.keys(newValidationErrors)[0]}"]`)
|
|
|
|
.focus()
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await register({
|
2022-12-01 18:45:35 +00:00
|
|
|
name: cleanName,
|
|
|
|
email: cleanEmail,
|
2022-10-25 16:25:42 +00:00
|
|
|
password: password()
|
|
|
|
})
|
|
|
|
|
|
|
|
setIsSuccess(true)
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof ApiError && error.code === 'user_already_exists') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setSubmitError(error.message)
|
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Show when={!isSuccess()}>
|
2023-08-13 13:36:18 +00:00
|
|
|
<form onSubmit={handleSubmit} class={styles.authForm} ref={(el) => (authFormRef.current = el)}>
|
2023-05-18 20:02:19 +00:00
|
|
|
<div>
|
2023-07-30 12:31:54 +00:00
|
|
|
<AuthModalHeader modalType="register" />
|
2023-05-18 20:02:19 +00:00
|
|
|
<Show when={submitError()}>
|
|
|
|
<div class={styles.authInfo}>
|
|
|
|
<ul>
|
|
|
|
<li class={styles.warn}>{submitError()}</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-08-13 13:36:18 +00:00
|
|
|
<div
|
|
|
|
class={clsx('pretty-form__item', {
|
|
|
|
'pretty-form__item--error': validationErrors().fullName
|
|
|
|
})}
|
|
|
|
>
|
2023-05-18 20:02:19 +00:00
|
|
|
<input
|
|
|
|
name="fullName"
|
|
|
|
type="text"
|
|
|
|
placeholder={t('Full name')}
|
|
|
|
autocomplete=""
|
|
|
|
onInput={(event) => handleNameInput(event.currentTarget.value)}
|
|
|
|
/>
|
2023-08-13 13:36:18 +00:00
|
|
|
<label for="name">{t('Full name')}</label>
|
2022-10-25 16:25:42 +00:00
|
|
|
</div>
|
2023-08-13 13:36:18 +00:00
|
|
|
<Show when={validationErrors().fullName}>
|
|
|
|
<div class={styles.validationError}>{validationErrors().fullName}</div>
|
2023-05-18 20:02:19 +00:00
|
|
|
</Show>
|
2023-08-13 13:36:18 +00:00
|
|
|
<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)}
|
|
|
|
onBlur={handleEmailBlur}
|
|
|
|
/>
|
|
|
|
<label for="email">{t('Email')}</label>
|
2022-10-25 16:25:42 +00:00
|
|
|
</div>
|
2023-05-18 20:02:19 +00:00
|
|
|
<Show when={validationErrors().email}>
|
|
|
|
<div class={styles.validationError}>{validationErrors().email}</div>
|
|
|
|
</Show>
|
|
|
|
<Show when={emailChecks()[email()]}>
|
|
|
|
<div class={styles.validationError}>
|
|
|
|
{t("This email is already taken. If it's you")},{' '}
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
onClick={(event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
changeSearchParam('mode', 'login')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('enter')}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-08-13 13:36:18 +00:00
|
|
|
<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="current-password"
|
2023-08-21 11:11:18 +00:00
|
|
|
type={showPassword() ? 'text' : 'password'}
|
2023-05-18 20:02:19 +00:00
|
|
|
placeholder={t('Password')}
|
|
|
|
onInput={(event) => handlePasswordInput(event.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
<label for="password">{t('Password')}</label>
|
2023-08-21 11:11:18 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class={styles.passwordToggle}
|
|
|
|
onClick={() => setShowPassword(!showPassword())}
|
|
|
|
>
|
|
|
|
<Icon class={styles.passwordToggleIcon} name={showPassword() ? 'eye-off' : 'eye'} />
|
|
|
|
</button>
|
2023-05-18 20:02:19 +00:00
|
|
|
</div>
|
|
|
|
<Show when={validationErrors().password}>
|
|
|
|
<div class={clsx(styles.registerPassword, styles.validationError)}>
|
|
|
|
{validationErrors().password}
|
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<button class={clsx('button', styles.submitButton)} disabled={isSubmitting()} type="submit">
|
|
|
|
{isSubmitting() ? '...' : t('Join')}
|
|
|
|
</button>
|
2023-05-11 17:20:40 +00:00
|
|
|
</div>
|
2022-10-25 16:25:42 +00:00
|
|
|
</div>
|
|
|
|
|
2023-05-18 20:02:19 +00:00
|
|
|
<div>
|
|
|
|
<SocialProviders />
|
2022-10-25 16:25:42 +00:00
|
|
|
|
2023-05-18 20:02:19 +00:00
|
|
|
<div class={styles.authControl}>
|
|
|
|
<span class={styles.authLink} onClick={() => changeSearchParam('mode', 'login')}>
|
|
|
|
{t('I have an account')}
|
|
|
|
</span>
|
|
|
|
</div>
|
2022-10-25 16:25:42 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Show>
|
|
|
|
<Show when={isSuccess()}>
|
|
|
|
<div class={styles.title}>{t('Almost done! Check your email.')}</div>
|
|
|
|
<div class={styles.text}>{t("We've sent you a message with a link to enter our website.")}</div>
|
|
|
|
<div>
|
|
|
|
<button class={clsx('button', styles.submitButton)} onClick={() => hideModal()}>
|
|
|
|
{t('Back to main page')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|