2024-12-23 18:25:48 +01:00
|
|
|
// Dependencies
|
|
|
|
import { welcomeSteps } from './shared.mjs'
|
|
|
|
|
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus'
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
import React, { useState, useContext } from 'react'
|
|
|
|
import { useAccount } from '@freesewing/react/hooks/useAccount'
|
|
|
|
import { useBackend } from '@freesewing/react/hooks/useBackend'
|
|
|
|
|
|
|
|
// Components
|
|
|
|
import { Link as WebLink } from '@freesewing/react/components/Link'
|
2024-12-24 16:34:26 +01:00
|
|
|
import { NoIcon, OkIcon, SaveIcon, RightIcon } from '@freesewing/react/components/Icon'
|
2024-12-23 18:25:48 +01:00
|
|
|
import { StringInput } from '@freesewing/react/components/Input'
|
2024-12-24 16:34:26 +01:00
|
|
|
import { IconButton } from '@freesewing/react/components/Button'
|
|
|
|
import { WelcomeIcons } from './shared.mjs'
|
2024-12-23 18:25:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Component for the account/username page
|
|
|
|
*
|
|
|
|
* @params {object} props - All React props
|
|
|
|
* @params {bool} props.welcome - Set to true to use this component on the welcome page
|
|
|
|
* @params {function} props.Link - A framework specific Link component for client-side routing
|
|
|
|
*/
|
|
|
|
export const Username = ({ welcome = false, Link = false }) => {
|
|
|
|
if (!Link) Link = WebLink
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
const { account, setAccount } = useAccount()
|
|
|
|
const backend = useBackend()
|
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
|
|
|
const [username, setUsername] = useState(account.username)
|
|
|
|
const [available, setAvailable] = useState(true)
|
|
|
|
|
|
|
|
const update = async (value) => {
|
|
|
|
if (value !== username) {
|
|
|
|
setUsername(value)
|
|
|
|
const result = await backend.isUsernameAvailable(value)
|
|
|
|
setAvailable(result.available ? true : false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
setLoadingStatus([true, 'Saving username'])
|
|
|
|
const [status, body] = await backend.updateAccount({ username })
|
|
|
|
if (status === 200 && body.result === 'success') {
|
|
|
|
setAccount(body.account)
|
|
|
|
setLoadingStatus([true, 'Username updated', true, true])
|
|
|
|
} else setLoadingStatus([true, 'Something went wrong. Please report this', true, true])
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextHref =
|
|
|
|
welcomeSteps[account.control].length > 5
|
|
|
|
? '/welcome/' + welcomeSteps[account.control][5]
|
|
|
|
: '/docs/about/guide'
|
|
|
|
|
|
|
|
let btnClasses = 'daisy-btn mt-4 capitalize '
|
|
|
|
if (welcome) btnClasses += 'w-64 daisy-btn-secondary'
|
|
|
|
else btnClasses += 'w-full daisy-btn-primary'
|
|
|
|
|
|
|
|
return (
|
2024-12-26 18:33:49 +01:00
|
|
|
<div className="tw-w-full">
|
2024-12-23 18:25:48 +01:00
|
|
|
<StringInput
|
|
|
|
id="account-username"
|
|
|
|
label="Username"
|
|
|
|
current={username}
|
|
|
|
update={update}
|
|
|
|
valid={() => available}
|
|
|
|
placeholder={'Sorcha Ni Dhubghaill'}
|
|
|
|
labelBL={
|
2024-12-26 18:33:49 +01:00
|
|
|
<span className="tw-flex tw-flex-row tw-gap-1 tw-items-center">
|
2024-12-23 18:25:48 +01:00
|
|
|
{available ? (
|
|
|
|
<>
|
2024-12-26 18:33:49 +01:00
|
|
|
<OkIcon className="tw-w-4 tw-h-4 tw-text-success" stroke={4} /> Username is
|
|
|
|
available
|
2024-12-23 18:25:48 +01:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2024-12-26 18:33:49 +01:00
|
|
|
<NoIcon className="tw-w-4 tw-h-4 tw-text-error" stroke={3} /> This username is taken
|
2024-12-23 18:25:48 +01:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
/>
|
2024-12-26 18:33:49 +01:00
|
|
|
<p className="tw-text-right">
|
2024-12-23 18:25:48 +01:00
|
|
|
<button
|
|
|
|
disabled={!available}
|
2024-12-26 18:33:49 +01:00
|
|
|
className="tw-daisy-btn tw-daisy-btn-primary tw-w-full lg:tw-w-auto tw-mt-8"
|
2024-12-23 18:25:48 +01:00
|
|
|
onClick={save}
|
|
|
|
>
|
|
|
|
<SaveIcon /> Save Username
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
{welcome ? (
|
|
|
|
<>
|
2024-12-26 18:33:49 +01:00
|
|
|
<IconButton href={nextHref} className="tw-mt-4">
|
2024-12-24 16:34:26 +01:00
|
|
|
<RightIcon stroke={3} /> Continue
|
|
|
|
</IconButton>
|
2024-12-23 18:25:48 +01:00
|
|
|
{welcomeSteps[account.control].length > 0 ? (
|
|
|
|
<>
|
|
|
|
<progress
|
2024-12-26 18:33:49 +01:00
|
|
|
className="tw-daisy-progress tw-daisy-progress-primary tw-w-full tw-mt-12"
|
2024-12-23 18:25:48 +01:00
|
|
|
value={500 / welcomeSteps[account.control].length}
|
|
|
|
max="100"
|
|
|
|
></progress>
|
2024-12-26 18:33:49 +01:00
|
|
|
<span className="tw-pt-4 tw-text-sm tw-font-bold tw-opacity-50">
|
2024-12-23 18:25:48 +01:00
|
|
|
5 / {welcomeSteps[account.control].length}
|
|
|
|
</span>
|
2024-12-24 16:34:26 +01:00
|
|
|
<WelcomeIcons
|
2024-12-23 18:25:48 +01:00
|
|
|
done={welcomeSteps[account.control].slice(0, 4)}
|
|
|
|
todo={welcomeSteps[account.control].slice(5)}
|
|
|
|
current="username"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|