You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wiki/frontend/src/stores/common.js

42 lines
1.0 KiB

import { defineStore } from 'pinia'
import { difference } from 'es-toolkit/array'
export const useCommonStore = defineStore('common', {
state: () => ({
routerLoading: false,
locale: localStorage.getItem('locale') || 'en',
desiredLocale: localStorage.getItem('locale'),
blocksLoaded: []
}),
getters: {},
actions: {
async fetchLocaleStrings (locale) {
try {
return API_CLIENT.get(`locales/${locale}/strings`).json()
} catch (err) {
console.warn(err)
throw err
}
},
setLocale (locale) {
this.$patch({
locale,
desiredLocale: locale
})
localStorage.setItem('locale', locale)
},
async loadBlocks (blocks = []) {
const toLoad = difference(blocks, this.blocksLoaded)
for (const block of toLoad) {
try {
await import(/* @vite-ignore */ `/_blocks/${block}.js`)
this.blocksLoaded.push(block)
} catch (err) {
console.warn(`Failed to load ${block}: ${err.message}`)
}
}
}
}
})