1
0
Fork 0
freesewing/sites/org/pages/confirm/signup/[...confirmation].mjs

194 lines
6.1 KiB
JavaScript
Raw Normal View History

// Hooks
2023-05-05 19:56:51 +02:00
import { useEffect, useState, useContext } from 'react'
2023-03-24 17:43:38 +01:00
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
import { useRouter } from 'next/router'
2023-01-14 22:40:07 +01:00
import { useTranslation } from 'next-i18next'
2023-05-05 19:56:51 +02:00
// Context
import { LoadingContext } from 'shared/context/loading-context.mjs'
// Dependencies
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
2023-01-14 22:40:07 +01:00
import Link from 'next/link'
// Components
import { PageWrapper, ns as pageNs } from 'shared/components/wrappers/page.mjs'
import { BareLayout, ns as layoutNs } from 'site/components/layouts/bare.mjs'
import { WelcomeWrapper } from 'shared/components/wrappers/welcome.mjs'
import { Spinner } from 'shared/components/spinner.mjs'
import { Popout } from 'shared/components/popout/index.mjs'
2023-08-13 18:00:43 +02:00
import { GdprAccountDetails, ns as gdprNs } from 'shared/components/gdpr/details.mjs'
2023-01-14 22:40:07 +01:00
// Translation namespaces used on this page
const ns = Array.from(new Set([...pageNs, ...layoutNs, ...gdprNs, 'confirm', 'locales', 'themes']))
2023-01-15 10:51:45 +01:00
2023-05-17 13:50:56 +02:00
const SignupLinkExpired = () => <Popout fixme>Implement SignupLinkExpired compnonent</Popout>
2023-05-05 19:56:51 +02:00
const Checkbox = ({ value, setter, label, children = null }) => (
<div
className={`form-control p-4 hover:cursor-pointer rounded border-l-8 my-2
${value ? 'border-success bg-success' : 'border-error bg-error'}
bg-opacity-10 shadow`}
onClick={() => setter(value ? false : true)}
>
<div className="form-control flex flex-row items-center gap-2">
<input
type="checkbox"
className="checkbox"
checked={value ? 'checked' : ''}
onChange={() => setter(value ? false : true)}
/>
<span className="label-text">{label}</span>
</div>
{children}
</div>
)
const ConfirmSignUpPage = () => {
// Context
2023-05-17 13:50:56 +02:00
const { loading } = useContext(LoadingContext)
2023-05-05 19:56:51 +02:00
const router = useRouter()
// Get confirmation ID and check from url
const [confirmationId, confirmationCheck] = router.asPath.slice(1).split('/').slice(2)
const page = {
path: ['confirm', 'emailchange', confirmationId],
}
2023-08-20 18:48:40 +02:00
const { setAccount, setToken } = useAccount()
const backend = useBackend()
2023-05-05 19:56:51 +02:00
const { t } = useTranslation(ns)
const [id, setId] = useState(false)
2023-08-13 18:00:43 +02:00
const [details, setDetails] = useState(false)
const [consent1, setConsent1] = useState(false)
const [consent2, setConsent2] = useState(false)
2023-05-05 19:56:51 +02:00
const [ready, setReady] = useState(false)
const [error, setError] = useState(false)
const createAccount = async () => {
let consent = 0
2023-08-13 18:00:43 +02:00
if (consent1) consent = 1
if (consent1 && consent2) consent = 2
2023-05-05 19:56:51 +02:00
if (consent > 0 && id) {
const result = await backend.confirmSignup({ consent, id })
if (result.success) {
setToken(result.data.token)
setAccount(result.data.account)
router.push('/welcome')
} else {
// Something went wrong
console.log('something went wrong')
}
}
}
2023-08-13 18:00:43 +02:00
const giveConsent = () => {
setConsent1(true)
setConsent2(true)
}
2023-05-05 19:56:51 +02:00
useEffect(() => {
// Async inside useEffect requires this approach
const getConfirmation = async () => {
// Reach out to backend
const data = await backend.loadConfirmation({
id: confirmationId,
check: confirmationCheck,
})
if (data instanceof Error) setError(true)
setReady(true)
setId(confirmationId)
}
// Call async method
getConfirmation()
2023-05-17 14:04:57 +02:00
}, [backend, confirmationCheck, confirmationId])
2023-05-05 19:56:51 +02:00
// Short-circuit errors
if (error)
return (
<PageWrapper {...page} title={t('joinFreeSewing')} layout={BareLayout} footer={false}>
<WelcomeWrapper>
<SignupLinkExpired />
</WelcomeWrapper>
</PageWrapper>
)
return (
<PageWrapper {...page} title={t('joinFreeSewing')} layout={BareLayout} footer={false}>
<WelcomeWrapper>
{ready ? (
<>
<h1>{t('gdpr:privacyMatters')}</h1>
<p>{t('gdpr:compliant')}</p>
<p>{t('gdpr:consentWhyAnswer')}</p>
2023-08-13 18:00:43 +02:00
<h5 className="mt-8">{t('gdpr:accountQuestion')}</h5>
{details ? <GdprAccountDetails /> : null}
{consent1 ? (
<>
<Checkbox value={consent1} setter={setConsent1} label={t('gdpr:yesIDo')} />
<Checkbox
value={consent2}
setter={setConsent2}
label={t('gdpr:openDataQuestion')}
/>
</>
) : (
<button className="btn btn-primary btn-lg w-full mt-4" onClick={giveConsent}>
{t('gdpr:clickHere')}
</button>
)}
{consent1 && !consent2 ? <Popout note>{t('openDataInfo')}</Popout> : null}
<p className="text-center">
<button
className="btn btn-neutral btn-ghost btn-sm"
onClick={() => setDetails(!details)}
>
{t(details ? 'gdpr:hideDetails' : 'gdpr:showDetails')}
</button>
</p>
{!consent1 && <Popout note>{t('gdpr:noConsentNoAccountCreation')}</Popout>}
2023-05-05 19:56:51 +02:00
</>
) : (
<Spinner className="w-8 h-8 m-auto" />
)}
2023-08-13 18:00:43 +02:00
{consent1 && (
2023-05-05 19:56:51 +02:00
<button
onClick={createAccount}
className={`btn btn-lg w-full mt-8 ${loading ? 'btn-accent' : 'btn-primary'}`}
>
{loading ? (
<>
<Spinner />
<span>{t('gdpr:processing')}</span>
</>
) : (
<span>{t('gdpr:createAccount')}</span>
)}
</button>
)}
<p className="text-center opacity-50 mt-12">
<Link href="/docs/various/privacy" className="hover:text-secondary underline">
FreeSewing Privacy Notice
</Link>
</p>
</WelcomeWrapper>
<br />
</PageWrapper>
)
2023-01-14 22:40:07 +01:00
}
export default ConfirmSignUpPage
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, ns)),
2023-01-14 22:40:07 +01:00
},
}
}
export async function getStaticPaths() {
return {
paths: [],
fallback: true,
}
}