ldc-showroom/stores/useAppStore.ts
2024-01-17 17:44:12 +01:00

62 lines
2.2 KiB
TypeScript

import AppState from "../classes/AppState";
export const useAppStore = defineStore("appStore", {
state: () => ({
data: new AppState({} as TypeApp, "fr", "fr"),
activeBrand: null,
activeRange: null,
} as {
data: AppState;
activeBrand: TypeBrand | null;
activeRange: TypeRange | null;
}),
actions: {
setupAppData(dataJson: TypeApp, locale: string, fallbackLocale: string): void {
this.data = new AppState(dataJson, locale, fallbackLocale);
},
setActiveBrand(brand: TypeBrand|null): void {
this.activeBrand = brand;
},
setActiveRange(range: TypeRange|null): void {
this.activeRange = range;
},
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;
},
getSettings(): TypeSettings {
return this.data.settings();
},
getFont(fontId: number): TypeFont | null {
const font = this.data.font(fontId);
if (!font) {
return null;
}
return font;
},
getI18n() {
return this.data.geti18N();
},
switchLocale(locale: string): void {
this.data.switchLocale(locale);
}
}
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useAppStore, import.meta.hot));
}