import type { AuthModalSearchParams } from './types' import { clsx } from 'clsx' import { createSignal, JSX, Show } from 'solid-js' import { useLocalize } from '../../../context/localize' import { useSession } from '../../../context/session' // import { ApiError } from '../../../graphql/error' import { useRouter } from '../../../stores/router' import { validateEmail } from '../../../utils/validateEmail' import { email, setEmail } from './sharedLogic' import styles from './AuthModal.module.scss' type FormFields = { email: string } type ValidationErrors = Partial> export const ForgotPasswordForm = () => { const { changeSearchParams } = useRouter() const { t } = useLocalize() const handleEmailInput = (newEmail: string) => { setValidationErrors(({ email: _notNeeded, ...rest }) => rest) setEmail(newEmail.toLowerCase()) } const { actions: { forgotPassword }, } = useSession() const [submitError, setSubmitError] = createSignal('') const [isSubmitting, setIsSubmitting] = createSignal(false) const [validationErrors, setValidationErrors] = createSignal({}) const [isUserNotFount, setIsUserNotFound] = createSignal(false) const authFormRef: { current: HTMLFormElement } = { current: null } const [message, setMessage] = createSignal('') const handleSubmit = async (event: Event) => { event.preventDefault() setSubmitError('') setIsUserNotFound(false) const newValidationErrors: ValidationErrors = {} if (!email()) { newValidationErrors.email = t('Please enter email') } else if (!validateEmail(email())) { newValidationErrors.email = t('Invalid email') } setValidationErrors(newValidationErrors) const isValid = Object.keys(newValidationErrors).length === 0 if (!isValid) { authFormRef.current .querySelector(`input[name="${Object.keys(newValidationErrors)[0]}"]`) .focus() return } setIsSubmitting(true) try { const { data, errors } = await forgotPassword({ email: email(), redirect_uri: window.location.origin, }) if (data) { console.debug('[ForgotPasswordForm] authorizer response:', data) setMessage(data.message) } if (errors) { console.warn(errors) if (errors) { const error: Error = errors[0] if (error.cause === 'user_not_found') { setIsUserNotFound(true) return } else { setSubmitError(error.message) } } } } catch (error) { console.error(error) } finally { setIsSubmitting(false) } } return (
(authFormRef.current = el)} >

{t('Restore password')}

{t(message()) || t('Everything is ok, please give us your email address')}
handleEmailInput(event.currentTarget.value)} />
  • {submitError()}
{t("We can't find you, check email or")}{' '} { event.preventDefault() changeSearchParams({ mode: 'register', }) }} > {t('register')}
{validationErrors().email}
changeSearchParams({ mode: 'login', }) } > {t('I know the password')}
) }