2023-03-24 16:33:14 +01:00
|
|
|
// Dependencies
|
2023-02-12 18:35:54 +01:00
|
|
|
import { useState } from 'react'
|
|
|
|
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-02-12 18:35:54 +01:00
|
|
|
// Verification methods
|
|
|
|
import { validateEmail, validateTld } from 'site/utils.mjs'
|
|
|
|
// Components
|
2023-02-19 20:58:06 +01:00
|
|
|
import { BackToAccountButton } from './shared.mjs'
|
2023-02-26 13:16:40 +01:00
|
|
|
import { Popout } from 'shared/components/popout.mjs'
|
2023-02-12 18:35:54 +01:00
|
|
|
|
2023-02-26 13:16:40 +01:00
|
|
|
export const ns = ['account', 'toast']
|
2023-02-12 18:35:54 +01:00
|
|
|
|
2023-02-18 16:11:02 +01:00
|
|
|
export const EmailSettings = ({ app, title = false }) => {
|
2023-03-24 16:33:14 +01:00
|
|
|
const { account, setAccount, token } = useAccount()
|
|
|
|
const backend = useBackend(token)
|
2023-02-12 18:35:54 +01:00
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const toast = useToast()
|
2023-03-24 16:33:14 +01:00
|
|
|
const [email, setEmail] = useState(account.email)
|
2023-02-26 13:16:40 +01:00
|
|
|
const [changed, setChanged] = useState(false)
|
2023-02-12 18:35:54 +01:00
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
const result = await backend.updateAccount({ email })
|
2023-03-24 16:33:14 +01:00
|
|
|
if (result.success) {
|
|
|
|
setAccount(result.data.account)
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
2023-02-26 13:16:40 +01:00
|
|
|
setChanged(true)
|
2023-02-12 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const valid = (validateEmail(email) && validateTld(email)) || false
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-02-18 16:11:02 +01:00
|
|
|
{title ? <h2 className="text-4xl">{t('emailTitle')}</h2> : null}
|
2023-02-26 13:16:40 +01:00
|
|
|
{changed ? (
|
|
|
|
<Popout note>
|
|
|
|
<h3>{t('oneMoreThing')}</h3>
|
|
|
|
<p>{t('emailChangeConfirmation')}</p>
|
|
|
|
</Popout>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="flex flex-row items-center mt-4">
|
|
|
|
<input
|
|
|
|
value={email}
|
|
|
|
onChange={(evt) => setEmail(evt.target.value)}
|
|
|
|
className="input w-full input-bordered flex flex-row"
|
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className="btn mt-4 btn-primary w-full"
|
|
|
|
onClick={save}
|
2023-03-24 16:33:14 +01:00
|
|
|
disabled={!valid || email.toLowerCase() === account.email}
|
2023-02-26 13:16:40 +01:00
|
|
|
>
|
|
|
|
{t('save')}
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)}
|
2023-03-24 16:33:14 +01:00
|
|
|
<BackToAccountButton loading={app.state.loading} />
|
2023-02-12 18:35:54 +01:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|