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

104 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
2023-08-20 18:48:40 +02:00
import { useState } 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'
2023-08-19 18:01:22 +02:00
import { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs'
// Components
2023-08-26 09:06:51 +02:00
import { BackToAccountButton, Icons, welcomeSteps } from './shared.mjs'
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
import { ListInput } from 'shared/components/inputs.mjs'
import { DynamicOrgDocs } from 'shared/components/dynamic-docs/org.mjs'
import { ControlScore } from 'shared/components/control/score.mjs'
2023-01-22 19:46:56 +01:00
2023-08-19 18:01:22 +02:00
export const ns = ['account', 'status']
2023-01-22 19:46:56 +01:00
/** state handlers for any input that changes the control setting */
export const useControlState = () => {
// Hooks
2023-04-22 16:41:13 +02:00
const { account, setAccount, token } = useAccount()
2023-08-20 18:48:40 +02:00
const backend = useBackend()
2023-08-19 18:01:22 +02:00
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
// State
const [selection, setSelection] = useState(account.control)
2023-01-22 19:46:56 +01:00
// Method to update the control setting
2023-01-22 19:46:56 +01:00
const update = async (control) => {
if (control !== selection) {
if (token) {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
const result = await backend.updateAccount({ control })
if (result.success) {
setSelection(control)
setAccount(result.data.account)
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'settingsSaved', true, true])
} else setLoadingStatus([true, 'backendError', true, true])
}
//fallback for guest users
else {
setAccount({ ...account, control })
2023-04-22 16:41:13 +02:00
setSelection(control)
}
2023-01-22 19:46:56 +01:00
}
}
2023-08-19 18:01:22 +02:00
return { selection, update, LoadingStatus }
}
2023-08-26 09:15:28 +02:00
export const ControlSettings = ({ welcome = false, noBack = false }) => {
const { t, i18n } = useTranslation(ns)
2023-08-19 18:01:22 +02:00
const { selection, update, LoadingStatus } = useControlState()
// 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-08-19 18:01:22 +02:00
<LoadingStatus />
<ListInput
label={t('controlTitle')}
list={[1, 2, 3, 4, 5].map((val) => ({
val,
label: (
<div className="flex flex-row items-center w-full justify-between">
<span>{t(`control${val}.t`)}</span>
<ControlScore control={val} />
</div>
),
desc: t(`control${val}.d`),
}))}
current={selection}
update={update}
docs={<DynamicOrgDocs language={i18n.language} path={`site/account/control`} />}
/>
2023-01-23 20:23:53 +01:00
{welcome ? (
<>
<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}
</>
) : noBack ? null : (
<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
)
}