2023-01-23 20:23:53 +01:00
|
|
|
import { useState } from 'react'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import useBackend from 'site/hooks/useBackend.js'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { Choice } from '../shared.js'
|
2023-01-24 21:17:49 +01:00
|
|
|
import OkIcon from 'shared/components/icons/ok.js'
|
|
|
|
import NoIcon from 'shared/components/icons/no.js'
|
2023-01-23 20:23:53 +01:00
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
export const namespaces = ['username']
|
2023-01-23 20:23:53 +01:00
|
|
|
|
|
|
|
const welcomeSteps = {
|
|
|
|
1: {
|
|
|
|
href: '/docs/guide',
|
|
|
|
steps: 0,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
href: '/docs/guide',
|
|
|
|
steps: 3,
|
|
|
|
},
|
|
|
|
3: {
|
2023-01-24 21:17:49 +01:00
|
|
|
href: '/docs/guide',
|
2023-01-23 20:23:53 +01:00
|
|
|
steps: 5,
|
|
|
|
},
|
|
|
|
4: {
|
2023-01-24 21:17:49 +01:00
|
|
|
href: '/welcome/bio',
|
2023-01-23 20:23:53 +01:00
|
|
|
steps: 7,
|
|
|
|
},
|
|
|
|
5: {
|
|
|
|
href: '/',
|
|
|
|
steps: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
const UsernameSettings = ({ app, title = false, welcome = false }) => {
|
2023-01-23 20:23:53 +01:00
|
|
|
const backend = useBackend(app)
|
|
|
|
const { t } = useTranslation(namespaces)
|
2023-01-24 21:17:49 +01:00
|
|
|
const [username, setUsername] = useState(app.account.username)
|
|
|
|
const [available, setAvailable] = useState(true)
|
|
|
|
const [checking, setChecking] = useState(false)
|
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)
|
|
|
|
setChecking(true)
|
|
|
|
const free = await backend.isUsernameAvailable(evt.target.value)
|
|
|
|
setChecking(false)
|
|
|
|
setAvailable(free)
|
2023-01-23 20:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
const save = async () => {
|
|
|
|
const result = await backend.updateAccount({ username })
|
|
|
|
}
|
|
|
|
|
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 ? (
|
|
|
|
<>
|
|
|
|
<Link
|
|
|
|
href={welcomeSteps[app.account.control].href}
|
|
|
|
className="btn btn-primary w-full mt-12"
|
|
|
|
>
|
|
|
|
{t('continue')}
|
|
|
|
</Link>
|
|
|
|
{welcomeSteps[app.account.control].steps ? (
|
|
|
|
<>
|
|
|
|
<progress
|
|
|
|
className="progress progress-primary w-full mt-12"
|
2023-01-24 21:17:49 +01:00
|
|
|
value={500 / welcomeSteps[app.account.control].steps}
|
2023-01-23 20:23:53 +01:00
|
|
|
max="100"
|
|
|
|
></progress>
|
|
|
|
<span className="pt-4 text-sm font-bold opacity-50">
|
2023-01-24 21:17:49 +01:00
|
|
|
5 / {welcomeSteps[app.account.control].steps}
|
2023-01-23 20:23:53 +01:00
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:17:49 +01:00
|
|
|
export default UsernameSettings
|