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

109 lines
3.2 KiB
JavaScript
Raw Normal View History

// 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'
import { useRouter } from 'next/router'
2023-01-14 22:40:07 +01:00
import { useTranslation } from 'next-i18next'
import { nsMerge } from 'shared/utils.mjs'
// Dependencies
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
// 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'
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
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)
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>
{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: {
...(await serverSideTranslations(locale, ns)),
2023-01-14 22:40:07 +01:00
},
}
}
export async function getStaticPaths() {
return {
paths: [],
fallback: true,
}
}