portail-associations/shared/materialEvent.ts

72 lines
2.1 KiB
TypeScript

export const materialEventItems = [
{ key: "tente3x3", label: "Tente 3*3" },
{ key: "chapiteau5x5", label: "Chapiteau 5*5" },
{ key: "podium", label: "Podium" },
{ key: "autres", label: "Autres" },
] as const;
export const materialEventInventory: Record<(typeof materialEventItems)[number]["key"], number | null> = {
tente3x3: 12,
chapiteau5x5: 6,
podium: 2,
autres: null,
};
export const materialEventReplacementValues: Record<(typeof materialEventItems)[number]["key"], number | null> = {
tente3x3: 45000,
chapiteau5x5: 120000,
podium: 250000,
autres: null,
};
export type MaterialEventItemKey = (typeof materialEventItems)[number]["key"];
export type MaterialEventSelection = Record<MaterialEventItemKey, boolean>;
export type MaterialEventQuantities = Record<MaterialEventItemKey, string>;
export type MaterialEventQuantityMap = Record<MaterialEventItemKey, number>;
export const emptyMaterialEventSelection = (): MaterialEventSelection => ({
tente3x3: false,
chapiteau5x5: false,
podium: false,
autres: false,
});
export const emptyMaterialEventQuantities = (): MaterialEventQuantities => ({
tente3x3: "",
chapiteau5x5: "",
podium: "",
autres: "",
});
export function getMaterialEventLabel(key: MaterialEventItemKey | string) {
return materialEventItems.find((item) => item.key === key)?.label || String(key);
}
export function parseMaterialEventQuantity(value: unknown) {
const parsed = Number.parseInt(String(value ?? "").trim(), 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
}
export function emptyMaterialEventQuantityMap(initialValue: number = 0): MaterialEventQuantityMap {
return {
tente3x3: initialValue,
chapiteau5x5: initialValue,
podium: initialValue,
autres: initialValue,
};
}
export function sanitizeMaterialEventQuantityMap(rawValue: unknown): MaterialEventQuantityMap {
const next = emptyMaterialEventQuantityMap();
if (!rawValue || typeof rawValue !== "object") {
return next;
}
for (const item of materialEventItems) {
const value = (rawValue as Record<string, unknown>)[item.key];
next[item.key] = parseMaterialEventQuantity(value);
}
return next;
}