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

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
2023-09-04 12:18:52 +02:00
import { useState, useContext } from 'react'
2023-01-26 19:31:23 +01:00
import { useTranslation } from 'next-i18next'
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
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
// Components
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
2023-05-17 17:04:15 +02:00
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
import { MarkdownInput } from 'shared/components/inputs.mjs'
import { DynamicMdx } from 'shared/components/mdx/dynamic.mjs'
import { TipIcon } from 'shared/components/icons.mjs'
2023-01-26 19:31:23 +01:00
2023-08-19 18:01:22 +02:00
export const ns = ['account', 'status']
2023-01-26 19:31:23 +01:00
2023-04-22 16:41:13 +02:00
export const Tab = ({ id, activeTab, setActiveTab, t }) => (
2023-01-26 19:31:23 +01:00
<button
className={`text-xl font-bold capitalize tab tab-bordered grow
${activeTab === id ? 'tab-active' : ''}`}
onClick={() => setActiveTab(id)}
>
{t(id)}
</button>
)
2023-08-26 09:15:28 +02:00
export const BioSettings = ({ welcome = false }) => {
// Hooks
2023-08-20 18:48:40 +02:00
const { account, setAccount } = useAccount()
const backend = useBackend()
const { t, i18n } = useTranslation(ns)
2023-09-04 08:40:05 +02:00
const { setLoadingStatus } = useContext(LoadingStatusContext)
// State
2023-03-24 16:33:14 +01:00
const [bio, setBio] = useState(account.bio)
2023-01-26 19:31:23 +01:00
// Helper method to save bio
2023-01-26 19:31:23 +01:00
const save = async () => {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
const result = await backend.updateAccount({ bio })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'settingsSaved', true, true])
} else setLoadingStatus([true, 'backendError', true, false])
2023-01-26 19:31:23 +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 > 5
? '/welcome/' + welcomeSteps[account.control][6]
: '/docs/about/guide'
2023-01-26 19:31:23 +01:00
return (
<div className="max-w-xl xl:pl-4">
<MarkdownInput
2023-08-26 09:27:27 +02:00
id="account-bio"
label={t('bioTitle')}
update={setBio}
current={bio}
placeholder={t('bioTitle')}
docs={<DynamicMdx language={i18n.language} slug={`docs/about/site/account/bio`} />}
labelBL={
<span className="flex flex-row items-center gap-1">
<TipIcon className="w-6 h-6 text-success" />
{t('mdSupport')}
</span>
}
/>
<SaveSettingsButton btnProps={{ onClick: save }} welcome={welcome} />
2023-08-19 18:01:22 +02:00
{!welcome && <BackToAccountButton />}
2023-01-26 19:31:23 +01:00
{welcome ? (
<>
<ContinueButton btnProps={{ href: nextHref }} link />
2023-03-24 16:33:14 +01:00
{welcomeSteps[account.control].length > 0 ? (
2023-01-26 19:31:23 +01:00
<>
<progress
className="progress progress-primary w-full mt-12"
2023-03-24 16:33:14 +01:00
value={600 / welcomeSteps[account.control].length}
2023-01-26 19:31:23 +01:00
max="100"
></progress>
<span className="pt-4 text-sm font-bold opacity-50">
2023-03-24 16:33:14 +01:00
6 / {welcomeSteps[account.control].length}
2023-01-26 19:31:23 +01:00
</span>
<Icons
2023-03-24 16:33:14 +01:00
done={welcomeSteps[account.control].slice(0, 5)}
todo={welcomeSteps[account.control].slice(6)}
2023-01-26 19:31:23 +01:00
current="bio"
/>
</>
) : null}
</>
) : null}
</div>
2023-01-26 19:31:23 +01:00
)
}