2023-03-24 16:33:14 +01:00
|
|
|
// Dependencies
|
2023-04-28 21:23:06 +02:00
|
|
|
import { useState, useContext } from 'react'
|
2023-02-18 13:54:35 +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'
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingContext } from 'shared/context/loading-context.mjs'
|
2023-02-18 13:54:35 +01:00
|
|
|
// Components
|
|
|
|
import Link from 'next/link'
|
2023-02-19 20:58:06 +01:00
|
|
|
import { BackToAccountButton } from './shared.mjs'
|
2023-05-17 17:04:15 +02:00
|
|
|
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
|
2023-02-18 13:54:35 +01:00
|
|
|
import { Popout } from 'shared/components/popout.mjs'
|
|
|
|
import { RightIcon } from 'shared/components/icons.mjs'
|
|
|
|
|
2023-02-19 20:49:15 +01:00
|
|
|
export const ns = ['account', 'toast']
|
2023-02-18 13:54:35 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
export const PasswordSettings = ({ 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)
|
2023-02-18 13:54:35 +01:00
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const toast = useToast()
|
2023-04-28 21:23:06 +02:00
|
|
|
|
|
|
|
// State
|
2023-02-18 13:54:35 +01:00
|
|
|
const [password, setPassword] = useState('')
|
2023-02-26 14:19:43 +01:00
|
|
|
const [reveal, setReveal] = useState(false)
|
2023-02-18 13:54:35 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to save password to account
|
2023-02-18 13:54:35 +01:00
|
|
|
const save = async () => {
|
2023-04-28 21:23:06 +02:00
|
|
|
startLoading()
|
2023-02-18 13:54:35 +01:00
|
|
|
const result = await backend.updateAccount({ password })
|
2023-03-24 16:33:14 +01:00
|
|
|
if (result.success) {
|
|
|
|
setAccount(result.data.account)
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
2023-04-28 21:23:06 +02:00
|
|
|
stopLoading()
|
2023-02-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-03-27 19:21:27 +02:00
|
|
|
<div className="max-w-xl">
|
2023-02-18 16:11:02 +01:00
|
|
|
{title ? <h2 className="text-4xl">{t('passwordTitle')}</h2> : null}
|
2023-02-26 14:19:43 +01:00
|
|
|
<div className="flex flex-row items-center mt-4 gap-2">
|
2023-02-18 13:54:35 +01:00
|
|
|
<input
|
|
|
|
value={password}
|
|
|
|
onChange={(evt) => setPassword(evt.target.value)}
|
|
|
|
className="input w-full input-bordered flex flex-row"
|
2023-02-26 14:19:43 +01:00
|
|
|
type={reveal ? 'text' : 'password'}
|
2023-02-19 20:49:15 +01:00
|
|
|
placeholder={t('newPasswordPlaceholder')}
|
2023-02-18 13:54:35 +01:00
|
|
|
/>
|
2023-02-26 14:19:43 +01:00
|
|
|
<button
|
|
|
|
className="btn hover:bg-opacity-10 border-0 btn-outline"
|
|
|
|
onClick={() => setReveal(!reveal)}
|
|
|
|
>
|
|
|
|
<span role="img" className="text-3xl">
|
|
|
|
{reveal ? '👀' : '🙈'}
|
|
|
|
</span>
|
|
|
|
</button>
|
2023-02-18 13:54:35 +01:00
|
|
|
</div>
|
2023-04-28 21:23:06 +02:00
|
|
|
<SaveSettingsButton btnProps={{ onClick: save, disabled: password.length < 4 }} />
|
|
|
|
{!welcome && <BackToAccountButton loading={loading} />}
|
2023-03-24 16:33:14 +01:00
|
|
|
{!account.mfaEnabled && (
|
2023-02-18 13:54:35 +01:00
|
|
|
<Popout tip>
|
|
|
|
<h5>{t('mfaTipTitle')}</h5>
|
|
|
|
<p>{t('mfaTipMsg')}</p>
|
|
|
|
<p className="text-right m-0 pt-0">
|
|
|
|
<Link className="btn btn-secondary btn-accent" href="/account/mfa">
|
|
|
|
{t('mfa')} <RightIcon className="h-6 w-6 ml-2" />
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</Popout>
|
|
|
|
)}
|
2023-03-27 19:21:27 +02:00
|
|
|
</div>
|
2023-02-18 13:54:35 +01:00
|
|
|
)
|
|
|
|
}
|