Files
webshop/shop/lib/wizard-state.ts
DanielS 43e6a92663
All checks were successful
Staging Build / build (push) Successful in 2m48s
feat: implement category flags, drop global tables, fix sidebar sticky layout
2026-07-10 15:27:55 +02:00

40 lines
1.1 KiB
TypeScript

import { Product, ProductModule, Category, WizardSelections } from './types';
/**
* Handle toggling of a module selection.
* Handles category-specific allow_multiselect rules.
*/
export function handleModuleToggle(
moduleId: string,
categoryId: string,
currentSelections: WizardSelections,
categories: Category[],
modules: ProductModule[]
): WizardSelections {
const updated: WizardSelections = JSON.parse(JSON.stringify(currentSelections));
const category = categories.find(c => c.id === categoryId);
const allowMultiple = category ? category.allow_multiselect : true;
if (!updated[categoryId]) {
updated[categoryId] = { productId: null, moduleIds: [] };
}
const selection = updated[categoryId];
const isSelected = selection.moduleIds.includes(moduleId);
if (isSelected) {
// Toggle off
selection.moduleIds = selection.moduleIds.filter(id => id !== moduleId);
} else {
// Toggle on
if (!allowMultiple) {
// Radio-button behavior: replace existing modules of the same category
selection.moduleIds = [moduleId];
} else {
selection.moduleIds.push(moduleId);
}
}
return updated;
}