Skip to content

Commit

Permalink
fix(stateData): fix state of survey
Browse files Browse the repository at this point in the history
  • Loading branch information
lailabjil committed Jul 19, 2023
1 parent 5675aa4 commit 746ddf7
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/components/commons/LoopSurveyPage/LoopSurveyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LoopEnum } from "enumerations/LoopEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder } from "orchestrator/Orchestrator";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { getLoopLastCompletedStep } from "service/loop-service";
import { loopActivityStepperData } from "service/loop-stepper-service";
import { setEnviro } from "service/navigation-service";
Expand Down Expand Up @@ -53,7 +53,8 @@ const LoopSurveyPage = (props: LoopSurveyPageProps) => {
const context: OrchestratorContext = useOutletContext();
setEnviro(context, useNavigate(), callbackHolder);

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

const { classes, cx } = useStyles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { OrchestratorForStories, callbackHolder } from "orchestrator/Orchestrator";
import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { getLabels, getLabelsWhenQuit } from "service/alert-service";
import { getLoopInitialPage, skipBackPage, skipNextPage } from "service/loop-service";
import { getLoopPageSubpage, getStepData } from "service/loop-stepper-service";
Expand Down Expand Up @@ -46,7 +46,8 @@ const LoopSurveyPageStep = (props: LoopSurveyPageStepProps) => {
const { t } = useTranslation();
const context: OrchestratorContext = useOutletContext();
setEnviro(context, useNavigate(), callbackHolder);
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

const paramIteration = useParams().iteration;
const currentIteration = paramIteration ? +paramIteration : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { surveyReadOnly } from "service/survey-activity-service";
import { getPrintedFirstName, getPrintedSurveyDate } from "service/survey-service";
import { getSurveyIdFromUrl } from "utils/utils";
import SurveyPage from "../SurveyPage";
import { useLocation } from "react-router-dom";

export interface SurveyPageStepProps {
currentPage: EdtRoutesNameEnum;
Expand Down Expand Up @@ -54,7 +55,8 @@ const SurveyPageStep = (props: SurveyPageStepProps) => {

const { t } = useTranslation();
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

const navigate = useNavigate();

Expand Down
1 change: 1 addition & 0 deletions src/enumerations/StateDataStateEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export enum StateDataStateEnum {
VALIDATED = "VALIDATED",
TOEXTRACT = "TO-EXTRACT",
EXTRACTED = "EXTRACTED",
COMPLETED = "COMPLETED",
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { callbackHolder } from "orchestrator/Orchestrator";
import React, { useCallback, useEffect, useState } from "react";
import { isAndroid, isIOS, isMobile } from "react-device-detect";
import { useTranslation } from "react-i18next";
import { Outlet, useNavigate, useOutletContext } from "react-router-dom";
import { Outlet, useLocation, useNavigate, useOutletContext } from "react-router-dom";
import { getLoopSize, setLoopSize } from "service/loop-service";
import {
getCurrentNavigatePath,
Expand Down Expand Up @@ -72,7 +72,8 @@ const ActivityOrRoutePlannerPage = () => {
const source =
context?.source?.components != null ? context.source : getSource(SourcesEnum.ACTIVITY_SURVEY);

let idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
let idSurvey = getSurveyIdFromUrl(context, location);
const { t } = useTranslation();
const [isSubchildDisplayed, setIsSubChildDisplayed] = React.useState(false);
const [isAddActivityOrRouteOpen, setIsAddActivityOrRouteOpen] = React.useState(false);
Expand Down Expand Up @@ -159,7 +160,7 @@ const ActivityOrRoutePlannerPage = () => {
} else {
setSkip(false);
}
idSurvey = getSurveyIdFromUrl(context);
idSurvey = getSurveyIdFromUrl(context, location);
context.idSurvey = idSurvey;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder, OrchestratorForStories } from "orchestrator/Orchestrator";
import { Fragment, useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { getLabelsWhenQuit } from "service/alert-service";
import { getLoopInitialPage } from "service/loop-service";
import { getLoopPageSubpage, getNextLoopPage, getStepData } from "service/loop-stepper-service";
Expand All @@ -37,7 +37,8 @@ const ActivityDurationPage = () => {
const { classes } = useStyles();
setEnviro(context, useNavigate(), callbackHolder);

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const paramIteration = useParams().iteration;
const currentIteration = paramIteration ? +paramIteration : 0;
const isRoute = getValue(idSurvey, FieldNameEnum.ISROUTE, currentIteration) as boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import LoopSurveyPage from "components/commons/LoopSurveyPage/LoopSurveyPage";
import { EdtRoutesNameEnum } from "enumerations/EdtRoutesNameEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder, OrchestratorForStories } from "orchestrator/Orchestrator";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { filtrePage, getLoopInitialPage, getValueOfActivity, skipNextPage } from "service/loop-service";
import {
getLoopPageSubpage,
Expand Down Expand Up @@ -56,7 +56,8 @@ const MainActivityPage = () => {
const context: OrchestratorContext = useOutletContext();
setEnviro(context, useNavigate(), callbackHolder);

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const currentPage = EdtRoutesNameEnum.MAIN_ACTIVITY;
const stepData = getStepData(currentPage);
const paramIteration = useParams().iteration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FieldNameEnum } from "enumerations/FieldNameEnum";
import { ReferentielsEnum } from "enumerations/ReferentielsEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { useTranslation } from "react-i18next";
import { useOutletContext, useParams } from "react-router-dom";
import { useLocation, useOutletContext, useParams } from "react-router-dom";
import {
getActivitySecondaryActivityRef,
getRouteSecondaryActivityRef,
Expand All @@ -18,7 +18,8 @@ const SecondaryActivitySelectionPage = () => {
const context: OrchestratorContext = useOutletContext();
const { t } = useTranslation();

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const paramIteration = useParams().iteration;
const currentIteration = paramIteration ? +paramIteration : 0;
const isRoute = getValue(idSurvey, FieldNameEnum.ISROUTE, currentIteration) as boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { OrchestratorForStories, callbackHolder } from "orchestrator/Orchestrator";
import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { getLabels, getLabelsWhenQuit } from "service/alert-service";
import { getLoopInitialPage } from "service/loop-service";
import { getLoopPageSubpage, getPreviousLoopPage, getStepData } from "service/loop-stepper-service";
Expand All @@ -34,7 +34,8 @@ const WithScreenPage = () => {
const context: OrchestratorContext = useOutletContext();
setEnviro(context, useNavigate(), callbackHolder);

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const paramIteration = useParams().iteration;
const currentIteration = paramIteration ? +paramIteration : 0;
const currentPage = EdtRoutesNameEnum.WITH_SCREEN;
Expand Down
7 changes: 4 additions & 3 deletions src/pages/activity/activity-summary/ActivitySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { LunaticModel, OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder } from "orchestrator/Orchestrator";
import React, { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext } from "react-router-dom";
import { getFlatLocalStorageValue, getLocalStorageValue } from "service/local-storage-service";
import { getLoopSize, setLoopSize } from "service/loop-service";
import {
Expand Down Expand Up @@ -82,7 +82,8 @@ const ActivitySummaryPage = () => {

const source =
context?.source?.components != null ? context.source : getSource(SourcesEnum.ACTIVITY_SURVEY);
let idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
let idSurvey = getSurveyIdFromUrl(context, location);
const [score, setScore] = React.useState<number | undefined>(undefined);
const [isAddActivityOrRouteOpen, setIsAddActivityOrRouteOpen] = React.useState(false);
const localIsSummaryEdited = getLocalStorageValue(
Expand Down Expand Up @@ -130,7 +131,7 @@ const ActivitySummaryPage = () => {
}, []);

useEffect(() => {
idSurvey = getSurveyIdFromUrl(context);
idSurvey = getSurveyIdFromUrl(context, location);
context.idSurvey = idSurvey;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { LoopEnum } from "enumerations/LoopEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext, useParams } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";
import { loopActivityRouteStepperData, loopActivityStepperData } from "service/loop-stepper-service";
import { getLoopParameterizedNavigatePath, navFullPath } from "service/navigation-service";
import { surveyReadOnly } from "service/survey-activity-service";
Expand All @@ -19,7 +19,8 @@ const EditActivityInformationPage = () => {
const { t } = useTranslation();
const navigate = useNavigate();
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

const { classes } = useStyles();
const paramIteration = useParams().iteration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import greatestActivityDay from "assets/illustration/greatest-activity-day.svg";
import SurveyPageStep from "components/commons/SurveyPage/SurveyPageStep/SurveyPageStep";
import { EdtRoutesNameEnum } from "enumerations/EdtRoutesNameEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { useOutletContext } from "react-router-dom";
import { useLocation, useOutletContext } from "react-router-dom";
import { getActivitesSelectedLabel } from "service/survey-activity-service";
import { getSurveyIdFromUrl } from "utils/utils";

const GreatestActivityDayPage = () => {
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const uniqueActivities = getActivitesSelectedLabel(idSurvey).filter(
(value, index, self) =>
index ===
Expand Down
5 changes: 3 additions & 2 deletions src/pages/activity/phone-time/PhoneTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { OrchestratorForStories, callbackHolder } from "orchestrator/Orchestrator";
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext } from "react-router-dom";
import {
getNavigatePath,
getOrchestratorPage,
Expand All @@ -25,7 +25,8 @@ import { getSurveyIdFromUrl } from "utils/utils";
const PhoneTimePage = () => {
const currentPage = EdtRoutesNameEnum.PHONE_TIME;
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const { t } = useTranslation();
setEnviro(context, useNavigate(), callbackHolder);

Expand Down
5 changes: 3 additions & 2 deletions src/pages/activity/worst-activity-day/WorstActivityDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import worstActivityDay from "assets/illustration/worst-activity-day.svg";
import SurveyPageStep from "components/commons/SurveyPage/SurveyPageStep/SurveyPageStep";
import { EdtRoutesNameEnum } from "enumerations/EdtRoutesNameEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { useOutletContext } from "react-router-dom";
import { useLocation, useOutletContext } from "react-router-dom";
import { getActivitesSelectedLabel } from "service/survey-activity-service";
import { getSurveyIdFromUrl } from "utils/utils";

const WorstActivityDayPage = () => {
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const uniqueActivities = getActivitesSelectedLabel(idSurvey).filter(
(value, index, self) =>
index ===
Expand Down
5 changes: 3 additions & 2 deletions src/pages/day-of-survey/DayOfSurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import { FieldNameEnum } from "enumerations/FieldNameEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder } from "orchestrator/Orchestrator";
import React from "react";
import { useOutletContext } from "react-router-dom";
import { useLocation, useOutletContext } from "react-router-dom";
import { navToErrorPage } from "service/navigation-service";
import { surveyReadOnly } from "service/survey-activity-service";
import { getComponentId, setValue } from "service/survey-service";
import { getSurveyIdFromUrl } from "utils/utils";

const DayOfSurveyPage = () => {
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

let [disabledButton, setDisabledButton] = React.useState<boolean>(false);
const modifiable = !surveyReadOnly(context.rightsSurvey);
Expand Down
5 changes: 3 additions & 2 deletions src/pages/edit-global-information/EditGlobalInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { FieldNameEnum } from "enumerations/FieldNameEnum";
import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder } from "orchestrator/Orchestrator";
import React from "react";
import { useOutletContext } from "react-router-dom";
import { useLocation, useOutletContext } from "react-router-dom";
import { getComponentsOfVariable, setValue } from "service/survey-service";
import { getSurveyIdFromUrl } from "utils/utils";

const EditGlobalInformationPage = () => {
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
let [disabledButton, setDisabledButton] = React.useState<boolean>(false);

const keydownChange = () => {
Expand Down
7 changes: 4 additions & 3 deletions src/pages/end-survey/EndSurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { callbackHolder } from "orchestrator/Orchestrator";
import { SetStateAction, useCallback, useEffect, useState } from "react";
import { isMobile } from "react-device-detect";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext } from "react-router-dom";
import { remotePutSurveyData, remotePutSurveyDataReviewer } from "service/api-service";
import { getFlatLocalStorageValue } from "service/local-storage-service";
import {
Expand All @@ -37,7 +37,8 @@ const EndSurveyPage = () => {
const { classes, cx } = useStyles();
const { t } = useTranslation();
const context: OrchestratorContext = useOutletContext();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

const navigate = useNavigate();

Expand Down Expand Up @@ -86,7 +87,7 @@ const EndSurveyPage = () => {
const remoteSaveSurveyAndGoBackHome = useCallback(() => {
const dataWithIsEnvoyed = setValue(idSurvey, FieldNameEnum.ISENVOYED, true);
const stateData: StateData = {
state: StateDataStateEnum.VALIDATED,
state: StateDataStateEnum.COMPLETED,
date: Date.now(),
currentPage: getCurrentPage(callbackHolder.getData(), context.source),
};
Expand Down
6 changes: 3 additions & 3 deletions src/pages/work-time/kind-of-week/KindOfWeek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { OrchestratorForStories, callbackHolder } from "orchestrator/Orchestrator";
import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext } from "react-router-dom";
import {
getNavigatePath,
getOrchestratorPage,
Expand All @@ -29,8 +29,8 @@ const KindOfWeekPage = () => {
const context: OrchestratorContext = useOutletContext();
const { t } = useTranslation();
setEnviro(context, useNavigate(), callbackHolder);

const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);
const currentPage = EdtRoutesNameEnum.KIND_OF_WEEK;

const routeEnd =
Expand Down
5 changes: 3 additions & 2 deletions src/pages/work-time/weekly-planner/WeeklyPlanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OrchestratorContext } from "interface/lunatic/Lunatic";
import { callbackHolder, OrchestratorForStories } from "orchestrator/Orchestrator";
import React, { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useOutletContext } from "react-router-dom";
import { useLocation, useNavigate, useOutletContext } from "react-router-dom";
import {
closeFormularieAndNav,
getFullNavigatePath,
Expand All @@ -32,7 +32,8 @@ import { getSurveyIdFromUrl } from "utils/utils";
const WeeklyPlannerPage = () => {
const context: OrchestratorContext = useOutletContext();
const { t } = useTranslation();
const idSurvey = getSurveyIdFromUrl(context);
const location = useLocation();
const idSurvey = getSurveyIdFromUrl(context, location);

setEnviro(context, useNavigate(), callbackHolder);

Expand Down
17 changes: 15 additions & 2 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,23 @@ const setLocalDatabase = (stateData: StateData, data: LunaticData, idSurvey: str
});
};

const getSurveyStateData = (data: LunaticData, idSurvey: string): StateData => {
const getStateOfSurvey = (idSurvey: string): StateDataStateEnum => {
const isSent = getValue(idSurvey, FieldNameEnum.ISENVOYED) as boolean;
const isLocked = surveyLocked(idSurvey);

let state: StateDataStateEnum = StateDataStateEnum.INIT;

if (isSent) {
state = StateDataStateEnum.COMPLETED;
} else if (isLocked) {
state = StateDataStateEnum.VALIDATED;
}
return state;
};

const getSurveyStateData = (data: LunaticData, idSurvey: string): StateData => {
const stateData: StateData = {
state: isSent ? StateDataStateEnum.VALIDATED : StateDataStateEnum.INIT,
state: getStateOfSurvey(idSurvey),
date: Date.now(),
currentPage: getCurrentPage(data),
};
Expand Down
Loading

0 comments on commit 746ddf7

Please sign in to comment.