import { useCallback } from 'react'; import type { LunaticComponentDefinition, LunaticReducerState } from '../type'; import type { LunaticComponentProps } from '../../components/type'; import { isObject } from '../../utils/object'; /** * Check if a page has one response (value is filled for at least one field). */ export function usePageHasResponse( components: LunaticComponentProps[], executeExpression: LunaticReducerState['executeExpression'] ): () => boolean { return useCallback(() => { return hasOneResponse(components, executeExpression); }, [components, executeExpression]); } const hasMissingResponse = (component: LunaticComponentProps): boolean => { if ('missingResponse' in component && component.missingResponse?.value) return true; return false; }; const hasArbitraryValue = (component: LunaticComponentProps): boolean => { if ('arbitraryValue' in component && component.arbitraryValue !== undefined) return true; return false; }; const hasOneResponseInQuestion = ( component: LunaticComponentProps<'Question'>, executeExpression: LunaticReducerState['executeExpression'] ): boolean => { return ( 'components' in component && Array.isArray(component.components) && hasOneResponse(component.components, executeExpression) ); }; // For Table, we have to extract components from its body and apply isSubComponentsEmpty function const hasOneResponseInTable = ( component: LunaticComponentProps<'Table'>, executeExpression: LunaticReducerState['executeExpression'] ): boolean => { const childrenComponent = component.body.reduce((_, row) => { const componentsInRow = row.filter( (cell) => isObject(cell) && 'componentType' in cell ); return [..._, ...componentsInRow]; }, [] as LunaticComponentProps[]); return !isSubComponentsEmpty(childrenComponent, executeExpression); }; const hasOneResponseInRosterForLoop = ( component: LunaticComponentProps<'RosterForLoop'>, executeExpression: LunaticReducerState['executeExpression'] ): boolean => { return ( 'components' in component && Array.isArray(component.components) && !isSubComponentsEmpty(component.components, executeExpression) ); }; function hasOneResponse( components: LunaticComponentProps[], executeExpression: LunaticReducerState['executeExpression'] ): boolean { if (!Array.isArray(components) || components.length === 0) { return true; } for (const component of components) { // Some components are considered as "filled" by default // We assume they are not in the same page has other components if ( ['PairwiseLinks', 'Roundabout', 'Sequence', 'Subsequence'].includes( component.componentType ?? '' ) ) { return true; } // We have a missing response for this component if (hasMissingResponse(component)) return true; // We have an arbitrary response for this component if (hasArbitraryValue(component)) return true; // For Table, we have to extract components from its body and apply isSubComponentsEmpty function if ( component.componentType === 'Table' && hasOneResponseInTable(component, executeExpression) ) { return true; } // We found a value in one of the root component. Used for Loop if ('value' in component && !isEmpty(component.value)) { return true; } // For Question, we need to check subcomponents if ( component.componentType === 'Question' && hasOneResponseInQuestion(component, executeExpression) ) return true; // For rosterForLoop we need to inspect child components if ( component.componentType === 'RosterForLoop' && hasOneResponseInRosterForLoop(component, executeExpression) ) { return true; } } return false; } /** * Check if a value is empty. * - `null`, `undefined` or `''`. * - for arrays, every item must be empty. * - for objects, every value must be empty. */ function isEmpty(value: unknown): boolean { // Array is empty if all items are empty if (Array.isArray(value)) { // We find one value that is not empty return !value.some((v) => !isEmpty(v)); } // For object inspect each values if (typeof value === 'object' && value !== null) { return isEmpty(Object.values(value)); } return (value ?? '') === '' || value === false; } /** * For complex component we need to inspect child components, interpret the response value. */ function isSubComponentsEmpty( components: (LunaticComponentProps | LunaticComponentDefinition)[], executeExpression: LunaticReducerState['executeExpression'] ): boolean { for (const component of components) { if ('responses' in component && Array.isArray(component.responses)) { for (const response of component.responses) { if ( !isEmpty( executeExpression({ type: 'VTL', value: response.response?.name }) ) ) { return false; } } } if ( 'response' in component && component.response && typeof component.response === 'object' && 'name' in component.response && typeof component.response.name === 'string' && !isEmpty( executeExpression({ type: 'VTL', value: component.response.name }) ) ) { return false; } } return true; }