// 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/index.mjs'
import { BackToAccountButton } from './shared.mjs'
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
import { GdprAccountDetails, 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 [consent1, setConsent1] = useState(account?.consent > 0)
const [consent2, setConsent2] = useState(account?.consent > 1)
// Helper method to update the account
const update = async () => {
let newConsent = 0
if (consent1) newConsent = 1
if (consent1 && consent2) newConsent = 2
if (newConsent !== account.consent) {
startLoading()
const result = await backend.updateAccount({ consent: newConsent })
if (result.data?.result === 'success') 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()
}
return (
{title ?
{t('privacyMatters')}
: null}
{t('compliant')}
{t('consentWhyAnswer')}
{t('accountQuestion')}
{consent1 ? (
) : (
)}
{consent1 ? (
) : null}
{consent1 && !consent2 ?
{t('openDataInfo')} : null}
{!consent1 &&
{t('noConsentNoAccount')}}
{consent1 ? (
) : (
)}
FreeSewing Privacy Notice
)
}