40 lines
1.1 KiB
TypeScript
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;
|
|
}
|