12
This commit is contained in:
@@ -9,16 +9,17 @@ import type { WebApp } from '@twa-dev/types'
|
||||
interface AppSettings {
|
||||
fontSize: number
|
||||
locale: string
|
||||
timeZoneBot: { tz: string, offset: number, offsetString: string }
|
||||
localeBot: string
|
||||
}
|
||||
|
||||
const defaultFontSize = 16
|
||||
const minFontSize = 10
|
||||
const maxFontSize = 22
|
||||
const fontSizeStep = 2
|
||||
|
||||
const defaultSettings: AppSettings = {
|
||||
fontSize: defaultFontSize,
|
||||
locale: 'en-US'
|
||||
locale: 'en-US',
|
||||
timeZoneBot: { tz: 'Europe/Moscow', offset: 3, offsetString: '+03:00' },
|
||||
localeBot: 'en-US'
|
||||
}
|
||||
|
||||
export const useSettingsStore = defineStore('settings', () => {
|
||||
@@ -30,13 +31,17 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
const isInit = ref(false)
|
||||
|
||||
const currentFontSize = computed(() => settings.value?.fontSize ?? defaultFontSize)
|
||||
const canIncrease = computed(() => currentFontSize.value < maxFontSize)
|
||||
const canDecrease = computed(() => currentFontSize.value > minFontSize)
|
||||
|
||||
const supportLocale = [
|
||||
{ value: 'en-US', label: 'English' },
|
||||
{ value: 'ru-RU', label: 'Русский' }
|
||||
]
|
||||
|
||||
const supportFontSizes = [
|
||||
{ value: 12, label: 'settings__fontsize_small' },
|
||||
{ value: 16, label: 'settings__fontsize_medium' },
|
||||
{ value: 20, label: 'settings__fontsize_large' }
|
||||
]
|
||||
|
||||
const detectLocale = (): string => {
|
||||
const localeMap = {
|
||||
@@ -74,10 +79,12 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
const init = async () => {
|
||||
if (authStore.isAuth) {
|
||||
try {
|
||||
const response = await api.get('/customer/settings')
|
||||
const { data } = await api.get('/customer/settings')
|
||||
settings.value = {
|
||||
fontSize: response.data.data.settings.fontSize || defaultSettings.fontSize,
|
||||
locale: response.data.data.settings.locale || detectLocale()
|
||||
fontSize: data.data.settings.fontSize || defaultSettings.fontSize,
|
||||
locale: data.data.settings.locale || detectLocale(),
|
||||
timeZoneBot: data.data.settings.timeZone || defaultSettings.timeZoneBot,
|
||||
localeBot: data.data.settings.localeBot || detectLocale()
|
||||
}
|
||||
} catch {
|
||||
settings.value.locale = detectLocale()
|
||||
@@ -93,12 +100,6 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
isInit.value = true
|
||||
}
|
||||
|
||||
const updateLocale = async (newLocale: string) => {
|
||||
settings.value.locale = newLocale
|
||||
applyLocale()
|
||||
await saveSettings()
|
||||
}
|
||||
|
||||
const saveSettings = async () => {
|
||||
await api.put('/customer/settings', { settings: settings.value })
|
||||
}
|
||||
@@ -110,19 +111,6 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
await saveSettings()
|
||||
}
|
||||
|
||||
const clampFontSize = (size: number) =>
|
||||
Math.max(minFontSize, Math.min(size, maxFontSize))
|
||||
|
||||
const increaseFontSize = async () => {
|
||||
const newSize = clampFontSize(currentFontSize.value + fontSizeStep)
|
||||
await updateSettings({ fontSize: newSize })
|
||||
}
|
||||
|
||||
const decreaseFontSize = async () => {
|
||||
const newSize = clampFontSize(currentFontSize.value - fontSizeStep)
|
||||
await updateSettings({ fontSize: newSize })
|
||||
}
|
||||
|
||||
watch(() => authStore.isAuth, (newVal) => {
|
||||
if (newVal !== undefined) void init()
|
||||
}, { immediate: true })
|
||||
@@ -130,14 +118,10 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
return {
|
||||
settings,
|
||||
supportLocale,
|
||||
supportFontSizes,
|
||||
isInit,
|
||||
currentFontSize,
|
||||
canIncrease,
|
||||
canDecrease,
|
||||
init,
|
||||
increaseFontSize,
|
||||
decreaseFontSize,
|
||||
updateSettings,
|
||||
updateLocale
|
||||
updateSettings
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user