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'
|
2023-01-29 16:44:02 +01:00
|
|
|
// Components
|
2023-02-12 18:35:54 +01:00
|
|
|
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { OkIcon, NoIcon } from 'shared/components/icons.mjs'
|
2023-05-17 17:32:19 +02:00
|
|
|
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
|
2023-08-25 17:43:47 +02:00
|
|
|
import { StringInput } from 'shared/components/inputs.mjs'
|
2023-09-11 19:59:02 +02:00
|
|
|
import { DynamicOrgDocs } from 'site/components/dynamic-org-docs.mjs'
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-09-04 08:40:05 +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 UsernameSettings = ({ welcome = false }) => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Hooks
|
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)
|
2023-08-25 17:43:47 +02:00
|
|
|
const { t, i18n } = useTranslation(ns)
|
2023-03-24 16:33:14 +01:00
|
|
|
const [username, setUsername] = useState(account.username)
|
2023-01-24 21:17:49 +01:00
|
|
|
const [available, setAvailable] = useState(true)
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-08-25 17:43:47 +02:00
|
|
|
const update = async (value) => {
|
|
|
|
if (value !== username) {
|
|
|
|
setUsername(value)
|
|
|
|
const result = await backend.isUsernameAvailable(value)
|
2023-08-16 12:48:34 +02:00
|
|
|
if (result?.response?.response?.status === 404) setAvailable(true)
|
|
|
|
else setAvailable(false)
|
2023-01-23 20:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
const save = async () => {
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
2023-02-12 18:35:54 +01:00
|
|
|
const result = await backend.updateAccount({ username })
|
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, true])
|
2023-01-24 21:17:49 +01:00
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
|
2023-02-12 18:35:54 +01:00
|
|
|
let btnClasses = 'btn mt-4 capitalize '
|
2023-08-19 18:01:22 +02:00
|
|
|
if (welcome) btnClasses += 'w-64 btn-secondary'
|
|
|
|
else btnClasses += 'w-full btn-primary'
|
2023-02-12 18:35:54 +01:00
|
|
|
|
2023-01-23 20:23:53 +01:00
|
|
|
return (
|
2023-03-27 19:21:27 +02:00
|
|
|
<div className="max-w-xl">
|
2023-08-25 17:43:47 +02:00
|
|
|
<StringInput
|
2023-08-26 09:27:27 +02:00
|
|
|
id="account-username"
|
2023-08-25 17:43:47 +02:00
|
|
|
label={t('usernameTitle')}
|
|
|
|
current={username}
|
|
|
|
update={update}
|
|
|
|
valid={() => available}
|
|
|
|
placeholder={'Sorcha Ni Dhubghaill'}
|
|
|
|
labelBL={
|
|
|
|
<span className="flex flex-row gap-1 items-center">
|
|
|
|
{available ? (
|
|
|
|
<>
|
|
|
|
<OkIcon className="w-4 h-4 text-success" stroke={4} /> {t('usernameAvailable')}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<NoIcon className="w-4 h-4 text-error" stroke={3} /> {t('usernameNotAvailable')}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
docs={<DynamicOrgDocs language={i18n.language} path={`site/account/username`} />}
|
|
|
|
/>
|
2023-02-12 18:35:54 +01:00
|
|
|
<button className={btnClasses} disabled={!available} onClick={save}>
|
|
|
|
<span className="flex flex-row items-center gap-2">
|
2023-08-19 18:01:22 +02:00
|
|
|
{available ? t('save') : t('usernameNotAvailable')}
|
2023-02-12 18:35:54 +01:00
|
|
|
</span>
|
2023-01-24 21:17:49 +01:00
|
|
|
</button>
|
|
|
|
|
2023-01-23 20:23:53 +01:00
|
|
|
{welcome ? (
|
|
|
|
<>
|
2023-04-28 21:23:06 +02:00
|
|
|
<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}
|
|
|
|
</>
|
2023-02-12 18:35:54 +01:00
|
|
|
) : (
|
2023-08-19 18:01:22 +02:00
|
|
|
<BackToAccountButton />
|
2023-02-12 18:35:54 +01:00
|
|
|
)}
|
2023-03-27 19:21:27 +02:00
|
|
|
</div>
|
2023-01-23 20:23:53 +01:00
|
|
|
)
|
|
|
|
}
|