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

89 lines
3 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
2023-08-19 18:01:22 +02:00
import { cloudflareImageUrl } from 'shared/utils.mjs'
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 } from './shared.mjs'
2023-05-17 17:04:15 +02:00
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
import { PassiveImageInput } from 'shared/components/inputs.mjs'
import { DynamicMdx } from 'shared/components/mdx/dynamic.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-08-26 09:15:28 +02:00
export const ImgSettings = ({ welcome = false }) => {
2023-08-20 18:48:40 +02:00
const { account, setAccount } = useAccount()
const backend = useBackend()
2023-09-04 08:40:05 +02:00
const { setLoadingStatus } = useContext(LoadingStatusContext)
const { t, i18n } = useTranslation(ns)
2023-01-27 20:39:09 +01:00
const [img, setImg] = useState('')
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({ img })
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
}
const nextHref = '/docs/about/guide'
2023-01-26 19:31:23 +01:00
return (
2023-03-27 19:21:27 +02:00
<div className="max-w-xl">
{!welcome || img !== false ? (
<img
alt="img"
2023-10-20 15:20:08 +02:00
src={img || cloudflareImageUrl({ id: `uid-${account.ihash}`, variant: 'public' })}
className="shadow mb-4"
/>
) : null}
<PassiveImageInput
2023-08-26 09:27:27 +02:00
id="account-img"
label={t('image')}
placeholder={'image'}
update={setImg}
current={img}
valid={(val) => val.length > 0}
docs={<DynamicMdx language={i18n.language} slug={`docs/about/site/account/img`} />}
/>
2023-01-26 19:31:23 +01:00
{welcome ? (
<>
<button className={`btn btn-secondary mt-4 px-8`} onClick={save} disabled={!img}>
{t('save')}
</button>
<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={700 / 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
7 / {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, 6)}
todo={welcomeSteps[account.control].slice(7)}
2023-01-26 19:31:23 +01:00
current="img"
/>
</>
) : null}
</>
) : (
<>
<SaveSettingsButton btnProps={{ onClick: save }} />
2023-08-19 18:01:22 +02:00
<BackToAccountButton />
</>
)}
2023-03-27 19:21:27 +02:00
</div>
2023-01-26 19:31:23 +01:00
)
}