// Dependencies
import { useState, useContext } from 'react'
import { useTranslation } from 'next-i18next'
// Hooks
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
import { useToast } from 'shared/hooks/use-toast.mjs'
// Context
import { LoadingContext } from 'shared/context/loading-context.mjs'
// Components
import Link from 'next/link'
import { Popout } from 'shared/components/popout.mjs'
import { BackToAccountButton } from './shared.mjs'
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
import {
GdprProfileDetails,
GdprMeasurementsDetails,
ns as gdprNs,
} from 'shared/components/gdpr/details.mjs'
export const ns = [...gdprNs, 'account', 'toast']
const Checkbox = ({ value, setter, label, children = null }) => (
setter(value ? false : true)}
>
setter(value ? false : true)}
/>
{label}
{children}
)
export const ConsentSettings = ({ title = false }) => {
// Context
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
// Hooks
const { account, token, setAccount, setToken } = useAccount()
const backend = useBackend(token)
const toast = useToast()
const { t } = useTranslation(ns)
// State
const [profile, setProfile] = useState(account?.consent > 0)
const [measurements, setMeasurements] = useState(account?.consent > 1)
const [openData, setOpenData] = useState(account?.consent > 2)
// Helper method to update the account
const update = async () => {
let newConsent = 0
if (profile) newConsent = 1
if (profile && measurements) newConsent = 2
if (profile && measurements && openData) newConsent = 3
if (newConsent !== account.consent) {
startLoading()
const result = await backend.updateAccount({ consent: newConsent })
if (result === true) toast.for.settingsSaved()
else toast.for.backendError()
stopLoading()
}
}
// Helper method to remove the account
const removeAccount = async () => {
startLoading()
const result = await backend.removeAccount()
if (result === true) toast.for.settingsSaved()
else toast.for.backendError()
setToken(null)
setAccount({ username: false })
stopLoading()
}
// Part A of the consent screen
const partA = (
<>
{t('profileQuestion')}
{profile ? (
) : (
<>
{t('profileWarning')}
>
)}
>
)
// Part B of the consent screen
const partB = (
<>
{t('setQuestion')}
{measurements ? (
) : (
)}
{measurements ? (
) : null}
{measurements && !openData ? {t('openDataInfo')} : null}
{!measurements && {t('noConsentNoPatterns')}}
>
)
return (
{title ?
{t('privacyMatters')}
: null}
{t('compliant')}
{t('consentWhyAnswer')}
{partA}
{profile && partB}
{profile && measurements ?
: null}
{profile && !measurements ? (
) : null}
{!profile ? (
) : null}
FreeSewing Privacy Notice
)
}