Skip to content

Commit

Permalink
refactor(locales): move available locales to settings, update types
Browse files Browse the repository at this point in the history
  • Loading branch information
VChet committed Oct 13, 2024
1 parent ed5dfad commit 22b2d7c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 17 additions & 5 deletions src/store/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref } from "vue";
import { createGlobalState, useStorage } from "@vueuse/core";
import { createGlobalState, useLocalStorage } from "@vueuse/core";
import type { RouteRecordName } from "vue-router";
import dayjs from "dayjs";
import { useRegisterSW } from "virtual:pwa-register/vue";
Expand All @@ -9,20 +9,31 @@ import { i18n } from "@/main";
import { useEventsStore } from "./events";
import type { Day } from "@/classes/Day";

function getDayJSLocaleData(locale: string): ILocale {
type LocaleCode = "en" | "ru";
interface SettingsStore {
locale: LocaleCode
onboarding: boolean
}

function getDayJSLocaleData(locale: LocaleCode): ILocale {
switch (locale) {
case "ru": return tamrielRu;
case "en": default: return tamrielEn;
}
}

export const useSettingsStore = createGlobalState(() => {
const settings = useStorage("settings", {
const availableLocales: { code: LocaleCode, label: string }[] = [
{ code: "en", label: "English" },
{ code: "ru", label: "Русский" }
];

const settings = useLocalStorage<SettingsStore>("settings", {
locale: "en",
onboarding: false
}, localStorage, { mergeDefaults: true });
}, { mergeDefaults: true });

async function setLocale(locale: string): Promise<void> {
async function setLocale(locale: LocaleCode): Promise<void> {
const { setEventsData } = useEventsStore();

dayjs.locale(getDayJSLocaleData(locale));
Expand All @@ -37,6 +48,7 @@ export const useSettingsStore = createGlobalState(() => {
const selectedDay = ref<Day | null>(null);

return {
availableLocales,
settings,
setLocale,

Expand Down
6 changes: 1 addition & 5 deletions src/views/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ import CommonHeader from "@/components/CommonHeader.vue";
const commitHash = import.meta.env.VITE_GIT_COMMIT_HASH;
const commitDate = composeCommitDate(import.meta.env.VITE_GIT_COMMIT_DATE);
const availableLocales = [
{ code: "en", label: "English" },
{ code: "ru", label: "Русский" }
];
const { settings, setLocale, needRefresh, updateServiceWorker } = useSettingsStore();
const { availableLocales, settings, setLocale, needRefresh, updateServiceWorker } = useSettingsStore();
const locale = ref(settings.value.locale);
watch(locale, setLocale);
</script>
Expand Down

0 comments on commit 22b2d7c

Please sign in to comment.