Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: row control #851

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/use-lunatic/commons/compile-controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isLoopComponent } from '../reducer/commons';
import { replaceComponentSequence } from '../replace-component-sequence';
import type {
LunaticComponentDefinition,
Expand All @@ -11,23 +10,37 @@ import fillComponentExpressions from './fill-components/fill-component-expressio
import getComponentsFromState from './get-components-from-state';
import { checkRoundaboutControl } from '../reducer/controls/check-roundabout-control';
import { checkBaseControl } from '../reducer/controls/check-base-control';
import type { FilledLunaticComponentProps } from './fill-components/fill-components';

export type StateForControls = Pick<
LunaticState,
'pager' | 'pages' | 'isInLoop' | 'executeExpression'
>;

type ComponentDefinition =
| LunaticComponentDefinition
| FilledLunaticComponentProps;

const isLoopComponent = (
component: ComponentDefinition
): component is ComponentDefinition & {
componentType: 'Loop' | 'RosterForLoop';
} => {
return ['Loop', 'RosterForLoop'].includes(component.componentType);
};

/**
* Check if components of the current page have errors, and return a map of error (indexed by component ID)
*/
function checkComponents(
state: StateForControls,
components: LunaticComponentDefinition[]
components: ComponentDefinition[]
): Record<string, LunaticError[]> {
let errors = {} as Record<string, LunaticError[]>;

for (const component of components) {
const { controls, id } = component;

// The component has global level controls
if (Array.isArray(controls)) {
const componentErrors = checkControls(
Expand Down Expand Up @@ -82,10 +95,15 @@ function checkControls(
* Figure out the number of iterations of a component
*/
function computeIterations(
component: LunaticComponentDefinition,
component: ComponentDefinition,
executeExpression: LunaticState['executeExpression']
): number {
if ('iterations' in component) {
if (
'iterations' in component &&
component.iterations &&
typeof component.iterations === 'object' &&
'value' in component.iterations
) {
return executeExpression<number>(component.iterations.value);
}
if ('response' in component) {
Expand All @@ -107,7 +125,7 @@ function computeIterations(
*/
function checkComponentInLoop(
state: StateForControls,
component: LunaticComponentDefinition,
component: ComponentDefinition,
errors: Record<string, LunaticError[]>
): Record<string, LunaticError[]> {
// The component has no controls, skip it
Expand All @@ -126,8 +144,13 @@ function checkComponentInLoop(
};
// The component is filtered on this iteration, skip it
if (
component.conditionFilter &&
!state.executeExpression(component.conditionFilter.value, iterationPager)
// conditionFilter can be the interpreted expression, or the object representing the expression
(typeof component.conditionFilter == 'object' &&
!state.executeExpression(
component.conditionFilter.value,
iterationPager
)) ||
component.conditionFilter === false
) {
continue;
}
Expand Down
1 change: 0 additions & 1 deletion src/use-lunatic/reducer/commons/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as resizeArrayVariable } from './resize-array-variable';
export { isLoopComponent } from './is-loop-component';
export * from './validate-condition-filter';
9 changes: 0 additions & 9 deletions src/use-lunatic/reducer/commons/is-loop-component.ts

This file was deleted.

Loading