Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sprint22 #54

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/i18n/locales/fr/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
"validate-all-empties-surveys": "Valider les carnets vides",
"validate-survey": "Valider le carnet",
"lock-survey": "Verrouiller le carnet",
"lock-all-surveys": "Verrouiller tous les carnets",
"not-lock-survey": "Devérrouiller le carnet",
"status-survey": {
"locked": "Verrouillé",
Expand All @@ -242,9 +243,11 @@
},
"lock-popup": {
"boldContent": "Êtes-vous sûr de vouloir verrouiller le carnet ?",
"boldContent-all-surveys": "Êtes-vous sûr de vouloir verrouiller tous les carnets ?",
"boldContent-not-locked": "Êtes-vous sûr de vouloir déverrouiller le carnet ?",
"boldContent-not-unlocked": "Vous ne pouvez pas déverrouiller le carnet !",
"content": "En cochant cette option, l'enquêté ne pourra pas modifier le carnet.",
"content-all-surveys": "En cochant cette option, les enquêtés ne pourront pas modifier les carnets.",
"content-not-locked": "En cochant cette option, l'enquêté pourra modifier le carnet.",
"content-not-unlocked": "Une fois que vous avez commencé à remplir le carnet, vous ne pouvez pas le déverrouiller.",
"confirm-button": "Compris"
Expand Down
55 changes: 44 additions & 11 deletions src/pages/home-surveyed/HomeSurveyed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ReactComponent as LockIcon } from "assets/illustration/mui-icon/lock.sv
import { ReactComponent as PowerSettingsIcon } from "assets/illustration/mui-icon/power-settings.svg";
import { ReactComponent as RemoveCircleIcon } from "assets/illustration/mui-icon/remove-circle.svg";
import { ReactComponent as ReminderNoteImg } from "assets/illustration/reminder-note.svg";
import { ReactComponent as ErrorIcon } from "assets/illustration/error/activity.svg";
import BreadcrumbsReviewer from "components/commons/BreadcrumbsReviewer/BreadcrumbsReviewer";
import FlexCenter from "components/commons/FlexCenter/FlexCenter";
import LoadingFull from "components/commons/LoadingFull/LoadingFull";
Expand Down Expand Up @@ -62,6 +63,7 @@ const HomeSurveyedPage = () => {
const [initialized, setInitialized] = React.useState<boolean>(false);
const [state, setState] = React.useState<LunaticData | undefined>(undefined);
const [isAddActivityOrRouteOpen, setIsAddActivityOrRouteOpen] = React.useState(false);
const [isAlertLockDisplayed, setIsAlertLockDisplayed] = React.useState<boolean>(false);
const [datas, setDatas] = React.useState<Person[]>([]);

const source = getSource(SourcesEnum.WORK_TIME_SURVEY);
Expand All @@ -70,16 +72,26 @@ const HomeSurveyedPage = () => {
const idHousehold = localStorage.getItem(LocalStorageVariableEnum.ID_HOUSEHOLD);
let userDatas: Person[];

//TODO: Set Alert to separate component as it is the same as in activity page minus the unlock labels
const alertLockLabels = {
boldContent: t("page.reviewer-home.lock-popup.boldContent-all-surveys"),
content: t("page.reviewer-home.lock-popup.content-all-surveys"),
cancel: t("page.alert-when-quit.alert-cancel"),
complete: t("page.reviewer-home.lock-survey"),
};

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);
});
};

Expand Down Expand Up @@ -198,6 +210,12 @@ const HomeSurveyedPage = () => {
navigate(getNavigatePath(EdtRoutesNameEnum.REVIEWER_HOME));
}, []);

const displayAlert = useCallback(
(setDisplayAlert: React.Dispatch<React.SetStateAction<boolean>>, display: boolean) => () =>
setDisplayAlert(display),
[],
);

const renderReminderNote = () => {
return (
<FlexCenter className={classes.spacing}>
Expand Down Expand Up @@ -236,7 +254,6 @@ const HomeSurveyedPage = () => {
setInitialized(true);
});
});

return (
<>
{renderReminderNote()}
Expand Down Expand Up @@ -292,6 +309,7 @@ const HomeSurveyedPage = () => {
!survey.startsWith("activitySurvey") && !survey.startsWith("workTimeSurvey"),
);
initHome(idsSurveysSelected);
setIsAlertLockDisplayed(false);
});
}, []);

Expand All @@ -313,6 +331,13 @@ const HomeSurveyedPage = () => {
let groups = Object.keys(userDatas);
return renderPageOrLoadingOrError(
<ErrorBoundary FallbackComponent={ErrorProvider}>
<Alert
isAlertDisplayed={isAlertLockDisplayed}
onCompleteCallBack={lockSurveys}
onCancelCallBack={displayAlert(setIsAlertLockDisplayed, false)}
labels={alertLockLabels}
icon={<ErrorIcon aria-label={t("page.alert-when-quit.alt-alert-icon")} />}
></Alert>
{renderReminderNote()}

<Box className={classes.groupCardBox}>
Expand Down Expand Up @@ -346,11 +371,15 @@ const HomeSurveyedPage = () => {
</Button>
<Button
variant="contained"
onClick={lockSurveys}
onClick={displayAlert(setIsAlertLockDisplayed, true)}
className={cx(classes.navButton)}
disabled={!navigator.onLine}
>
<LockIcon aria-label={t("accessibility.asset.mui-icon.padlock")} />
<LockIcon
className={cx(classes.lockIcon)}
aria-label={t("accessibility.asset.mui-icon.padlock")}
/>
{t("page.reviewer-home.lock-all-surveys")}
</Button>
</FlexCenter>
</Box>
Expand Down Expand Up @@ -514,6 +543,10 @@ const useStyles = makeStylesEdt({ "name": { NavButton: HomeSurveyedPage } })(the
emptyHeader: {
visibility: "hidden",
},
lockIcon: {
width: "1.5rem",
marginRight: "0.2rem",
},
}));

export default HomeSurveyedPage;
45 changes: 19 additions & 26 deletions src/service/api-service/getRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ const remoteGetSurveyStateData = (
getHeader(stromaeBackOfficeApiBaseUrl),
)
.then(response => {
console.log("Get stateData", response.data);
const stateData: StateData = response.data;
resolve(stateData);
})
Expand All @@ -216,6 +215,22 @@ const remoteGetSurveyStateData = (
});
});
};
const remoteGetSurveyDataSurveyed = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<SurveyData> => {
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,
Expand Down Expand Up @@ -254,36 +269,12 @@ const requestGetDataReviewer = (
});
};

const requestGetStateReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<StateData> => {
return new Promise<StateData>(resolve => {
axios
.get(
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey + "/state-data",
getHeader(stromaeBackOfficeApiBaseUrl),
)
.then(response => {
resolve(response.data);
})
.catch(err => {
console.log(err);
if (err.response?.status === 403) {
setError(ErrorCodeEnum.NO_RIGHTS);
} else {
resolve(initStateData());
}
});
});
};

const requestGetSurveyDataReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<SurveyData> => {
return requestGetDataReviewer(idSurvey, setError).then(data => {
return requestGetStateReviewer(idSurvey, setError).then((stateData: StateData) => {
return remoteGetSurveyStateData(idSurvey, setError).then((stateData: StateData) => {
return new Promise(resolve => {
const surveyData: SurveyData = {
stateData: stateData,
Expand Down Expand Up @@ -325,6 +316,8 @@ export {
fetchSurveysSourcesByIds,
fetchUserSurveysInfo,
remoteGetSurveyData,
requestGetDataReviewer,
remoteGetSurveyStateData,
remoteGetSurveyDataReviewer,
remoteGetSurveyDataSurveyed,
};
21 changes: 11 additions & 10 deletions src/service/api-service/putRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ const requestPutSurveyData = (
token?: string,
): Promise<SurveyData> => {
const collectedData = transformCollectedArray(data?.data?.COLLECTED);
const stateData = data.stateData;
if (data.data) {
data.data.COLLECTED = collectedData;
delete data.data.COLLECTED?.WEEKLYPLANNER;
delete data.data.stateData;
}
console.log("requestPutSurveyData", data);
const stateData = data.stateData;
const tempData = { ...data };
delete tempData.data.COLLECTED?.WEEKLYPLANNER;
delete tempData.data.stateData;
console.log("requestPutSurveyData", tempData);
const putLunaticData = axios.put(
`${stromaeBackOfficeApiBaseUrl}api/survey-unit/${idSurvey}/data`,
data.data,
tempData.data,
getHeader(stromaeBackOfficeApiBaseUrl, token),
);

Expand Down Expand Up @@ -68,8 +69,6 @@ const remotePutSurveyDataReviewer = (
stateData: StateData,
data: LunaticData,
): Promise<SurveyData> => {
//Temporar check on token validity to avoid 401 error, if not valid, reload page
//
const now = new Date();
const tokenExpiresAt = jwt<JwtPayload>(getUserToken() ?? "").exp;
// * 1000 because tokenExpiresAt is in seconds and now.getTime() in milliseconds
Expand All @@ -96,13 +95,15 @@ const requestPutDataReviewer = (
): Promise<LunaticData> => {
console.log("requestPutDataReviewer", data);
data.COLLECTED = transformCollectedArray(data?.COLLECTED);
//TODO: Find another solution to avoid ui problem
delete data.COLLECTED?.WEEKLYPLANNER;
const tempData = { ...data };
delete tempData.COLLECTED?.WEEKLYPLANNER;
delete tempData.stateData;
console.log("requestPutSurveyData", tempData);
return new Promise<LunaticData>(resolve => {
axios
.put(
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey + "/data",
data,
tempData,
getHeader(stromaeBackOfficeApiBaseUrl, token),
)
.then(() => {
Expand Down
35 changes: 18 additions & 17 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ 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,
remoteGetSurveyStateData,
requestGetDataReviewer,
} from "service/api-service/getRemoteData";
import { lunaticDatabase } from "service/lunatic-database";
import { LABEL_WORK_TIME_SURVEY, getCurrentPageSource } from "service/orchestrator-service";
import {
Expand All @@ -54,11 +58,7 @@ import {
} from "assets/surveyData";
import { EdtUserRightsEnum } from "./../enumerations/EdtUserRightsEnum";
import { LunaticData } from "./../interface/lunatic/Lunatic";
import {
fetchUserSurveysInfo,
remoteGetSurveyData,
remoteGetSurveyDataReviewer,
} from "./api-service/getRemoteData";
import { fetchUserSurveysInfo, remoteGetSurveyData } from "./api-service/getRemoteData";
import { getFlatLocalStorageValue } from "./local-storage-service";
import { navToPlanner, setAllNamesOfGroupAndNav } from "./navigation-service";
import { addToAutocompleteActivityReferentiel } from "./referentiel-service";
Expand All @@ -68,7 +68,7 @@ import { remotePutSurveyData, remotePutSurveyDataReviewer } from "./api-service/
import { fetchReferentiels } from "./api-service/getLocalSurveyData";
import { fetchRemoteReferentiels } from "service/api-service/getRemoteData";
import {
getSurveyStateData,
getLocalSurveyStateData,
initStateData,
isDemoMode,
isSurveyClosed,
Expand Down Expand Up @@ -533,15 +533,15 @@ const getRemoteSavedSurveyData = (
return Promise.reject(new Error("Offline"));
}

const urlRemote = isReviewer() ? remoteGetSurveyDataReviewer : remoteGetSurveyData;

return urlRemote(surveyId, setError)
const getSurveyDataFunction = isReviewer() ? requestGetDataReviewer : remoteGetSurveyData;
const getSurveyStateDataFunction = remoteGetSurveyStateData;
//TODO: Refactor dirty code
return getSurveyDataFunction(surveyId, setError)
.then((remoteSurveyData: any) => {
// TODO: remove any and improve ts types
const surveyData = initializeData(remoteSurveyData, surveyId);
return lunaticDatabase.get(surveyId).then(localSurveyData => {
if (shouldInitData(remoteSurveyData, localSurveyData)) {
const stateData = getSurveyStateData(surveyData);
const stateData = getLocalSurveyStateData(surveyData);
setLocalDatabase(stateData, surveyData, surveyId);
return lunaticDatabase.save(surveyId, surveyData);
} else {
Expand All @@ -552,8 +552,10 @@ const getRemoteSavedSurveyData = (
if (weeklyPlanner) {
remoteSurveyData.COLLECTED.WEEKLYPLANNER = weeklyPlanner;
}
const stateData = getSurveyStateData(surveyData);
setLocalDatabase(stateData, remoteSurveyData, surveyId);
getSurveyStateDataFunction(surveyId, setError).then(stateData => {
setLocalDatabase(stateData, remoteSurveyData, surveyId);
});

return lunaticDatabase.save(surveyId, surveyData);
}
}
Expand Down Expand Up @@ -892,7 +894,6 @@ const saveData = (
const dataIsChanged = dataIsChange(idSurvey, data, oldDataSurvey);
const isChange = forceUpdate || dataIsChanged;
datas.set(idSurvey, data);

data = updateLocked(idSurvey, data);
let stateData: StateData = data?.stateData ?? stateDataForced ?? initStateData(data);

Expand All @@ -915,7 +916,6 @@ const saveData = (
data: data,
};
data.lastRemoteSaveDate = stateData.date;
console.log("StateData to save", stateData);

if (isReviewerMode) {
return remotePutSurveyDataReviewer(idSurvey, stateData, data).then(() => {
Expand Down Expand Up @@ -1696,6 +1696,7 @@ export {
addToAutocompleteActivityReferentiel,
arrayOfSurveysPersonDemo,
createDataEmpty,
createNameSurveyMap,
existVariableEdited,
getAuthCache,
getComponentId,
Expand All @@ -1721,7 +1722,7 @@ export {
getSurveyDate,
getSurveysIdsForHousehold,
getSurveyRights,
getSurveyStateData,
getLocalSurveyStateData,
getTabsData,
getUserDatas,
getUserDatasActivity,
Expand Down
Loading
Loading