59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import Toast from "./Toast";
|
|
|
|
export default class AppState {
|
|
/** Json data as exported from zip and compliant with schemas */
|
|
private jsonData: TypeApp;
|
|
|
|
constructor(jsonData: TypeApp) {
|
|
this.jsonData = jsonData;
|
|
}
|
|
|
|
public settings(): TypeSettings {
|
|
return this.jsonData.settings;
|
|
}
|
|
|
|
// * --- BRANDS
|
|
|
|
public brands(): Array<TypeBrand> {
|
|
return this.jsonData.items;
|
|
}
|
|
|
|
public brand(slug:string): TypeBrand|undefined {
|
|
return this.jsonData.items.find(brand => brand.slug === slug);
|
|
}
|
|
|
|
public brandFromRange(range:TypeRange): TypeBrand {
|
|
const brand = this.jsonData.items.find(brand => brand.id === range.brand_id);
|
|
if (!brand) {
|
|
Toast.toast("Une erreur est survenue");
|
|
throw new Error(`Could not find brand with id ${range.brand_id}`);
|
|
}
|
|
return brand;
|
|
}
|
|
|
|
// * --- END BRANDS
|
|
|
|
// * --- RANGES
|
|
|
|
public ranges(brand: TypeBrand | null = null): Array<TypeRange> {
|
|
const ranges: Array<TypeRange> = [];
|
|
this.jsonData.items.map((brandItem) => {
|
|
if (brand === null || brand?.id === brandItem.id) {
|
|
ranges.push(...brandItem.ranges);
|
|
return brandItem;
|
|
}
|
|
return brandItem;
|
|
});
|
|
return ranges.sort((rangeA, rangeB) => rangeA.order - rangeB.order);
|
|
}
|
|
|
|
public range(slug:string): TypeRange|undefined {
|
|
return this.ranges().find(range => range.slug === slug);
|
|
}
|
|
|
|
// * --- END RANGES
|
|
|
|
public font(fontId: number): TypeFont|undefined {
|
|
return this.jsonData.fonts.find(font=> font.id === fontId);
|
|
}
|
|
}
|