Skip to content

Commit

Permalink
add a mechanism to reload data
Browse files Browse the repository at this point in the history
  • Loading branch information
renaud23 committed Dec 21, 2023
1 parent cfd134b commit cf0f016
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/orchestrator/UseLunatic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { useQuestionnaireTitle } from './useQuestionnaireTitle';
import { useRedirectIfAlreadyValidated } from './useRedirectIfAlreadyValidated';
import { useSaving } from './useSaving';
import { usePrevious } from '../../lib/commons/usePrevious';
import { useRefreshData } from './useRefreshData';
import { LunaticData } from '../../typeLunatic/type';

export function createPersonalizationMap(
personalization: Array<PersonalizationElement>
Expand Down Expand Up @@ -50,6 +52,7 @@ export function UseLunatic(props: PropsWithChildren<OrchestratorProps>) {
Record<string, string | number | boolean | Array<string>>
>({});
const { data, stateData, personalization = [] } = surveyUnitData ?? {};
const [currentData, setCurrentData] = useState<LunaticData | undefined>(data);
const { currentPage: pageFromAPI, state } = stateData ?? {};
const [refreshControls, setRefreshControls] = useState(false);
const shouldSync = useRef(false);
Expand Down Expand Up @@ -109,7 +112,9 @@ export function UseLunatic(props: PropsWithChildren<OrchestratorProps>) {
compileControls,
pageTag,
pager,
} = useLunatic(source, data, args);
} = useLunatic(source, currentData, args);

useRefreshData({ pageTag, refreshData: setCurrentData });

useEffect(() => {
(
Expand Down
27 changes: 27 additions & 0 deletions src/components/orchestrator/useRefreshData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useContext, useEffect } from 'react';
import { useMetadata } from '../../hooks/useMetadata';
import { loadSourceDataContext } from '../loadSourceData/LoadSourceDataContext';
import { LunaticData } from '../../typeLunatic/type';

type Args = {
pageTag?: string;
refreshData: (data: LunaticData) => void;
};

export function useRefreshData({ pageTag, refreshData }: Args) {
const { getSurveyUnitData } = useContext(loadSourceDataContext);
const metadata = useMetadata();
const pages = metadata?.refreshDataOnPages;

useEffect(() => {
if (pageTag && pages) {
if (pages.indexOf(pageTag) !== -1) {
getSurveyUnitData?.().then((su) => {
if (su) {
refreshData(su.data);
}
});
}
}
}, [pageTag, pages, refreshData, getSurveyUnitData]);
}
1 change: 1 addition & 0 deletions src/typeStromae/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@ export type MetadataSurvey = {
Submit: SubmitType;
redirections: Record<string, string>;
errorPage: Record<string, string>;
refreshDataOnPages?: Array<string>;
} & { optionalPages: Record<string, OptionalPage> };

0 comments on commit cf0f016

Please sign in to comment.