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

91 lines
3.1 KiB
JavaScript
Raw Normal View History

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
2023-09-04 12:18:52 +02:00
import { useState, useContext } from 'react'
import { useTranslation } from 'next-i18next'
2023-03-24 16:33:14 +01:00
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
// Components
import { Icons, welcomeSteps, BackToAccountButton, NumberBullet } from './shared.mjs'
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
import { ListInput } from 'shared/components/inputs.mjs'
import { DynamicOrgDocs } from 'site/components/dynamic-org-docs.mjs'
2023-01-23 20:23:53 +01:00
2023-08-19 18:01:22 +02:00
export const ns = ['account', 'status']
2023-01-23 20:23:53 +01:00
2023-08-26 09:15:28 +02:00
export const ImperialSettings = ({ welcome = false }) => {
// Hooks
2023-08-20 18:48:40 +02:00
const { account, setAccount } = useAccount()
2023-09-04 08:40:05 +02:00
const { setLoadingStatus } = useContext(LoadingStatusContext)
2023-08-20 18:48:40 +02:00
const backend = useBackend()
const { t, i18n } = useTranslation(ns)
// State
2023-03-24 16:33:14 +01:00
const [selection, setSelection] = useState(account?.imperial === true ? 'imperial' : 'metric')
2023-01-23 20:23:53 +01:00
// Helper method to update account
2023-01-23 20:23:53 +01:00
const update = async (val) => {
if (val !== selection) {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
2023-01-23 20:23:53 +01:00
const result = await backend.updateAccount({ imperial: val === 'imperial' ? true : false })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
setSelection(val)
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'settingsSaved', true, true])
} else setLoadingStatus([true, 'backendError', true, true])
2023-01-23 20:23:53 +01:00
}
}
// Next step in the onboarding
2023-01-26 19:31:23 +01:00
const nextHref =
2023-03-24 16:33:14 +01:00
welcomeSteps[account?.control].length > 3
? '/welcome/' + welcomeSteps[account?.control][3]
2023-01-26 19:31:23 +01:00
: '/docs/guide'
2023-01-23 20:23:53 +01:00
return (
2023-03-27 19:21:27 +02:00
<div className="max-w-xl">
<ListInput
2023-08-26 09:27:27 +02:00
id="account-units"
label={t('unitsTitle')}
list={['metric', 'imperial'].map((val) => ({
val,
label: (
<div className="flex flex-row items-center w-full justify-between">
<span>{t(`${val}Units`)}</span>
<NumberBullet nr={val === 'imperial' ? '″' : 'cm'} color="secondary" />
</div>
),
desc: t(`${val}Unitsd`),
}))}
current={selection}
update={update}
docs={<DynamicOrgDocs language={i18n.language} path={`site/account/units`} />}
/>
2023-01-23 20:23:53 +01:00
{welcome ? (
<>
<ContinueButton btnProps={{ href: nextHref }} link />
2023-03-24 16:33:14 +01:00
{welcomeSteps[account?.control].length > 0 ? (
2023-01-23 20:23:53 +01:00
<>
<progress
className="progress progress-primary w-full mt-12"
2023-03-24 16:33:14 +01:00
value={300 / welcomeSteps[account?.control].length}
2023-01-23 20:23:53 +01:00
max="100"
></progress>
<span className="pt-4 text-sm font-bold opacity-50">
2023-03-24 16:33:14 +01:00
3 / {welcomeSteps[account?.control].length}
2023-01-23 20:23:53 +01:00
</span>
2023-01-26 19:31:23 +01:00
<Icons
2023-03-24 16:33:14 +01:00
done={welcomeSteps[account?.control].slice(0, 2)}
todo={welcomeSteps[account?.control].slice(3)}
2023-01-26 19:31:23 +01:00
current="units"
/>
2023-01-23 20:23:53 +01:00
</>
) : null}
</>
) : (
2023-08-19 18:01:22 +02:00
<BackToAccountButton />
)}
2023-03-27 19:21:27 +02:00
</div>
2023-01-23 20:23:53 +01:00
)
}