2023-02-12 18:35:54 +01:00
|
|
|
// Hooks
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { useBackend } from 'site/hooks/useBackend.mjs'
|
|
|
|
import { useToast } from 'site/hooks/useToast.mjs'
|
|
|
|
// 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-12 18:35:54 +01:00
|
|
|
|
|
|
|
export const ns = ['account']
|
|
|
|
|
2023-02-18 16:11:02 +01:00
|
|
|
export const EmailSettings = ({ app, title = false }) => {
|
2023-02-12 18:35:54 +01:00
|
|
|
const backend = useBackend(app)
|
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const toast = useToast()
|
|
|
|
const [email, setEmail] = useState(app.account.email)
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
const result = await backend.updateAccount({ email })
|
|
|
|
if (result) toast.for.settingsSaved()
|
|
|
|
else toast.for.backendError()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 18:35:54 +01:00
|
|
|
<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>
|
2023-02-19 20:58:06 +01:00
|
|
|
<button className="btn mt-4 btn-primary w-full" onClick={save} disabled={!valid}>
|
|
|
|
{t('save')}
|
|
|
|
</button>
|
2023-02-12 18:35:54 +01:00
|
|
|
<BackToAccountButton loading={app.loading} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|