feat: Add table logic for role and permission sections
This commit is contained in:
@@ -9,35 +9,54 @@ export function useSectionSpawning() {
|
||||
let resultSections = sections
|
||||
|
||||
for (const formElement of updatedFormElements) {
|
||||
if (!formElement.sectionSpawnTrigger || !formElement.reference) {
|
||||
const triggers = formElement.sectionSpawnTriggers
|
||||
if (!triggers || triggers.length === 0 || !formElement.reference) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Extract trigger configuration and current element value
|
||||
const trigger = formElement.sectionSpawnTrigger
|
||||
const triggerValue = getFormElementValue(formElement)
|
||||
const shouldSpawn = shouldSpawnSection(trigger, triggerValue)
|
||||
// Use resultSections to check for existing spawned sections (in case multiple spawns happen)
|
||||
const existingSpawnedSections = getSpawnedSectionsForElement(resultSections, formElement.reference)
|
||||
|
||||
// Handle three spawn states:
|
||||
// 1. Condition met but no section spawned yet → create new section
|
||||
if (shouldSpawn && existingSpawnedSections.length === 0) {
|
||||
resultSections = spawnNewSection(resultSections, formElement, trigger, triggerValue)
|
||||
}
|
||||
// 2. Condition no longer met but section exists → remove spawned section
|
||||
else if (!shouldSpawn && existingSpawnedSections.length > 0) {
|
||||
resultSections = removeSpawnedSections(resultSections, formElement.reference)
|
||||
}
|
||||
// 3. Condition still met and section exists → update section titles if value changed
|
||||
else if (shouldSpawn && existingSpawnedSections.length > 0 && triggerValue) {
|
||||
resultSections = updateSpawnedSectionTitles(resultSections, formElement.reference, trigger, triggerValue)
|
||||
// Process each trigger independently
|
||||
for (const trigger of triggers) {
|
||||
resultSections = processSingleTrigger(resultSections, formElement, trigger, triggerValue)
|
||||
}
|
||||
}
|
||||
|
||||
return resultSections
|
||||
}
|
||||
|
||||
function processSingleTrigger(
|
||||
sections: FormElementSectionDto[],
|
||||
formElement: FormElementDto,
|
||||
trigger: SectionSpawnTriggerDto,
|
||||
triggerValue: string
|
||||
): FormElementSectionDto[] {
|
||||
let resultSections = sections
|
||||
const shouldSpawn = shouldSpawnSection(trigger, triggerValue)
|
||||
// Find existing spawned section for this specific trigger (by template reference)
|
||||
const existingSpawnedSection = findSpawnedSectionForTrigger(
|
||||
resultSections,
|
||||
formElement.reference!,
|
||||
trigger.templateReference
|
||||
)
|
||||
|
||||
// Handle three spawn states:
|
||||
// 1. Condition met but no section spawned yet → create new section
|
||||
if (shouldSpawn && !existingSpawnedSection) {
|
||||
resultSections = spawnNewSection(resultSections, formElement, trigger, triggerValue)
|
||||
}
|
||||
// 2. Condition no longer met but section exists → remove spawned section
|
||||
else if (!shouldSpawn && existingSpawnedSection) {
|
||||
resultSections = removeSpawnedSectionForTrigger(resultSections, formElement.reference!, trigger.templateReference)
|
||||
}
|
||||
// 3. Condition still met and section exists → update section titles if value changed
|
||||
else if (shouldSpawn && existingSpawnedSection && triggerValue) {
|
||||
resultSections = updateSpawnedSectionTitles(resultSections, formElement.reference!, trigger, triggerValue)
|
||||
}
|
||||
|
||||
return resultSections
|
||||
}
|
||||
|
||||
function spawnNewSection(
|
||||
sections: FormElementSectionDto[],
|
||||
element: FormElementDto,
|
||||
@@ -94,8 +113,30 @@ export function useSectionSpawning() {
|
||||
})
|
||||
}
|
||||
|
||||
function removeSpawnedSections(sections: FormElementSectionDto[], elementReference: string): FormElementSectionDto[] {
|
||||
return sections.filter((section) => section.spawnedFromElementReference !== elementReference || section.isTemplate)
|
||||
function findSpawnedSectionForTrigger(
|
||||
sections: FormElementSectionDto[],
|
||||
elementReference: string,
|
||||
templateReference: string
|
||||
): FormElementSectionDto | undefined {
|
||||
return sections.find(
|
||||
(section) =>
|
||||
!section.isTemplate &&
|
||||
section.spawnedFromElementReference === elementReference &&
|
||||
section.templateReference === templateReference
|
||||
)
|
||||
}
|
||||
|
||||
function removeSpawnedSectionForTrigger(
|
||||
sections: FormElementSectionDto[],
|
||||
elementReference: string,
|
||||
templateReference: string
|
||||
): FormElementSectionDto[] {
|
||||
return sections.filter(
|
||||
(section) =>
|
||||
section.isTemplate ||
|
||||
section.spawnedFromElementReference !== elementReference ||
|
||||
section.templateReference !== templateReference
|
||||
)
|
||||
}
|
||||
|
||||
function spawnSectionFromTemplate(
|
||||
@@ -146,13 +187,6 @@ export function useSectionSpawning() {
|
||||
return trigger.sectionSpawnConditionType === VisibilityConditionType.Show ? isConditionMet : !isConditionMet
|
||||
}
|
||||
|
||||
function getSpawnedSectionsForElement(
|
||||
sections: FormElementSectionDto[],
|
||||
elementReference: string
|
||||
): FormElementSectionDto[] {
|
||||
return sections.filter((section) => !section.isTemplate && section.spawnedFromElementReference === elementReference)
|
||||
}
|
||||
|
||||
function findTemplateSection(
|
||||
sections: FormElementSectionDto[],
|
||||
templateReference: string
|
||||
|
||||
Reference in New Issue
Block a user