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

126 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
import { useState, useContext } from 'react'
2023-01-23 20:23:53 +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'
import { useToast } from 'shared/hooks/use-toast.mjs'
// Context
import { LoadingContext } from 'shared/context/loading-context.mjs'
// Components
import { Spinner } from 'shared/components/spinner.mjs'
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
import { OkIcon, NoIcon } from 'shared/components/icons.mjs'
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
2023-01-23 20:23:53 +01:00
export const ns = ['account', 'toast']
2023-01-23 20:23:53 +01:00
export const UsernameSettings = ({ title = false, welcome = false }) => {
// Context
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
// Hooks
2023-03-24 16:33:14 +01:00
const { account, setAccount, token } = useAccount()
const backend = useBackend(token)
const toast = useToast()
const { t } = useTranslation(ns)
2023-03-24 16:33:14 +01:00
const [username, setUsername] = useState(account.username)
const [available, setAvailable] = useState(true)
2023-01-23 20:23:53 +01:00
const update = async (evt) => {
evt.preventDefault()
if (evt.target.value !== username) {
setUsername(evt.target.value)
const free = await backend.isUsernameAvailable(evt.target.value)
setAvailable(free)
2023-01-23 20:23:53 +01:00
}
}
const save = async () => {
startLoading()
const result = await backend.updateAccount({ username })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
toast.for.settingsSaved()
} else toast.for.backendError()
stopLoading()
}
2023-01-26 19:31:23 +01:00
const nextHref =
2023-03-24 16:33:14 +01:00
welcomeSteps[account.control].length > 4
? '/welcome/' + welcomeSteps[account.control][5]
2023-01-26 19:31:23 +01:00
: '/docs/guide'
let btnClasses = 'btn mt-4 capitalize '
if (welcome) {
btnClasses += 'w-64 '
if (loading) btnClasses += 'btn-accent '
else btnClasses += 'btn-secondary '
} else {
btnClasses += 'w-full '
if (loading) btnClasses += 'btn-accent '
else btnClasses += 'btn-primary '
}
2023-01-23 20:23:53 +01:00
return (
2023-03-27 19:21:27 +02:00
<div className="max-w-xl">
{title ? <h1 className="text-4xl">{t('usernameTitle')}</h1> : null}
<div className="flex flex-row items-center">
<input
value={username}
onChange={update}
className="input w-full input-bordered flex flex-row"
type="text"
placeholder={t('title')}
/>
<span className={`-ml-10 rounded-full p-1 ${available ? 'bg-success' : 'bg-error'}`}>
{available ? (
<OkIcon className="w-5 h-5 text-neutral-content" stroke={4} />
) : (
<NoIcon className="w-5 h-5 text-neutral-content" stroke={3} />
)}
</span>
</div>
<button className={btnClasses} disabled={!available} onClick={save}>
<span className="flex flex-row items-center gap-2">
{loading ? (
<>
<Spinner />
<span>{t('processing')}</span>
</>
) : available ? (
t('save')
) : (
t('usernameNotAvailable')
)}
</span>
</button>
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={500 / 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
5 / {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, 4)}
todo={welcomeSteps[account.control].slice(5)}
2023-01-26 19:31:23 +01:00
current="username"
/>
2023-01-23 20:23:53 +01:00
</>
) : null}
</>
) : (
<BackToAccountButton loading={loading} />
)}
2023-03-27 19:21:27 +02:00
</div>
2023-01-23 20:23:53 +01:00
)
}