ldc-showroom/stores/useAppStore.ts

63 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import AppState from "../classes/AppState";
2024-01-11 15:21:59 +01:00
export const useAppStore = defineStore("appStore", {
state: () => ({
2024-01-17 17:44:12 +01:00
data: new AppState({} as TypeApp, "fr", "fr"),
activeBrand: null,
2024-01-11 17:43:37 +01:00
activeRange: null,
2024-01-11 15:21:59 +01:00
} as {
data: AppState;
activeBrand: TypeBrand | null;
2024-01-11 17:43:37 +01:00
activeRange: TypeRange | null;
2024-01-11 15:21:59 +01:00
}),
actions: {
2024-01-17 17:44:12 +01:00
setupAppData(dataJson: TypeApp, locale: string, fallbackLocale: string): void {
this.data = new AppState(dataJson, locale, fallbackLocale);
2024-01-11 15:21:59 +01:00
},
setActiveBrand(brand: TypeBrand|null): void {
this.activeBrand = brand;
},
2024-01-11 17:43:37 +01:00
setActiveRange(range: TypeRange|null): void {
this.activeRange = range;
2024-01-11 17:20:05 +01:00
},
2024-01-12 11:50:21 +01:00
getNextRange(): TypeRange | null {
if (!this.activeRange) {
return null;
}
const ranges = this.data.ranges(this.data.brandFromRange(this.activeRange));
const activeRangeIndex = ranges.findIndex(range => this.activeRange!.id === range.id);
return activeRangeIndex === -1 ? null :
ranges.at(activeRangeIndex+1) ?? null;
},
getPreviousRange(): TypeRange| null {
if (!this.activeRange) {
return null;
}
const ranges = this.data.ranges(this.data.brandFromRange(this.activeRange));
const activeRangeIndex = ranges.findIndex(range => this.activeRange!.id === range.id);
return (activeRangeIndex === -1 || (activeRangeIndex-1 < 0)) ? null :
ranges.at(activeRangeIndex-1) ?? null;
2024-01-15 10:22:37 +01:00
},
getSettings(): TypeSettings {
return this.data.settings();
2024-01-16 10:25:13 +01:00
},
getFont(fontId: number): TypeFont | null {
const font = this.data.font(fontId);
if (!font) {
return null;
}
return font;
2024-01-17 17:44:12 +01:00
},
getI18n() {
return this.data.geti18N();
},
switchLocale(locale: string): void {
this.data.switchLocale(locale);
2024-01-12 11:50:21 +01:00
}
2024-01-11 15:21:59 +01:00
}
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useAppStore, import.meta.hot));
}