1
0
Fork 0
freesewing/sites/shared/components/account/consent.mjs

117 lines
4 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
2023-09-04 08:40:05 +02:00
import { useState, useContext } from 'react'
2023-02-18 16:11:02 +01:00
import { useTranslation } from 'next-i18next'
2023-08-19 18:01:22 +02:00
import { nsMerge } from 'shared/utils.mjs'
2023-09-04 08:40:05 +02:00
// Context
import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs'
2023-03-24 16:33:14 +01:00
// Hooks
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
2023-02-18 16:11:02 +01:00
// Components
import Link from 'next/link'
import { Popout } from 'shared/components/popout/index.mjs'
2023-02-19 20:58:06 +01:00
import { BackToAccountButton } from './shared.mjs'
2023-05-17 17:04:15 +02:00
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
2023-08-13 18:00:43 +02:00
import { GdprAccountDetails, ns as gdprNs } from 'shared/components/gdpr/details.mjs'
2023-02-18 16:11:02 +01:00
2023-09-04 08:40:05 +02:00
export const ns = nsMerge(gdprNs, 'account', 'status')
2023-02-18 16:11:02 +01: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>
)
export const ConsentSettings = ({ title = false }) => {
// Hooks
2023-08-20 18:48:40 +02:00
const { account, setAccount, setToken } = useAccount()
const backend = useBackend()
2023-09-04 08:40:05 +02:00
const { setLoadingStatus } = useContext(LoadingStatusContext)
2023-02-18 16:11:02 +01:00
const { t } = useTranslation(ns)
// State
2023-08-13 18:00:43 +02:00
const [consent1, setConsent1] = useState(account?.consent > 0)
const [consent2, setConsent2] = useState(account?.consent > 1)
2023-02-18 16:11:02 +01:00
// Helper method to update the account
2023-02-18 16:11:02 +01:00
const update = async () => {
let newConsent = 0
2023-08-13 18:00:43 +02:00
if (consent1) newConsent = 1
if (consent1 && consent2) newConsent = 2
2023-03-24 16:33:14 +01:00
if (newConsent !== account.consent) {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
2023-02-18 16:11:02 +01:00
const result = await backend.updateAccount({ consent: newConsent })
2023-08-19 18:01:22 +02:00
if (result.data?.result === 'success') {
setLoadingStatus([true, 'settingsSaved', true, true])
setAccount(result.data.account)
} else setLoadingStatus([true, 'backendError', true, true])
2023-02-18 16:11:02 +01:00
}
}
// Helper method to remove the account
2023-02-18 16:11:02 +01:00
const removeAccount = async () => {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
2023-02-18 16:11:02 +01:00
const result = await backend.removeAccount()
2023-08-19 18:01:22 +02:00
if (result === true) setLoadingStatus([true, 'settingsSaved', true, true])
else setLoadingStatus([true, 'backendError', true, true])
setToken(null)
setAccount({ username: false })
2023-02-18 16:11:02 +01:00
}
2023-08-13 18:00:43 +02:00
return (
<div className="max-w-xl xl:pl-4">
{title ? <h2 className="text-4xl">{t('privacyMatters')}</h2> : null}
<p>{t('compliant')}</p>
<p>{t('consentWhyAnswer')}</p>
<h5 className="mt-8">{t('accountQuestion')}</h5>
<GdprAccountDetails />
{consent1 ? (
<Checkbox value={consent1} setter={setConsent1} label={t('yesIDo')} />
2023-02-18 16:11:02 +01:00
) : (
<button
2023-08-13 18:00:43 +02:00
className="btn btn-primary btn-lg w-full mt-4"
onClick={() => setConsent1(!consent1)}
2023-02-18 16:11:02 +01:00
>
{t('clickHere')}
</button>
)}
2023-08-13 18:00:43 +02:00
{consent1 ? (
<Checkbox value={consent2} setter={setConsent2} label={t('openDataQuestion')} />
2023-02-18 16:11:02 +01:00
) : null}
2023-08-13 18:00:43 +02:00
{consent1 && !consent2 ? <Popout note>{t('openDataInfo')}</Popout> : null}
{!consent1 && <Popout warning>{t('noConsentNoAccount')}</Popout>}
{consent1 ? (
<SaveSettingsButton btnProps={{ onClick: update }} />
) : (
2023-02-18 16:11:02 +01:00
<SaveSettingsButton
label={t('account:removeAccount')}
btnProps={{
onClick: removeAccount,
className: 'btn mt-4 capitalize w-full btn-error',
}}
/>
2023-08-13 18:00:43 +02:00
)}
2023-08-19 18:01:22 +02:00
<BackToAccountButton />
2023-02-18 16:11:02 +01:00
<p className="text-center opacity-50 mt-12">
<Link href="/docs/about/privacy" className="hover:text-secondary underline">
2023-02-18 16:11:02 +01:00
FreeSewing Privacy Notice
</Link>
</p>
</div>
2023-02-18 16:11:02 +01:00
)
}