From 8bab5136e0810b5f3859034f9beea3b9a5e0cf58 Mon Sep 17 00:00:00 2001 From: Mailine Nguyen <64129348+MailineN@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:36:58 +0200 Subject: [PATCH] fix: double request that lead to init data in reviewer mode --- src/pages/home-surveyed/HomeSurveyed.tsx | 21 ++++++++++++--------- src/service/api-service/getRemoteData.ts | 18 ++++++++++++++++++ src/service/survey-service.ts | 8 ++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/pages/home-surveyed/HomeSurveyed.tsx b/src/pages/home-surveyed/HomeSurveyed.tsx index e712947a..f201b63e 100644 --- a/src/pages/home-surveyed/HomeSurveyed.tsx +++ b/src/pages/home-surveyed/HomeSurveyed.tsx @@ -47,6 +47,7 @@ import { surveysIds, userDatasMap, initializeDatas, + createNameSurveyMap, } from "service/survey-service"; import { isDemoMode, lockAllSurveys, validateAllEmptySurveys } from "service/survey-state-service"; import { getUserRights } from "service/user-service"; @@ -72,14 +73,16 @@ const HomeSurveyedPage = () => { const initHome = (idsSurveysSelected: string[]) => { initializeHomeSurveys(idHousehold ?? "").then(() => { - initializeSurveysDatasCache(idsSurveysSelected).finally(() => { - userDatas = userDatasMap(); - if (getData(idsSurveysSelected[0]) != undefined) { - setState(getData(idsSurveysSelected[0])); - setInitialized(true); - } - setDatas(userDatas); - }); + // initializeSurveysDatasCache(idsSurveysSelected).finally(() => { + + // }); + + userDatas = userDatasMap(); + if (getData(idsSurveysSelected[0]) != undefined) { + setState(getData(idsSurveysSelected[0])); + setInitialized(true); + } + setDatas(userDatas); }); }; @@ -258,7 +261,6 @@ const HomeSurveyedPage = () => { const renderHomeInterviewer = () => { let userDataGroupedInterv = nameSurveyGroupMap(); let groups = Object.keys(userDataGroupedInterv); - console.log("groups", groups); return ( <> {renderReminderNote()} @@ -310,6 +312,7 @@ const HomeSurveyedPage = () => { const renderHomeReviewer = () => { let userDatas = groupBy(userDatasMap(), nameSurveyData => nameSurveyData.num); + console.log("userDatas", userDatas); let groups = Object.keys(userDatas); return renderPageOrLoadingOrError( diff --git a/src/service/api-service/getRemoteData.ts b/src/service/api-service/getRemoteData.ts index d07b074b..4bb8ff6c 100644 --- a/src/service/api-service/getRemoteData.ts +++ b/src/service/api-service/getRemoteData.ts @@ -216,6 +216,22 @@ const remoteGetSurveyStateData = ( }); }); }; +const remoteGetSurveyDataSurveyed = ( + idSurvey: string, + setError: (error: ErrorCodeEnum) => void, +): Promise => { + return remoteGetSurveyData(idSurvey, setError).then(data => { + return remoteGetSurveyStateData(idSurvey, setError).then((stateData: StateData) => { + return new Promise(resolve => { + const surveyData: SurveyData = { + stateData: stateData, + data: data, + }; + resolve(surveyData); + }); + }); + }); +}; const requestGetDataReviewer = ( idSurvey: string, @@ -325,6 +341,8 @@ export { fetchSurveysSourcesByIds, fetchUserSurveysInfo, remoteGetSurveyData, + requestGetDataReviewer, remoteGetSurveyStateData, remoteGetSurveyDataReviewer, + remoteGetSurveyDataSurveyed, }; diff --git a/src/service/survey-service.ts b/src/service/survey-service.ts index 65a361e4..a3b48098 100644 --- a/src/service/survey-service.ts +++ b/src/service/survey-service.ts @@ -36,7 +36,10 @@ import { } from "interface/lunatic/Lunatic"; import { AuthContextProps } from "oidc-react"; import { NavigateFunction } from "react-router-dom"; -import { fetchReviewerSurveysAssignments } from "service/api-service/getRemoteData"; +import { + fetchReviewerSurveysAssignments, + requestGetDataReviewer, +} from "service/api-service/getRemoteData"; import { lunaticDatabase } from "service/lunatic-database"; import { LABEL_WORK_TIME_SURVEY, getCurrentPageSource } from "service/orchestrator-service"; import { @@ -533,7 +536,7 @@ const getRemoteSavedSurveyData = ( return Promise.reject(new Error("Offline")); } - const urlRemote = isReviewer() ? remoteGetSurveyDataReviewer : remoteGetSurveyData; + const urlRemote = isReviewer() ? requestGetDataReviewer : remoteGetSurveyData; return urlRemote(surveyId, setError) .then((remoteSurveyData: any) => { @@ -1696,6 +1699,7 @@ export { addToAutocompleteActivityReferentiel, arrayOfSurveysPersonDemo, createDataEmpty, + createNameSurveyMap, existVariableEdited, getAuthCache, getComponentId,