2025-04-01 16:15:20 +02:00
|
|
|
// Dependencies
|
|
|
|
import { welcomeSteps } from './shared.mjs'
|
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus'
|
|
|
|
// Hooks
|
|
|
|
import React, { useState, useContext } from 'react'
|
|
|
|
import { useAccount } from '@freesewing/react/hooks/useAccount'
|
|
|
|
import { useBackend } from '@freesewing/react/hooks/useBackend'
|
|
|
|
// Components
|
2025-05-30 11:29:55 +02:00
|
|
|
import { RightIcon } from '@freesewing/react/components/Icon'
|
2025-04-01 16:15:20 +02:00
|
|
|
import { ListInput } from '@freesewing/react/components/Input'
|
|
|
|
import { NumberCircle } from '@freesewing/react/components/Number'
|
|
|
|
import { IconButton } from '@freesewing/react/components/Button'
|
|
|
|
import { WelcomeIcons } from './shared.mjs'
|
|
|
|
|
2025-05-09 17:46:51 +02:00
|
|
|
/**
|
|
|
|
* A component to manage the user's units
|
2025-04-01 16:15:20 +02:00
|
|
|
*
|
2025-05-09 17:46:51 +02:00
|
|
|
* @component
|
|
|
|
* @param {bool} [props.welcome = false] - Set to true to render the welcome/onboarding view
|
|
|
|
* @returns {JSX.Element}
|
2025-04-01 16:15:20 +02:00
|
|
|
*/
|
|
|
|
export const Units = ({ welcome = false }) => {
|
|
|
|
// Hooks
|
|
|
|
const { account, setAccount } = useAccount()
|
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
|
|
|
const backend = useBackend()
|
|
|
|
|
|
|
|
// State
|
|
|
|
const [selection, setSelection] = useState(account?.imperial === true ? 'imperial' : 'metric')
|
|
|
|
|
|
|
|
// Helper method to update account
|
|
|
|
const update = async (val) => {
|
|
|
|
if (val !== selection) {
|
|
|
|
setLoadingStatus([true, 'Saving units'])
|
|
|
|
const [status, body] = await backend.updateAccount({
|
|
|
|
imperial: val === 'imperial' ? true : false,
|
|
|
|
})
|
|
|
|
if (status === 200 && body.result === 'success') {
|
|
|
|
setAccount(body.account)
|
|
|
|
setSelection(body.account.imperial ? 'imperial' : 'metric')
|
|
|
|
setLoadingStatus([true, 'Units updated', true, true])
|
|
|
|
} else setLoadingStatus([true, 'Something went wrong. Please report this', true, true])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next step in the onboarding
|
|
|
|
const nextHref =
|
|
|
|
welcomeSteps[account?.control].length > 3
|
|
|
|
? '/welcome/' + welcomeSteps[account?.control][3]
|
|
|
|
: '/docs/about/guide'
|
|
|
|
|
|
|
|
return (
|
2025-04-18 08:07:13 +00:00
|
|
|
<div className="tw:w-full">
|
2025-04-01 16:15:20 +02:00
|
|
|
<ListInput
|
|
|
|
id="account-units"
|
|
|
|
label="Units"
|
|
|
|
list={['metric', 'imperial'].map((val) => ({
|
|
|
|
val,
|
|
|
|
label: (
|
2025-04-18 08:07:13 +00:00
|
|
|
<div className="tw:flex tw:flex-row tw:items-center tw:w-full tw:justify-between">
|
2025-04-01 16:15:20 +02:00
|
|
|
<span>{val === 'metric' ? 'Metric units (cm)' : 'Imperial units (inch)'}</span>
|
|
|
|
<NumberCircle nr={val === 'imperial' ? '″' : 'cm'} color="secondary" />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
desc:
|
|
|
|
val === 'metric'
|
|
|
|
? 'Pick this if you prefere centimeters over inches'
|
|
|
|
: 'Pick this if you prefer inches over centimeters',
|
|
|
|
}))}
|
|
|
|
current={selection}
|
|
|
|
update={update}
|
|
|
|
/>
|
|
|
|
{welcome ? (
|
|
|
|
<>
|
2025-04-18 08:07:13 +00:00
|
|
|
<IconButton href={nextHref} className="tw:mt-4">
|
2025-04-01 16:15:20 +02:00
|
|
|
<RightIcon stroke={3} /> Continue
|
|
|
|
</IconButton>
|
|
|
|
{welcomeSteps[account?.control].length > 0 ? (
|
|
|
|
<>
|
|
|
|
<progress
|
2025-04-18 08:07:13 +00:00
|
|
|
className="tw:daisy-progress tw:daisy-progress-primary tw:w-full tw:mt-12"
|
2025-04-01 16:15:20 +02:00
|
|
|
value={300 / welcomeSteps[account?.control].length}
|
|
|
|
max="100"
|
|
|
|
></progress>
|
2025-04-18 08:07:13 +00:00
|
|
|
<span className="tw:pt-4 tw:text-sm tw:font-bold tw:opacity-50">
|
2025-04-01 16:15:20 +02:00
|
|
|
3 / {welcomeSteps[account?.control].length}
|
|
|
|
</span>
|
|
|
|
<WelcomeIcons
|
|
|
|
done={welcomeSteps[account?.control].slice(0, 2)}
|
|
|
|
todo={welcomeSteps[account?.control].slice(3)}
|
|
|
|
current="units"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|