1
0
Fork 0
freesewing/sites/shared/components/susi/sign-up.mjs

217 lines
7.4 KiB
JavaScript
Raw Normal View History

2023-07-29 21:01:26 +02:00
// Hooks
import { useState, useContext } from 'react'
import { useBackend } from 'shared/hooks/use-backend.mjs'
import { useTranslation } from 'next-i18next'
2023-08-29 12:19:47 +02:00
import { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs'
2023-07-29 21:01:26 +02:00
// Context
import { ModalContext } from 'shared/context/modal-context.mjs'
// Dependencies
2023-08-28 14:28:07 +02:00
import { validateEmail, validateTld, horFlexClasses, horFlexClassesNoSm } from 'shared/utils.mjs'
2023-07-29 21:01:26 +02:00
// Components
import Link from 'next/link'
import { Robot } from 'shared/components/robot/index.mjs'
import {
LeftIcon,
HelpIcon,
GoogleIcon,
GitHubIcon,
KeyIcon,
SettingsIcon,
EmailIcon,
2023-08-29 12:19:47 +02:00
DownIcon,
} from 'shared/components/icons.mjs'
2023-07-29 21:01:26 +02:00
import { ModalWrapper } from 'shared/components/wrappers/modal.mjs'
import { EmailInput } from 'shared/components/inputs.mjs'
2023-07-29 21:01:26 +02:00
// Translation namespaces used on this page
2023-08-28 14:10:43 +02:00
export const ns = ['susi', 'errors']
2023-07-29 21:01:26 +02:00
export const SignUp = () => {
// Context
const { setModal } = useContext(ModalContext)
const backend = useBackend()
2023-08-28 14:10:43 +02:00
const { t, i18n } = useTranslation(ns)
2023-08-29 12:19:47 +02:00
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
2023-07-29 21:01:26 +02:00
const [email, setEmail] = useState('')
const [emailValid, setEmailValid] = useState(false)
const [result, setResult] = useState(false)
2023-08-29 12:19:47 +02:00
const [showAll, setShowAll] = useState(false)
2023-07-29 21:01:26 +02:00
const updateEmail = (value) => {
2023-07-29 21:01:26 +02:00
setEmail(value)
const valid = (validateEmail(value) && validateTld(value)) || false
setEmailValid(valid === true ? true : false)
}
const signupHandler = async (evt) => {
evt.preventDefault()
2023-08-29 12:19:47 +02:00
if (!emailValid) {
setLoadingStatus([true, t('susi:pleaseProvideValidEmail'), true, false])
return
}
2023-07-29 21:01:26 +02:00
let res
try {
res = await backend.signUp({
email,
language: i18n.language,
setResult,
})
} catch (err) {
// Here to keep the stupid linter happy
console.log(err)
}
2023-08-13 18:00:43 +02:00
if (res.data.result === 'created') setResult('success')
2023-07-29 21:01:26 +02:00
else {
setModal(
<ModalWrapper bg="base-100 lg:bg-base-300">
<div className="bg-base-100 rounded-lg p-4 lg:px-8 max-w-xl lg:shadow-lg">
<h3>An error occured while trying to process your request</h3>
<Robot pose="ohno" className="m-auto w-56" embed />
2023-08-28 14:10:43 +02:00
<p className="text-lg">{t('susi:err2')}</p>
<p className="text-lg">{t('susi:err3')}</p>
2023-07-29 21:01:26 +02:00
<div className="flex flex-row gap-4 items-center justify-center p-8 flex-wrap">
<button className="btn btn-primary px-8" onClick={() => setResult(false)}>
<LeftIcon />
2023-08-28 14:10:43 +02:00
<span className="pl-2">{t('susi:back')}</span>
2023-07-29 21:01:26 +02:00
</button>
<Link href="/support" className="btn btn-primary btn-outline px-8">
<HelpIcon />
2023-08-28 14:10:43 +02:00
<span className="pl-2">{t('susi:contact')}</span>
2023-07-29 21:01:26 +02:00
</Link>
</div>
</div>
</ModalWrapper>
)
}
}
return (
<div className="w-full">
2023-08-29 12:19:47 +02:00
<LoadingStatus />
<h2 className="text-inherit">
2023-07-29 21:01:26 +02:00
{result ? (
result === 'success' ? (
2023-08-28 14:10:43 +02:00
<span>{t('susi:emailSent')}!</span>
2023-07-29 21:01:26 +02:00
) : (
<span>An error occured while trying to process your request</span>
)
) : (
2023-08-28 14:10:43 +02:00
<span>{t('susi:createAFreeSewingAccount')}</span>
2023-07-29 21:01:26 +02:00
)}
</h2>
{result ? (
result === 'success' ? (
<>
<p className="text-inherit text-lg">
2023-08-28 14:10:43 +02:00
{t('susi:checkYourInbox')} <b>FreeSewing.org</b>
2023-07-29 21:01:26 +02:00
</p>
2023-08-28 14:10:43 +02:00
<p className="text-inherit text-lg">{t('susi:clickSignupLink')}</p>
2023-07-29 21:01:26 +02:00
<div className="flex flex-row gap-4 items-center justify-center p-8">
<button className="btn btn-ghost" onClick={() => setResult(false)}>
2023-08-28 14:10:43 +02:00
{t('susi:back')}
2023-07-29 21:01:26 +02:00
</button>
<Link href="/support" className="btn btn-ghost">
2023-08-28 14:10:43 +02:00
{t('susi:contact')}
2023-07-29 21:01:26 +02:00
</Link>
</div>
</>
) : (
<>
<Robot pose="ohno" className="m-auto w-56 text-inherit" embed />
2023-08-28 14:10:43 +02:00
<p className="text-inherit text-lg">{t('susi:err2')}</p>
<p className="text-inherit text-lg">{t('susi:err3')}</p>
2023-07-29 21:01:26 +02:00
<div className="flex flex-row gap-4 items-center justify-center p-8">
<button className="btn btn-ghost" onClick={() => setResult(false)}>
2023-08-28 14:10:43 +02:00
{t('susi:back')}
2023-07-29 21:01:26 +02:00
</button>
<Link href="/support" className="btn btn-ghost">
2023-08-28 14:10:43 +02:00
{t('susi:contact')}
2023-07-29 21:01:26 +02:00
</Link>
</div>
</>
)
) : (
<>
2023-08-29 12:19:47 +02:00
<p className="text-inherit">{t('toReceiveSignupLink')}:</p>
<form onSubmit={signupHandler}>
<EmailInput
2023-08-26 09:27:27 +02:00
id="signup-email"
2023-08-28 14:10:43 +02:00
label={t('susi:emailAddress')}
2023-08-29 12:19:47 +02:00
current={email}
original={''}
valid={(val) => emailValid}
2023-08-28 14:10:43 +02:00
placeholder={t('susi:emailAddress')}
update={updateEmail}
2023-07-29 21:01:26 +02:00
/>
<button
className={`btn btn-primary btn-lg mt-2 w-full ${horFlexClasses} disabled:bg-neutral disabled:text-neutral-content disabled:opacity-50`}
type="submit"
>
2023-08-29 12:19:47 +02:00
<EmailIcon />
{t('susi:emailSignupLink')}
</button>
2023-07-29 21:01:26 +02:00
</form>
2023-08-29 12:19:47 +02:00
{showAll ? (
<>
<div className="grid grid-cols-1 md:grid-cols-2 gap-2 items-center mt-4">
{['Google', 'Github'].map((provider) => (
<button
key={provider}
id={provider}
className={`${horFlexClasses} btn btn-secondary`}
>
{provider === 'Google' ? <GoogleIcon stroke={0} /> : <GitHubIcon />}
<span>{t('susi:signUpWithProvider', { provider })}</span>
</button>
))}
</div>
<Link
className={`${horFlexClassesNoSm} w-full btn btn-lg btn-neutral mt-2`}
href="/signup"
>
<span className="hidden md:block">
<KeyIcon className="h-10 w-10" />
</span>
{t('susi:signInHere')}
</Link>
<Link
className={`${horFlexClassesNoSm} w-full btn btn-neutral btn-outline mt-2`}
href="/migrate"
>
<span className="hidden md:block">
<SettingsIcon />
</span>
{t('susi:migrateV2Account')}
</Link>
<div className="flex flex-row justify-center mt-2">
<button
onClick={() => setShowAll(false)}
className={`btn btn-ghost ${horFlexClasses}`}
>
<DownIcon className="w-6 h-6 rotate-180" />
{t('susi:fewerOptions')}
<DownIcon className="w-6 h-6 rotate-180" />
</button>
</div>
</>
) : (
<div className="flex flex-row justify-center mt-2">
2023-08-28 17:10:03 +02:00
<button
2023-08-29 12:19:47 +02:00
onClick={() => setShowAll(true)}
className={`btn btn-ghost ${horFlexClasses}`}
2023-08-28 17:10:03 +02:00
>
2023-08-29 12:19:47 +02:00
<DownIcon />
{t('susi:moreOptions')}
<DownIcon />
</button>
2023-08-29 12:19:47 +02:00
</div>
)}
2023-07-29 21:01:26 +02:00
</>
)}
</div>
2023-07-29 21:01:26 +02:00
)
}