1
0
Fork 0
freesewing/sites/org/pages/signup/index.mjs

154 lines
5.2 KiB
JavaScript
Raw Normal View History

// Hooks
2022-12-27 18:20:53 +01:00
import { useState } from 'react'
2023-03-24 17:43:38 +01:00
import { useApp } from 'site/hooks/use-app.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
import { useTranslation } from 'next-i18next'
// Dependencies
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
2023-02-05 18:44:54 +01:00
import { validateEmail, validateTld } from 'site/utils.mjs'
// Components
import Link from 'next/link'
import { PageWrapper } from 'site/components/wrappers/page.mjs'
import { BareLayout } from 'site/components/layouts/bare.mjs'
import { SusiWrapper } from 'site/components/wrappers/susi.mjs'
import { Robot } from 'shared/components/robot/index.mjs'
import { EmailValidButton } from 'site/components/buttons/email-valid-button.mjs'
import { ButtonText } from '../signin/index.mjs'
import { EmailIcon, RightIcon, WarningIcon } from 'shared/components/icons.mjs'
// Translation namespaces used on this page
const namespaces = ['signup', 'errors']
2022-12-27 18:20:53 +01:00
const DarkLink = ({ href, txt }) => (
<Link className="decoration-1 underline text-medium font-medium hover:decoration-2" href={href}>
{txt}
</Link>
)
2022-12-28 21:04:30 +01:00
const SignUpPage = (props) => {
const app = useApp(props)
2023-03-24 17:43:38 +01:00
const backend = useBackend()
2023-03-24 21:30:04 +01:00
const { t, i18n } = useTranslation(namespaces)
2022-12-27 18:20:53 +01:00
const [email, setEmail] = useState('')
const [emailValid, setEmailValid] = useState(false)
const [result, setResult] = useState(false)
2023-01-09 21:10:41 +01:00
const [error, setError] = useState(null)
2022-12-27 18:20:53 +01:00
const updateEmail = (evt) => {
const value = evt.target.value
setEmail(value)
const valid = (validateEmail(value) && validateTld(value)) || false
setEmailValid(valid === true ? true : false)
}
const signupHandler = async (evt) => {
evt.preventDefault()
2022-12-28 21:04:30 +01:00
if (!emailValid) return
let res
try {
2023-01-22 19:46:56 +01:00
res = await backend.signUp({
2022-12-27 18:20:53 +01:00
email,
2023-03-24 21:30:04 +01:00
language: i18n.language,
2022-12-28 21:04:30 +01:00
setResult,
2022-12-27 18:20:53 +01:00
})
2022-12-28 21:04:30 +01:00
} catch (err) {
setError(app.error(err))
2023-01-09 21:10:41 +01:00
// Here to keep the stupid linter happy
console.log(error)
2022-12-28 21:04:30 +01:00
}
2023-01-17 21:11:16 +01:00
if (res.result === 'success') setResult('success')
else setResult('fail')
2022-12-27 18:20:53 +01:00
}
2023-03-24 21:30:04 +01:00
const loadingClasses = app.state.loading ? 'opacity-50' : ''
2022-12-28 21:04:30 +01:00
2022-12-27 18:20:53 +01:00
return (
<PageWrapper app={app} title={t('joinFreeSewing')} layout={BareLayout} footer={false}>
2023-01-17 21:11:16 +01:00
<SusiWrapper theme={app.theme} error={result && result !== 'success'}>
2022-12-28 21:04:30 +01:00
<h1 className={`text-neutral-content font-light text-3xl mb-0 pb-0 ${loadingClasses}`}>
{result ? (
2023-01-17 21:11:16 +01:00
result === 'success' ? (
2022-12-28 21:04:30 +01:00
<span>{t('emailSent')}!</span>
) : (
2023-01-17 21:11:16 +01:00
<span>An error occured while trying to process your request</span>
2022-12-28 21:04:30 +01:00
)
) : (
<span>{t('joinFreeSewing')}!</span>
)}
2022-12-27 18:20:53 +01:00
</h1>
2023-01-17 21:11:16 +01:00
{result ? (
result === 'success' ? (
<>
<p className="text-neutral-content text-lg">
{t('checkYourInbox')} <b>FreeSewing.org</b>
</p>
<p className="text-neutral-content text-lg">{t('clickSignupLink')}</p>
<div className="flex flex-row gap-4 items-center justify-center p-8">
<button className="btn btn-ghost" onClick={() => setResult(false)}>
{t('back')}
</button>
<Link href="/support" className="btn btn-ghost">
{t('contact')}
</Link>
</div>
</>
) : (
<>
<Robot pose="ohno" className="m-auto w-56 text-neutral-content" embed />
<p className="text-neutral-content text-lg">{t('err2')}</p>
<p className="text-neutral-content text-lg">{t('err3')}</p>
<div className="flex flex-row gap-4 items-center justify-center p-8">
<button className="btn btn-ghost" onClick={() => setResult(false)}>
{t('back')}
</button>
<Link href="/support" className="btn btn-ghost">
{t('contact')}
</Link>
</div>
</>
)
) : (
<>
<p className={`text-neutral-content ${loadingClasses}`}>{t('toReceiveSignupLink')}:</p>
<form onSubmit={signupHandler}>
<input
type="email"
name="email"
onChange={updateEmail}
placeholder={t('emailAddress')}
className={`input input-bordered w-full text-base-content ${loadingClasses}`}
autoFocus={true}
value={email}
/>
<EmailValidButton
email={email}
app={app}
t={t}
validText={t('emailSignupLink')}
invalidText={t('pleaseProvideValidEmail')}
btnProps={{ type: 'submit' }}
/>
2023-01-17 21:11:16 +01:00
</form>
<p
className={`text-neutral-content text-sm mt-4 opacity-80 text-center ${loadingClasses}`}
>
{t('alreadyHaveAnAccount')} <DarkLink href="/signin" txt={t('signInHere')} />
</p>
</>
)}
2022-12-27 18:20:53 +01:00
</SusiWrapper>
</PageWrapper>
2022-12-27 18:20:53 +01:00
)
}
export default SignUpPage
export async function getStaticProps({ locale }) {
return {
props: {
2023-01-14 22:40:07 +01:00
...(await serverSideTranslations(locale)),
2022-12-27 18:20:53 +01:00
},
}
}