
This removes the useApp hook from all org pages in favor of various context. This means there is no longer global state that gets passed around, instead each component that requires access to something shared (like account, or navigation) can just use the context instead. This is a first step, as a lot of shared components stil rely on app not to mention the dev and lab sites.
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
// Dependencies
|
|
import { useState, useContext } from 'react'
|
|
import { useTranslation } from 'next-i18next'
|
|
// 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'
|
|
// Context
|
|
import { LoadingContext } from 'shared/context/loading-context.mjs'
|
|
// Components
|
|
import { BackToAccountButton, Choice } from './shared.mjs'
|
|
// Config
|
|
import { freeSewingConfig as conf } from 'site/freesewing.config.mjs'
|
|
|
|
export const ns = ['account', 'locales', 'toast']
|
|
|
|
export const LanguageSettings = ({ title = false }) => {
|
|
// Context
|
|
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
|
|
|
|
// Hooks
|
|
const { account, setAccount, token } = useAccount()
|
|
const backend = useBackend(token)
|
|
const toast = useToast()
|
|
const { t } = useTranslation(ns)
|
|
|
|
// State
|
|
const [language, setLanguage] = useState(account.language || 'en')
|
|
|
|
// Helper method to update the account
|
|
const update = async (lang) => {
|
|
if (lang !== language) {
|
|
startLoading()
|
|
setLanguage(lang)
|
|
const result = await backend.updateAccount({ language: lang })
|
|
if (result.success) {
|
|
setAccount(result.data.account)
|
|
toast.for.settingsSaved()
|
|
} else toast.for.backendError()
|
|
stopLoading()
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-xl">
|
|
{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>
|
|
))}
|
|
<BackToAccountButton loading={loading} />
|
|
</div>
|
|
)
|
|
}
|