Skip to content

Commit

Permalink
Merge pull request #48 from InseeFrLab/feat/sprint21
Browse files Browse the repository at this point in the history
Feat/sprint21
  • Loading branch information
MichaelC67 authored Apr 17, 2024
2 parents 7ee9ad6 + 34fa8ee commit fbd77b3
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 12 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"name": "edt",
"version": "3.14.12",
"dateVersion": "11/04/2024",
"version": "3.14.13",
"dateVersion": "17/04/2024",
"licence": "MIT",
"dependencies": {
"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
"@inseefr/lunatic": "2.0.7-v2",
"@inseefrlab/lunatic-edt": "0.12.4",
"@inseefrlab/lunatic-edt": "0.12.5",
"@mui/material": "^5.10.7",
"@mui/styles": "^5.12.0",
"@react-pdf/font": "2.2.0",
"@react-pdf/renderer": "2.1.2",
"@types/elasticlunrjs": "^1.0.0",
"axios": "^1.2.2",
"chrome-launcher": "^0.15.2",
"chromium": "^3.0.3",
"dexie": "^3.2.2",
"elasticlunrjs": "^1.0.1",
"env-cmd": "^10.1.0",
"i18next": "^21.9.2",
"oidc-react": "^2.1.0",
Expand Down
9 changes: 9 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AutoCompleteActiviteOption } from "@inseefrlab/lunatic-edt";
import "App.scss";
import LoadingFull from "components/commons/LoadingFull/LoadingFull";
import { Index } from "elasticlunrjs";
import { EdtUserRightsEnum } from "enumerations/EdtUserRightsEnum";
import { ErrorCodeEnum } from "enumerations/ErrorCodeEnum";
import "i18n/i18n";
Expand All @@ -8,6 +10,8 @@ import ErrorPage from "pages/error/Error";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { EdtRoutes } from "routes/EdtRoutes";
import { getAutoCompleteRef } from "service/referentiel-service";
import { CreateIndex, optionsFiltered, setIndexSuggester } from "service/suggester-service";
import {
getAuthCache,
getDatas,
Expand All @@ -24,6 +28,8 @@ const App = () => {
const [error, setError] = useState<ErrorCodeEnum | undefined>(undefined);
const auth = useAuth();

const [index, setIndex] = useState<Index<AutoCompleteActiviteOption>>();

const getTokenHint = () => {
return localStorage.getItem("id_token") ?? undefined;
};
Expand Down Expand Up @@ -82,6 +88,9 @@ const App = () => {
initializeDatas(setError).then(() => {
setInitialized(true);
return initPropsAuth(auth).then(() => {
const options = optionsFiltered(getAutoCompleteRef());
const indexSuggester = CreateIndex(options, index, setIndex);
setIndexSuggester(indexSuggester);
setInitialized(true);
});
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import React, { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { getLabelsWhenQuit } from "service/alert-service";
import { getAutoCompleteRef, getNomenclatureRef } from "service/referentiel-service";
import { CreateIndexation, getIndexSuggester } from "service/suggester-service";
import { surveyReadOnly } from "service/survey-activity-service";
import { createNewActivityInCategory, getData } from "service/survey-service";
import { getSurveyIdFromUrl } from "utils/utils";
Expand Down Expand Up @@ -196,6 +197,8 @@ const MainActivityPage = () => {
addLightBlueIcon: <AddLightBlueIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
addWhiteIcon: <AddWhiteIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
modifiable: modifiable,
indexSuggester: getIndexSuggester(),
CreateIndex: CreateIndexation,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getAutoCompleteRef,
getRouteSecondaryActivityRef,
} from "service/referentiel-service";
import { CreateIndexation, getIndexSuggester } from "service/suggester-service";
import { surveyReadOnly } from "service/survey-activity-service";
import {
addToSecondaryActivityReferentiel,
Expand Down Expand Up @@ -99,6 +100,8 @@ const SecondaryActivitySelectionPage = () => {
),
iconSearch: <SearchIcon aria-label={t("accessibility.asset.mui-icon.search")} />,
},
indexSuggester: getIndexSuggester(),
CreateIndex: CreateIndexation,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ReactComponent as parentsIcon } from "assets/illustration/with-someone-
import LoopSurveyPageStep from "components/commons/LoopSurveyPage/LoopSurveyPageStep/LoopSurveyPageStep";
import { EdtRoutesNameEnum } from "enumerations/EdtRoutesNameEnum";
import { useTranslation } from "react-i18next";
import { CreateIndexation, getIndexSuggester } from "service/suggester-service";

const WithSomeoneSelectionPage = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -35,6 +36,8 @@ const WithSomeoneSelectionPage = () => {
},
},
displayStepper: false,
indexSuggester: getIndexSuggester(),
CreateIndex: CreateIndexation,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
onPrevious,
} from "service/navigation-service";
import { getAutoCompleteRef, getNomenclatureRef } from "service/referentiel-service";
import { CreateIndexation, getIndexSuggester } from "service/suggester-service";
import { mockData } from "service/survey-activity-service";
import { addToAutocompleteActivityReferentiel, getSource } from "service/survey-service";

Expand Down Expand Up @@ -154,6 +155,8 @@ const HelpCategoryActivity = () => {
extensionIcon: <ExtensionIcon aria-label={t("accessibility.asset.mui-icon.extension")} />,
addLightBlueIcon: <AddLightBlueIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
addWhiteIcon: <AddWhiteIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
indexSuggester: getIndexSuggester(),
CreateIndex: CreateIndexation,
};

const navToActivityRouteHome = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { getLoopInitialPage } from "service/loop-service";
import { getLoopPageSubpage, getStepData } from "service/loop-stepper-service";
import { getNavigatePath, navToHome, onClose, onNext, onPrevious } from "service/navigation-service";
import { getAutoCompleteRef, getNomenclatureRef } from "service/referentiel-service";
import { CreateIndexation, getIndexSuggester } from "service/suggester-service";
import { mockData } from "service/survey-activity-service";
import { addToAutocompleteActivityReferentiel, getSource } from "service/survey-service";

Expand Down Expand Up @@ -152,6 +153,8 @@ const HelpSubCategoryActivity = () => {
extensionIcon: <ExtensionIcon aria-label={t("accessibility.asset.mui-icon.extension")} />,
addLightBlueIcon: <AddLightBlueIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
addWhiteIcon: <AddWhiteIcon aria-label={t("accessibility.asset.mui-icon.add")} />,
indexSuggester: getIndexSuggester(),
CreateIndex: CreateIndexation,
};

const navToActivityRouteHome = useCallback(() => {
Expand Down
128 changes: 128 additions & 0 deletions src/service/suggester-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { AutoCompleteActiviteOption } from "@inseefrlab/lunatic-edt";
import elasticlunr, { Index } from "elasticlunrjs";
import { Dispatch, SetStateAction } from "react";
import stopWords from "../utils/stop_words_french.json";
import pairSynonymes from "../utils/synonymes-misspellings.json";

/**
* Remove accents
* @param value
* @returns
*/
export const removeAccents = (value: string) => {
return value
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "")
.replace(/'/g, " ");
};

const pronounAbbreviations = ["l", "d", "m", "s", "t"];

/**
* Activities with abbreviated pronouns (ex: de -> d')
* are not searched because pronouns are not skipped
* @param labelWithApostrophe
* @returns activity label with pronoun + apostroph replace
* with pronoun without abbreviation
*/
export const skipApostrophes = (labelWithApostrophe: string) => {
let label = labelWithApostrophe.toLowerCase();
pronounAbbreviations.forEach(abbrev => {
if (label?.includes(abbrev + "’")) {
label = label.replace(abbrev + "’", abbrev + "e ");
}
});
return label;
};

/**
* Add synonymes of misspellings
* @param option
* @returns
*/
export const addMisspellings = (option: AutoCompleteActiviteOption) => {
let labelWithMisspelling = "";

pairSynonymes.forEach(pairSynonyme => {
const term = pairSynonyme.termination[0];
pairSynonyme.misspelling.forEach(misspelling => {
if (option.label.includes(term)) {
const labelToReplace = option.label.replaceAll(term, misspelling) + "; ";
labelWithMisspelling =
labelWithMisspelling +
(labelWithMisspelling.includes(labelToReplace) ? "" : labelToReplace);
}
if (option.label.includes(misspelling)) {
const labelToReplace = option.label.replaceAll(misspelling, term) + "; ";
labelWithMisspelling =
labelWithMisspelling +
(labelWithMisspelling.includes(labelToReplace) ? "" : labelToReplace);
}
});
});
option.synonymes = option.synonymes + "; " + labelWithMisspelling;

return option;
};

export const activitesFiltredMap = (optionsFiltered: AutoCompleteActiviteOption[]) => {
const optionsFilteredMap = optionsFiltered.map(opt => {
const newOption: AutoCompleteActiviteOption = {
id: opt.id,
label: removeAccents(skipApostrophes(addMisspellings(opt).label)).replaceAll("’", "'"),
synonymes: opt.synonymes.replaceAll(";", "; "),
};
return newOption;
});
return optionsFilteredMap;
};

export const CreateIndexation = (optionsFiltered: AutoCompleteActiviteOption[]) => {
const optionsFilteredMap = activitesFiltredMap(optionsFiltered);

elasticlunr.clearStopWords();
elasticlunr.addStopWords(stopWords);

const temp: Index<AutoCompleteActiviteOption> = elasticlunr();
temp.addField("label");
temp.addField("synonymes");
temp.setRef("id");

for (const doc of optionsFilteredMap) {
temp.addDoc(doc);
}
return temp;
};

export function CreateIndex(
optionsFiltered: AutoCompleteActiviteOption[],
index: elasticlunr.Index<AutoCompleteActiviteOption> | undefined,
setIndex: Dispatch<SetStateAction<elasticlunr.Index<AutoCompleteActiviteOption> | undefined>>,
) {
const optionsFilteredMap = activitesFiltredMap(optionsFiltered);
index = CreateIndexation(optionsFilteredMap);
setIndex(index);

return index;
}

export const activitesFiltredUnique = (activitesAutoCompleteRef: AutoCompleteActiviteOption[]) => {
const optionsFiltered: AutoCompleteActiviteOption[] = activitesAutoCompleteRef.filter(
(option, i, arr) => arr.findIndex(opt => opt.label === option.label) === i,
);
return optionsFiltered;
};

export const optionsFiltered = (activitesAutoCompleteRef: AutoCompleteActiviteOption[]) => {
return activitesFiltredUnique(activitesAutoCompleteRef);
};

let indexSuggester: elasticlunr.Index<AutoCompleteActiviteOption>;

export const getIndexSuggester = () => {
return indexSuggester;
};

export const setIndexSuggester = (index: Index<AutoCompleteActiviteOption>) => {
indexSuggester = index;
};
12 changes: 7 additions & 5 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,13 @@ const initializeListSurveys = (setError: (error: ErrorCodeEnum) => void) => {
surveysData = data;
addArrayToSession("surveysData", surveysData);
data.forEach((surveyData: UserSurveys) => {
if (surveyData.questionnaireModelId === SourcesEnum.ACTIVITY_SURVEY) {
userDatas.push(surveyData);
}
if (surveyData.questionnaireModelId === SourcesEnum.WORK_TIME_SURVEY) {
userDatas.push(surveyData);
if (!userDatas?.find(user => user.surveyUnitId == surveyData.surveyUnitId)) {
if (surveyData.questionnaireModelId === SourcesEnum.ACTIVITY_SURVEY) {
userDatas.push(surveyData);
}
if (surveyData.questionnaireModelId === SourcesEnum.WORK_TIME_SURVEY) {
userDatas.push(surveyData);
}
}
});
return saveUserSurveysData({ data: userDatas }).then(saved => console.log(saved, data));
Expand Down
Loading

0 comments on commit fbd77b3

Please sign in to comment.