2023-01-29 16:44:02 +01:00
|
|
|
// Hooks
|
2023-01-23 20:23:53 +01:00
|
|
|
import { useState } from 'react'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { useBackend } from 'site/hooks/useBackend.mjs'
|
|
|
|
// Components
|
2023-01-23 20:23:53 +01:00
|
|
|
import Link from 'next/link'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { Icons, welcomeSteps } from '../shared.mjs'
|
|
|
|
import { OkIcon, NoIcon } from 'shared/components/icons.mjs'
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-01-28 18:10:29 +01:00
|
|
|
export const ns = ['username']
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-01-29 16:44:02 +01:00
|
|
|
export const UsernameSettings = ({ app, title = false, welcome = false }) => {
|
2023-01-23 20:23:53 +01:00
|
|
|
const backend = useBackend(app)
|
2023-01-28 18:10:29 +01:00
|
|
|
const { t } = useTranslation(ns)
|
2023-01-24 21:17:49 +01:00
|
|
|
const [username, setUsername] = useState(app.account.username)
|
|
|
|
const [available, setAvailable] = useState(true)
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-01-24 21:17:49 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
const save = async () => {
|
2023-01-27 21:09:44 +01:00
|
|
|
await backend.updateAccount({ username })
|
2023-01-24 21:17:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 19:31:23 +01:00
|
|
|
const nextHref =
|
|
|
|
welcomeSteps[app.account.control].length > 4
|
|
|
|
? '/welcome/' + welcomeSteps[app.account.control][5]
|
|
|
|
: '/docs/guide'
|
|
|
|
|
2023-01-23 20:23:53 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{title ? <h1 className="text-4xl">{t('title')}</h1> : null}
|
2023-01-24 21:17:49 +01:00
|
|
|
<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={`btn btn-secondary mt-4 ${available ? '' : 'btn-disabled'} w-64`}
|
|
|
|
onClick={save}
|
|
|
|
>
|
|
|
|
{available ? 'Save' : 'Username is not available'}
|
|
|
|
</button>
|
|
|
|
|
2023-01-23 20:23:53 +01:00
|
|
|
{welcome ? (
|
|
|
|
<>
|
2023-01-26 19:31:23 +01:00
|
|
|
<Link href={nextHref} className="btn btn-primary w-full mt-12">
|
2023-01-23 20:23:53 +01:00
|
|
|
{t('continue')}
|
|
|
|
</Link>
|
2023-01-26 19:31:23 +01:00
|
|
|
{welcomeSteps[app.account.control].length > 0 ? (
|
2023-01-23 20:23:53 +01:00
|
|
|
<>
|
|
|
|
<progress
|
|
|
|
className="progress progress-primary w-full mt-12"
|
2023-01-26 19:31:23 +01:00
|
|
|
value={500 / welcomeSteps[app.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-01-26 19:31:23 +01:00
|
|
|
5 / {welcomeSteps[app.account.control].length}
|
2023-01-23 20:23:53 +01:00
|
|
|
</span>
|
2023-01-26 19:31:23 +01:00
|
|
|
<Icons
|
|
|
|
done={welcomeSteps[app.account.control].slice(0, 4)}
|
|
|
|
todo={welcomeSteps[app.account.control].slice(5)}
|
|
|
|
current="username"
|
|
|
|
/>
|
2023-01-23 20:23:53 +01:00
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|