Skip to content

Commit

Permalink
Feat/global runtime variables (#68)
Browse files Browse the repository at this point in the history
* feat: create utils function to add external variables

* feat: add constants

* feat: global variables

- `GLOBAL_SURVEY_UNIT_ID`
- `GLOBAL_QUESTIONNAIRE_ID`
  • Loading branch information
laurentC35 authored Nov 8, 2023
1 parent f97a46a commit b0e49cd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const dontKnowButton = D.doesntKnowButton;
const refusedButton = D.refusalButton;

function LightOrchestrator({
surveyUnit,
initialData,
lastReachedPage,
standalone,
readonly,
pagination,
Expand Down Expand Up @@ -68,8 +69,8 @@ function LightOrchestrator({
goNextPage();
});

lunaticStateRef.current = useLunatic(source, surveyUnit?.data, {
lastReachedPage: surveyUnit?.stateData?.currentPage ?? '1',
lunaticStateRef.current = useLunatic(source, initialData, {
lastReachedPage: lastReachedPage ?? '1',
features,
pagination,
onChange: lightCustomHandleChange,
Expand Down
37 changes: 31 additions & 6 deletions queen-v2/src/components/orchestratorManager/orchestratorManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { useContext, useEffect, useMemo, useState } from 'react';
import { useHistory, useParams } from 'react-router-dom';
import { ORCHESTRATOR_COLLECT, ORCHESTRATOR_READONLY, READ_ONLY } from 'utils/constants';
import {
GLOBAL_QUEEN_VARIABLES,
ORCHESTRATOR_COLLECT,
ORCHESTRATOR_READONLY,
READ_ONLY,
} from 'utils/constants';
import { EventsManager, INIT_ORCHESTRATOR_EVENT, INIT_SESSION_EVENT } from 'utils/events';
import { useAPI, useAPIRemoteData, useAuth, useGetReferentiel } from 'utils/hook';
import { COMPLETED, VALIDATED, useQuestionnaireState } from 'utils/hook/questionnaire';
Expand All @@ -13,12 +18,19 @@ import Preloader from 'components/shared/preloader';
import { sendCloseEvent } from 'utils/communication';
import { useConstCallback } from 'utils/hook/useConstCallback';
import surveyUnitIdbService from 'utils/indexedbb/services/surveyUnit-idb-service';
import { checkQuestionnaire, getFullData, removeNullCollectedData } from 'utils/questionnaire';
import {
addGlobalVariablesToData,
addGlobalVariablesToQuestionnaire,
checkQuestionnaire,
getFullData,
removeNullCollectedData,
} from 'utils/questionnaire';

export const OrchestratorManager = () => {
const { standalone, apiUrl } = useContext(AppContext);
const { readonly: readonlyParam, idQ, idSU } = useParams();
const [surveyUnitData, setSurveyUnitData] = useState(null);
const [initalStateForLunatic, setInitalStateForLunatic] = useState(null);
const history = useHistory();

const readonly = readonlyParam === READ_ONLY;
Expand Down Expand Up @@ -65,8 +77,20 @@ export const OrchestratorManager = () => {
if (!init && questionnaire && surveyUnit) {
const { valid, error: questionnaireError } = checkQuestionnaire(questionnaire);
if (valid) {
setSource(questionnaire);
setSurveyUnitData(surveyUnit.data);
const globalQueenData = {
[GLOBAL_QUEEN_VARIABLES.GLOBAL_SURVEY_UNIT_ID]: surveyUnit.id,
[GLOBAL_QUEEN_VARIABLES.GLOBAL_QUESTIONNAIRE_ID]:
surveyUnit.questionnaireId ?? questionnaire.id,
};
const newQuestionnaire = addGlobalVariablesToQuestionnaire(questionnaire, globalQueenData);
setSource(newQuestionnaire);

const newData = addGlobalVariablesToData(surveyUnit?.data || {}, globalQueenData);
setSurveyUnitData(newData);
setInitalStateForLunatic({
initialData: newData,
lastReachedPage: surveyUnit?.stateData?.currentPage,
});
setInit(true);
LOGGER.log(INIT_ORCHESTRATOR_EVENT);
} else {
Expand Down Expand Up @@ -157,9 +181,10 @@ export const OrchestratorManager = () => {
{![READ_ONLY, undefined].includes(readonlyParam) && <NotFound />}
{loadingMessage && <Preloader message={loadingMessage} />}
{error && <Error message={error} />}
{!loadingMessage && !error && source && surveyUnit && (
{!loadingMessage && !error && source && initalStateForLunatic && (
<LightOrchestrator
surveyUnit={surveyUnit}
initialData={initalStateForLunatic.initialData}
lastReachedPage={initalStateForLunatic.lastReachedPage}
source={source}
getReferentiel={getReferentiel}
allData={surveyUnitData}
Expand Down
55 changes: 32 additions & 23 deletions queen-v2/src/components/visualizer/visualizer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useContext, useEffect, useState } from 'react';
import { useGetReferentiel, useRemoteData, useVisuQuery } from 'utils/hook';
import {
addGlobalVariablesToData,
addGlobalVariablesToQuestionnaire,
checkQuestionnaire,
downloadDataAsJson,
getFullData,
Expand All @@ -12,6 +14,7 @@ import LightOrchestrator from 'components/lightOrchestrator';
import Error from 'components/shared/Error';
import Preloader from 'components/shared/preloader';
import { useHistory } from 'react-router';
import { GLOBAL_QUEEN_VARIABLES } from 'utils/constants';
import { useQuestionnaireState } from 'utils/hook/questionnaire';
import { useConstCallback } from 'utils/hook/useConstCallback';
import surveyUnitIdbService from 'utils/indexedbb/services/surveyUnit-idb-service';
Expand All @@ -21,52 +24,57 @@ const Visualizer = () => {
const { apiUrl, standalone } = useContext(AppContext);
const [surveyUnitData, setSurveyUnitData] = useState(null);

const [surveyUnit, setSurveyUnit] = useState(undefined);
const [initalStateForLunatic, setInitalStateForLunatic] = useState(null);
const [error, setError] = useState(null);
const [source, setSource] = useState(null);

const { questionnaireUrl, dataUrl, nomenclatures, readonly } = useVisuQuery();
const {
surveyUnit: suData,
questionnaire,
loadingMessage,
errorMessage,
} = useRemoteData(questionnaireUrl, dataUrl);
const { surveyUnit, questionnaire, loadingMessage, errorMessage } = useRemoteData(
questionnaireUrl,
dataUrl
);

const { getReferentielForVizu } = useGetReferentiel(nomenclatures);

const [getState, , onDataChange] = useQuestionnaireState(
surveyUnit?.id,
suData?.stateData?.state
surveyUnit?.stateData?.state
);

const history = useHistory();

useEffect(() => {
if (suData === null) return;
const unit = {
...suData,
id: '1234',
};
if (surveyUnit === null) return;
const insertSuInIndexedDB = async su => {
console.log('Initiating sudata in IDB', su);
console.log('Initiating fake surveyUnit in IDB', su);
await surveyUnitIdbService.addOrUpdateSU(su);
};
insertSuInIndexedDB(unit);
setSurveyUnit(unit);
}, [suData]);
insertSuInIndexedDB(surveyUnit);
}, [surveyUnit]);

useEffect(() => {
if (questionnaireUrl && questionnaire && suData) {
if (questionnaireUrl && questionnaire && surveyUnit) {
const { valid, error: questionnaireError } = checkQuestionnaire(questionnaire);
if (valid) {
setSource(questionnaire);
setSurveyUnitData(suData?.data || {});
const globalQueenData = {
[GLOBAL_QUEEN_VARIABLES.GLOBAL_SURVEY_UNIT_ID]: surveyUnit.id,
[GLOBAL_QUEEN_VARIABLES.GLOBAL_QUESTIONNAIRE_ID]:
surveyUnit.questionnaireId ?? questionnaire.id,
};
const newQuestionnaire = addGlobalVariablesToQuestionnaire(questionnaire, globalQueenData);
setSource(newQuestionnaire);

const newData = addGlobalVariablesToData(surveyUnit?.data || {}, globalQueenData);
setSurveyUnitData(newData);
setInitalStateForLunatic({
initialData: newData,
lastReachedPage: surveyUnit?.stateData?.currentPage,
});
} else {
setError(questionnaireError);
}
}
}, [questionnaireUrl, questionnaire, suData, apiUrl]);
}, [questionnaireUrl, questionnaire, surveyUnit, apiUrl]);

useEffect(() => {
if (errorMessage) setError(errorMessage);
Expand Down Expand Up @@ -110,9 +118,10 @@ const Visualizer = () => {
<>
{loadingMessage && <Preloader message={loadingMessage} />}
{error && <Error message={error} />}
{questionnaireUrl && source && surveyUnit && (
{questionnaireUrl && source && initalStateForLunatic && (
<LightOrchestrator
surveyUnit={surveyUnit}
initialData={initalStateForLunatic.initialData}
lastReachedPage={initalStateForLunatic.lastReachedPage}
source={source}
autoSuggesterLoading={true}
getReferentiel={getReferentielForVizu}
Expand Down
5 changes: 5 additions & 0 deletions queen-v2/src/utils/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ export const SHORTCUT_NEXT = 'alt+enter';
export const SHORTCUT_PREVIOUS = 'alt+backspace';
export const SHORTCUT_FAST_FORWARD = 'alt+end';

export const GLOBAL_QUEEN_VARIABLES = {
GLOBAL_SURVEY_UNIT_ID: 'GLOBAL_SURVEY_UNIT_ID',
GLOBAL_QUESTIONNAIRE_ID: 'GLOBAL_QUESTIONNAIRE_ID',
};

export * from './paradata';
2 changes: 1 addition & 1 deletion queen-v2/src/utils/hook/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export const useRemoteData = (questionnaireUrl, dataUrl) => {
setLoadingMessage(Dictionary.waintingData);
const dR = await API.getRequest(dataUrl || DEFAULT_DATA_URL)(fakeToken);
if (!dR.error) {
setSurveyUnit(dR.data);
setSurveyUnit({ ...(dR.data || {}), id: '1234' });
setLoadingMessage(null);
} else setErrorMessage(getErrorMessage(dR, 'd'));
setLoadingMessage(null);
Expand Down
24 changes: 23 additions & 1 deletion queen-v2/src/utils/questionnaire/questionnaire.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MIN_LUNATIC_MODEL_VERSION, MIN_ENO_CORE_VERSION } from 'utils/constants';
import { MIN_ENO_CORE_VERSION, MIN_LUNATIC_MODEL_VERSION } from 'utils/constants';

const checkVersion = (actualVersion, expectedVersion) => {
try {
Expand Down Expand Up @@ -54,3 +54,25 @@ export const checkQuestionnaire = ({
}
return { valid: true };
};

/**
*
* @param {*} source questionnaire source
* @param {*} variables object of key, value
*/
export const addGlobalVariablesToQuestionnaire = (source = {}, globalVariables = {}) => {
const { variables } = source;
const newVariables = Object.entries(globalVariables).reduce((result, [name, value]) => {
return [...result, { variableType: 'EXTERNAL', name: name, value: null }];
}, variables);
return { ...source, variables: newVariables };
};

export const addGlobalVariablesToData = (lunaticData = {}, globalVariables = {}) => {
const { EXTERNAL } = lunaticData;
const newEXTERNAL = Object.entries(globalVariables).reduce((result, [name, value]) => {
return { ...result, [name]: value };
}, EXTERNAL || {});

return { ...lunaticData, EXTERNAL: newEXTERNAL };
};

0 comments on commit b0e49cd

Please sign in to comment.