2023-03-24 16:33:14 +01:00
|
|
|
// Dependencies
|
2023-04-28 21:23:06 +02:00
|
|
|
import { useState, useContext } from 'react'
|
2023-01-22 19:46:56 +01:00
|
|
|
import { useTranslation } from 'next-i18next'
|
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'
|
|
|
|
import { useToast } from 'shared/hooks/use-toast.mjs'
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingContext } from 'shared/context/loading-context.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
// Components
|
2023-02-18 16:11:02 +01:00
|
|
|
import { BackToAccountButton, Choice, Icons, welcomeSteps } from './shared.mjs'
|
2023-02-12 18:35:54 +01:00
|
|
|
import { ContinueButton } from 'site/components/buttons/continue-button.mjs'
|
2023-01-22 19:46:56 +01:00
|
|
|
|
2023-02-18 16:11:02 +01:00
|
|
|
export const ns = ['account', 'toast']
|
2023-01-22 19:46:56 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
export const ControlSettings = ({ title = false, welcome = false }) => {
|
|
|
|
// Context
|
|
|
|
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
|
|
|
|
|
|
|
|
// Hooks
|
2023-04-22 16:41:13 +02:00
|
|
|
const { account, setAccount, token } = useAccount()
|
2023-03-24 16:33:14 +01:00
|
|
|
const backend = useBackend(token)
|
2023-02-18 16:11:02 +01:00
|
|
|
const toast = useToast()
|
2023-01-28 18:10:29 +01:00
|
|
|
const { t } = useTranslation(ns)
|
2023-04-28 21:23:06 +02:00
|
|
|
|
|
|
|
// State
|
2023-03-24 16:33:14 +01:00
|
|
|
const [selection, setSelection] = useState(account.control || 2)
|
2023-01-22 19:46:56 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Method to update the control setting
|
2023-01-22 19:46:56 +01:00
|
|
|
const update = async (control) => {
|
|
|
|
if (control !== selection) {
|
2023-04-28 21:23:06 +02:00
|
|
|
startLoading()
|
2023-01-22 19:46:56 +01:00
|
|
|
const result = await backend.updateAccount({ control })
|
2023-04-22 16:41:13 +02:00
|
|
|
if (result.success) {
|
|
|
|
setSelection(control)
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
setAccount(result.data.account)
|
|
|
|
} else toast.for.backendError()
|
2023-04-28 21:23:06 +02:00
|
|
|
stopLoading()
|
2023-01-22 19:46:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper to get the link to the next onboarding step
|
|
|
|
const nextHref = welcome
|
|
|
|
? welcomeSteps[selection].length > 1
|
|
|
|
? '/welcome/' + welcomeSteps[selection][1]
|
|
|
|
: '/docs/guide'
|
|
|
|
: false
|
2023-01-26 19:31:23 +01:00
|
|
|
|
2023-01-22 19:46:56 +01:00
|
|
|
return (
|
2023-03-27 19:21:27 +02:00
|
|
|
<div className="max-w-xl">
|
2023-02-18 16:11:02 +01:00
|
|
|
{title ? <h1 className="text-4xl">{t('controlTitle')}</h1> : null}
|
2023-01-23 20:23:53 +01:00
|
|
|
{[1, 2, 3, 4, 5].map((val) => {
|
|
|
|
if (selection === 1 && val > 2) return null
|
|
|
|
if (selection === 2 && val > 3) return null
|
|
|
|
if (selection === 3 && val > 4) return null
|
|
|
|
if (selection === 5 && val < 4) return null
|
|
|
|
else
|
|
|
|
return (
|
2023-01-27 21:09:44 +01:00
|
|
|
<Choice val={val} t={t} update={update} current={selection} key={val}>
|
2023-01-23 20:23:53 +01:00
|
|
|
<span className="block text-lg leading-5">
|
2023-02-18 16:11:02 +01:00
|
|
|
{selection === 1 && val === 2 ? t('showMore') : t(`control${val}t`)}
|
2023-01-23 20:23:53 +01:00
|
|
|
</span>
|
|
|
|
{selection > 1 ? (
|
2023-02-18 16:11:02 +01:00
|
|
|
<span className="block text-normal font-light normal-case pt-1 leading-5">
|
|
|
|
{t(`control${val}d`)}
|
|
|
|
</span>
|
2023-01-23 20:23:53 +01:00
|
|
|
) : null}
|
|
|
|
</Choice>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
{welcome ? (
|
|
|
|
<>
|
2023-04-28 21:23:06 +02:00
|
|
|
<ContinueButton btnProps={{ href: nextHref }} link />
|
2023-01-26 19:31:23 +01:00
|
|
|
{welcomeSteps[selection].length > 1 ? (
|
2023-01-23 20:23:53 +01:00
|
|
|
<>
|
|
|
|
<progress
|
|
|
|
className="progress progress-primary w-full mt-12"
|
2023-01-26 19:31:23 +01:00
|
|
|
value={100 / welcomeSteps[selection].length}
|
2023-01-23 20:23:53 +01:00
|
|
|
max="100"
|
|
|
|
></progress>
|
|
|
|
<span className="pt-4 text-sm font-bold opacity-50">
|
2023-01-26 19:31:23 +01:00
|
|
|
1 / {welcomeSteps[selection].length}
|
2023-01-23 20:23:53 +01:00
|
|
|
</span>
|
2023-01-26 19:31:23 +01:00
|
|
|
<Icons done={[]} todo={welcomeSteps[selection].slice(1)} current="" />
|
2023-01-23 20:23:53 +01:00
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
2023-02-18 16:11:02 +01:00
|
|
|
) : (
|
2023-04-28 21:23:06 +02:00
|
|
|
<BackToAccountButton />
|
2023-02-18 16:11:02 +01:00
|
|
|
)}
|
2023-03-27 19:21:27 +02:00
|
|
|
</div>
|
2023-01-22 19:46:56 +01:00
|
|
|
)
|
|
|
|
}
|