2023-03-24 16:33:14 +01:00
|
|
|
// Dependencies
|
2023-08-20 18:35:19 +02:00
|
|
|
import { useState } 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'
|
2023-08-19 18:01:22 +02:00
|
|
|
import { useLoadingStatus } from 'shared/hooks/use-loading-status.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-07-23 18:42:06 +02:00
|
|
|
import { Popout } from 'shared/components/popout/index.mjs'
|
2023-02-18 13:54:35 +01:00
|
|
|
import { RightIcon } from 'shared/components/icons.mjs'
|
2023-08-25 18:47:42 +02:00
|
|
|
import { PasswordInput } from 'shared/components/inputs.mjs'
|
|
|
|
import { DynamicOrgDocs } from 'shared/components/dynamic-docs/org.mjs'
|
2023-02-18 13:54:35 +01:00
|
|
|
|
2023-08-19 18:01:22 +02:00
|
|
|
export const ns = ['account', 'status']
|
2023-02-18 13:54:35 +01:00
|
|
|
|
2023-08-26 09:15:28 +02:00
|
|
|
export const PasswordSettings = ({ welcome = false }) => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Hooks
|
2023-08-19 18:01:22 +02:00
|
|
|
const { account, setAccount } = useAccount()
|
|
|
|
const backend = useBackend()
|
2023-08-25 18:47:42 +02:00
|
|
|
const { t, i18n } = useTranslation(ns)
|
2023-08-19 18:01:22 +02:00
|
|
|
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
|
2023-04-28 21:23:06 +02:00
|
|
|
|
|
|
|
// State
|
2023-02-18 13:54:35 +01:00
|
|
|
const [password, setPassword] = useState('')
|
|
|
|
|
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-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
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)
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'settingsSaved', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-02-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-03-27 19:21:27 +02:00
|
|
|
<div className="max-w-xl">
|
2023-08-19 18:01:22 +02:00
|
|
|
<LoadingStatus />
|
2023-08-25 18:47:42 +02:00
|
|
|
<PasswordInput
|
2023-08-26 09:27:27 +02:00
|
|
|
id="account-password"
|
2023-08-25 18:47:42 +02:00
|
|
|
label={t('passwordTitle')}
|
|
|
|
current={password}
|
|
|
|
update={setPassword}
|
|
|
|
valid={(val) => val.length > 0}
|
|
|
|
placeholder={t('passwordTitle')}
|
|
|
|
docs={<DynamicOrgDocs language={i18n.language} path={`site/account/password`} />}
|
|
|
|
/>
|
2023-04-28 21:23:06 +02:00
|
|
|
<SaveSettingsButton btnProps={{ onClick: save, disabled: password.length < 4 }} />
|
2023-08-19 18:01:22 +02:00
|
|
|
{!welcome && <BackToAccountButton />}
|
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
|
|
|
)
|
|
|
|
}
|