import { LunaticComponents, type LunaticData, type LunaticError, type LunaticSlotComponents, type LunaticSource, ModalControls, useLunatic, } from '../..'; import React, { memo, type ReactNode, useCallback, useEffect, useMemo, useState, } from 'react'; import Ajv from 'ajv/dist/2020.js'; import LunaticSchema from '../../../lunatic-schema.json'; import { Logger } from '../../utils/logger'; import { OrchestratorOverview } from './OrchestratorOverview'; import { SchemaValidator } from './SchemaValidator'; import { OrchestratorData } from './OrchestratorData'; import type { ActiveFilter, PageTag } from '../../use-lunatic/type'; import type { IndexEntry } from '../../utils/search/SearchInterface'; import { OrchestratorSidebar } from './OrchestratorSidebar'; import type { Meta, StoryObj } from '@storybook/react'; import ReactHotkeys from 'react-hot-keys'; type Props = { source: LunaticSource; data: LunaticData; // Starting page initialPage?: PageTag; // Function used to load a list of option in a suggester getReferentiel: (name: string) => Promise; // Display detail input even if the corresponding checkbox is not checked detailAlwaysDisplayed?: boolean; // Set the "remove row" button as disabled in roster for loop. disableRosterForLoopDeleteRowButton?: boolean; // Enable missing buttons missing?: boolean; // Readonly mode readOnly: boolean; // List of custom components slots: Partial; // Last reached paged (used in the overview) lastReachedPage: PageTag; // Preload suggester data at the start of the form autoSuggesterLoading: boolean; disableFiltersDescription?: boolean; activeFilters?: ActiveFilter; management?: boolean; shortcut?: boolean; activeControls?: boolean; missingStrategy?: () => void; showOverview?: boolean; disabled?: boolean; extraTabs?: TabEntry[]; multiMode: boolean; }; type TabEntry = { label: ReactNode; children: ReactNode }; function OrchestratorForStories(props: Readonly) { const { source, data, disableFiltersDescription, activeFilters, management, shortcut, activeControls, initialPage, missing, missingStrategy, slots, showOverview, getReferentiel, readOnly, disabled, detailAlwaysDisplayed, disableRosterForLoopDeleteRowButton, autoSuggesterLoading, extraTabs = [], } = props; const componentsOptions = { detailAlwaysDisplayed, disableRosterForLoopDeleteRowButton, }; const { getComponents, goPreviousPage, goNextPage, goToPage, pager, pageTag, isFirstPage, isLastPage, overview, compileControls, getData, Provider, hasPageResponse, getMultimode, } = useLunatic(source, data, { initialPage, disableFiltersDescription, activeFilters, onChange: onLogChange, getReferentiel, management, missing, missingStrategy, lastReachedPage: props.lastReachedPage, missingShortcut: { dontKnow: 'f2', refused: 'f4' }, shortcut, activeControls, withOverview: showOverview, autoSuggesterLoading, componentsOptions, }); const components = getComponents(); const [errorActive, setErrorActive] = useState< Record >({}); const [errorsForModal, setErrorsForModal] = useState>(null); const skip = useCallback(() => { setErrorsForModal(null); goNextPage(); }, [goNextPage]); const closeModal = useCallback(() => setErrorsForModal(null), []); const handleGoNext = useCallback(() => { const { currentErrors, isCritical } = compileControls(); setErrorActive((v) => ({ ...v, [pageTag]: currentErrors || {} })); if (currentErrors && Object.keys(currentErrors).length > 0) { setErrorsForModal({ currentErrors, isCritical }); } else { goNextPage(); } }, [compileControls, goNextPage, pageTag]); // Allow PageDown / PageUp shortcut to ease navigation useEffect(() => { const listener = (e: KeyboardEvent) => { let stopPropagation = false; if (e.key === 'PageDown') { handleGoNext(); stopPropagation = true; } if (e.key === 'PageUp') { goPreviousPage(); stopPropagation = true; } if (stopPropagation) { e.preventDefault(); e.stopPropagation(); } }; document.addEventListener('keydown', listener); return () => { document.removeEventListener('keydown', listener); }; }, [handleGoNext, goPreviousPage]); // Check that the source is valid against the schema const errors = useMemo(() => { const ajv = new Ajv({ removeAdditional: true, strict: false, allErrors: true, discriminator: true, }); const validator = ajv.compile(LunaticSchema); const isSourceValid = validator(structuredClone(source)); // ajv mutate the object, send a clone if (!isSourceValid) { return validator.errors; } return []; }, [source]); const [tab, setTab] = useState(0); const tabs = [ { label: 'Form', children: ( ({ errors: errorActive[pageTag], disabled: disabled, ...(readOnly ? { readOnly: true } : {}), })} /> ), }, { label: 'Data', children: , }, ...extraTabs, ] as TabEntry[]; if (errors && errors.length > 0) { tabs.push({ label: ( <> Errors{' '} {errors.length} ), children: , }); } return (
{tabs.map((t, k) => ( ))}
{tabs[tab].children}
console.log('Data', getData(true))} onLogComponents={() => console.log('Components', components)} > {showOverview && ( )} true} /> {errorsForModal?.currentErrors && ( )}
); } function onLogChange(args: unknown) { Logger.log('onChange', args); } function logMissingStrategy() { Logger.log('no missing strategy'); } export const Orchestrator = memo(OrchestratorForStories); export const OrchestratorMeta: Meta = { component: Orchestrator, argTypes: { // Do not display controls for these args source: { table: { disable: true } }, data: { table: { disable: true } }, getReferentiel: { table: { disable: true } }, initialPage: { table: { disable: true } }, slots: { table: { disable: true } }, lastReachedPage: { table: { disable: true } }, autoSuggesterLoading: { table: { disable: true } }, missingStrategy: { table: { disable: true } }, extraTabs: { table: { disable: true } }, activeFilters: { control: 'select', options: ['ALL', 'EXTERNAL', 'NONE', 'DISABLED'] }, }, args: { disableFiltersDescription: false, activeFilters: 'ALL', management: false, shortcut: false, activeControls: true, initialPage: '1', missing: false, missingStrategy: logMissingStrategy, showOverview: false, readOnly: false, disabled: false, detailAlwaysDisplayed: false, disableRosterForLoopDeleteRowButton: false, autoSuggesterLoading: false, }, }; export type OrchestratorStory = StoryObj;