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'
|
|
|
|
// Components
|
2023-02-19 20:58:06 +01:00
|
|
|
import { BackToAccountButton, Choice } from './shared.mjs'
|
2023-02-12 18:35:54 +01:00
|
|
|
// Config
|
|
|
|
import { freeSewingConfig as conf } from 'site/freesewing.config.mjs'
|
|
|
|
|
|
|
|
export const ns = ['account', 'locales', 'toast']
|
|
|
|
|
2023-02-18 16:11:02 +01:00
|
|
|
export const LanguageSettings = ({ app, title = false }) => {
|
2023-02-12 18:35:54 +01:00
|
|
|
const backend = useBackend(app)
|
|
|
|
const toast = useToast()
|
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const [language, setLanguage] = useState(app.account.language || 'en')
|
|
|
|
|
|
|
|
const update = async (lang) => {
|
|
|
|
if (lang !== language) {
|
|
|
|
app.startLoading()
|
|
|
|
setLanguage(lang)
|
|
|
|
const result = await backend.updateAccount({ language: lang })
|
|
|
|
if (result === true) toast.for.settingsSaved()
|
|
|
|
else toast.for.backendError()
|
|
|
|
app.stopLoading()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-02-18 16:11:02 +01:00
|
|
|
{title ? <h2 className="text-4xl">{t('languageTitle')}</h2> : null}
|
2023-02-12 18:35:54 +01:00
|
|
|
{conf.languages.map((val) => (
|
|
|
|
<Choice val={val} t={t} update={update} current={language} key={val}>
|
|
|
|
<span className="block text-lg leading-5">{t(`locales:${val}`)}</span>
|
|
|
|
</Choice>
|
|
|
|
))}
|
|
|
|
<BackToAccountButton loading={app.loading} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|