2023-01-29 16:44:02 +01:00
|
|
|
// Hooks
|
2023-09-01 19:09:58 +02:00
|
|
|
import { useEffect, useState } 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'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { useRouter } from 'next/router'
|
2023-01-14 22:40:07 +01:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-09-01 18:30:24 +02:00
|
|
|
import { nsMerge } from 'shared/utils.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
// Dependencies
|
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
|
|
|
// Components
|
2023-04-16 19:37:57 +02:00
|
|
|
import { PageWrapper, ns as pageNs } from 'shared/components/wrappers/page.mjs'
|
|
|
|
import { BareLayout, ns as layoutNs } from 'site/components/layouts/bare.mjs'
|
2023-05-17 17:32:19 +02:00
|
|
|
import { WelcomeWrapper } from 'shared/components/wrappers/welcome.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { Spinner } from 'shared/components/spinner.mjs'
|
2023-07-23 18:42:06 +02:00
|
|
|
import { Popout } from 'shared/components/popout/index.mjs'
|
2023-09-01 18:30:24 +02:00
|
|
|
import { ConsentForm, ns as gdprNs } from 'shared/components/gdpr/form.mjs'
|
2023-01-14 22:40:07 +01:00
|
|
|
|
|
|
|
// Translation namespaces used on this page
|
2023-09-01 18:30:24 +02:00
|
|
|
const ns = nsMerge(pageNs, layoutNs, gdprNs, 'confirm')
|
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 ConfirmSignUpPage = () => {
|
2023-09-01 19:09:58 +02:00
|
|
|
// Hooks
|
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)
|
|
|
|
const [ready, setReady] = useState(false)
|
|
|
|
const [error, setError] = useState(false)
|
|
|
|
|
2023-09-01 18:30:24 +02:00
|
|
|
const createAccount = async ({ consent1, consent2 }) => {
|
2023-05-05 19:56:51 +02:00
|
|
|
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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2023-09-01 18:30:24 +02:00
|
|
|
{ready ? <ConsentForm submit={createAccount} /> : <Spinner className="w-8 h-8 m-auto" />}
|
2023-05-05 19:56:51 +02:00
|
|
|
</WelcomeWrapper>
|
|
|
|
<br />
|
|
|
|
</PageWrapper>
|
|
|
|
)
|
2023-01-14 22:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ConfirmSignUpPage
|
|
|
|
|
|
|
|
export async function getStaticProps({ locale }) {
|
|
|
|
return {
|
|
|
|
props: {
|
2023-01-28 18:10:29 +01:00
|
|
|
...(await serverSideTranslations(locale, ns)),
|
2023-01-14 22:40:07 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
return {
|
|
|
|
paths: [],
|
|
|
|
fallback: true,
|
|
|
|
}
|
|
|
|
}
|