1
0
Fork 0
freesewing/sites/shared/components/account/language.mjs

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
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'
// Components
2023-02-19 20:58:06 +01:00
import { BackToAccountButton, Choice } from './shared.mjs'
// 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-03-24 16:33:14 +01:00
const { account, setAccount, token } = useAccount()
const backend = useBackend(token)
const toast = useToast()
const { t } = useTranslation(ns)
2023-03-24 16:33:14 +01:00
const [language, setLanguage] = useState(account.language || 'en')
const update = async (lang) => {
if (lang !== language) {
app.startLoading()
setLanguage(lang)
const result = await backend.updateAccount({ language: lang })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
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}
{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>
))}
2023-03-24 16:33:14 +01:00
<BackToAccountButton loading={app.state.loading} />
</>
)
}