Skip to content

Commit

Permalink
fix(sonar): fix code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
lailabjil committed Jan 11, 2024
1 parent f73309f commit 049d4f3
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 94 deletions.
27 changes: 13 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,19 @@ const App = () => {
}
}
}, [auth]);
return (
<>
{initialized && !error ? (
<EdtRoutes />
) : !error ? (
<LoadingFull
message={t("page.home.loading.message")}
thanking={t("page.home.loading.thanking")}
/>
) : (
<ErrorPage errorCode={error} atInit={true} />
)}
</>
);

const renderErrorOrLoadingPage = () => {
!error ? (
<LoadingFull
message={t("page.home.loading.message")}
thanking={t("page.home.loading.thanking")}
/>
) : (
<ErrorPage errorCode={error} atInit={true} />
);
};

return <>{initialized && !error ? <EdtRoutes /> : renderErrorOrLoadingPage()}</>;
};

export default App;
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,19 @@ const SurveyPageStep = (props: SurveyPageStepProps) => {
modifiable: modifiable,
};

const nextRouteNav = useCallback(() => {
if (validateButton) {
return validateButton;
} else {
nextRoute
? saveAndNavFullPath(idSurvey, nextRoute)
: saveAndNextStep(idSurvey, context.source, context.surveyRootPage, currentPage);
}
}, []);

const surveyPageNotStepProps = {
idSurvey: idSurvey,
validate:
validateButton ??
useCallback(
() =>
nextRoute
? saveAndNavFullPath(idSurvey, nextRoute)
: saveAndNextStep(idSurvey, context.source, context.surveyRootPage, currentPage),
[],
),
validate: nextRouteNav,
srcIcon: errorIcon,
altIcon: errorAltIcon ? t(errorAltIcon) : undefined,
onNavigateBack: useCallback(() => saveAndNav(idSurvey), []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {
surveyLocked,
} from "service/survey-service";
import { isReviewer } from "service/user-service";
import { getSurveyIdFromUrl } from "utils/utils";
import { getClassCondition, getSurveyIdFromUrl } from "utils/utils";
import { v4 as uuidv4 } from "uuid";

const getSurveyDatePlanner = (idSurvey: string) => {
Expand Down Expand Up @@ -237,10 +237,6 @@ const isMobileApp = () => {
return !isPwa() && isMobile && (isIOS || isAndroid);
};

const getClassCondition = (classes: any, condition: boolean, classNameYes: any, classNameNo: any) => {
return condition ? classNameYes : classNameNo;
};

const isLockedLabels = (
isLocked: boolean,
variableEdited: boolean,
Expand Down Expand Up @@ -659,7 +655,7 @@ const ActivityOrRoutePlannerPage = () => {
<Box
className={cx(
classes.surveyPageBox,
getClassCondition(classes, isMobileApp(), classes.surveyPageBoxTablet, ""),
getClassCondition(isMobileApp(), classes.surveyPageBoxTablet, ""),
)}
>
{(isItDesktop || !isSubChildDisplayed) && (
Expand All @@ -683,15 +679,13 @@ const ActivityOrRoutePlannerPage = () => {
>
<Box
className={getClassCondition(
classes,
menuActivityPlannerDisplayed,
classes.outerContentBox,
heightClass(classes),
)}
>
<Box
className={getClassCondition(
classes,
menuActivityPlannerDisplayed,
classes.innerContentBox,
heightClass(classes),
Expand All @@ -709,7 +703,6 @@ const ActivityOrRoutePlannerPage = () => {
></Alert>
<Box
className={getClassCondition(
classes,
isReviewerMode() && activitiesRoutesOrGaps.length !== 0,
classes.infoReviewerBox,
classes.infoBox,
Expand Down Expand Up @@ -882,14 +875,8 @@ const ActivityOrRoutePlannerPage = () => {
)}
<Box
className={cx(
getClassCondition(isSubChildDisplayed && isItDesktop, classes.outletBoxDesktop, ""),
getClassCondition(
classes,
isSubChildDisplayed && isItDesktop,
classes.outletBoxDesktop,
"",
),
getClassCondition(
classes,
isSubChildDisplayed && !isItDesktop,
classes.outletBoxMobileTablet,
"",
Expand Down
7 changes: 1 addition & 6 deletions src/pages/activity/activity-summary/ActivitySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import {
} from "service/survey-service";
import { getUserRights } from "service/user-service";
import ActivitiesSummaryExportTemplate from "template/summary-export/ActivitiesSummaryExportTemplate";
import { getSurveyIdFromUrl } from "utils/utils";
import { getClassCondition, getSurveyIdFromUrl } from "utils/utils";
import { v4 as uuidv4 } from "uuid";

const getSurveyDatePlanner = (idSurvey: string) => {
Expand Down Expand Up @@ -502,7 +502,6 @@ const ActivitySummaryPage = () => {
<FlexCenter>
<Box
className={getClassCondition(
classes,
isReviewerMode && activitiesRoutesOrGaps.length !== 0,
classes.infoReviewerBox,
classes.infoBox,
Expand Down Expand Up @@ -723,10 +722,6 @@ const ActivitySummaryPage = () => {
);
};

const getClassCondition = (classes: any, condition: boolean, classNameYes: any, classNameNo: any) => {
return condition ? classNameYes : classNameNo;
};

const useStyles = makeStylesEdt<{ modifiable: boolean }>({ "name": { ActivitySummaryPage } })(
(theme, { modifiable }) => ({
infoBox: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ const HelpSubCategoryActivity = () => {
const source = getSource(SourcesEnum.ACTIVITY_SURVEY);
const data = mockData();

if (data && data.COLLECTED && data.COLLECTED[FieldNameEnum.MAINACTIVITY_ID])
if (data?.COLLECTED?.[FieldNameEnum.MAINACTIVITY_ID])
data.COLLECTED[FieldNameEnum.MAINACTIVITY_ID].COLLECTED = ["120"];

if (data && data.COLLECTED && data.COLLECTED[FieldNameEnum.MAINACTIVITY_ISFULLYCOMPLETED])
if (data?.COLLECTED?.[FieldNameEnum.MAINACTIVITY_ISFULLYCOMPLETED])
data.COLLECTED[FieldNameEnum.MAINACTIVITY_ISFULLYCOMPLETED].COLLECTED = [false];

const [helpStep, setHelpStep] = React.useState(3);
Expand Down
19 changes: 11 additions & 8 deletions src/pages/home-surveyed/HomeSurveyed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,11 @@ const HomeSurveyedPage = () => {
{renderReminderNote()}
<Box className={classes.groupCardBox}>
{interviewersUniques.map((interviewer, index) => (
<>
<PersonCard
numPerson={index}
values={arrayOfSurveysPersonDemo(interviewer, index)}
/>
</>
<PersonCard
key={index}
numPerson={index}
values={arrayOfSurveysPersonDemo(interviewer, index)}
/>
))}
</Box>
</>,
Expand All @@ -273,7 +272,11 @@ const HomeSurveyedPage = () => {
{renderReminderNote()}
<Box className={classes.groupCardBox}>
{groups.map((group, index) => (
<PersonCard numPerson={index} values={userDataGroupedInterv[group]} />
<PersonCard
key={index}
numPerson={index}
values={userDataGroupedInterv[group]}
/>
))}
</Box>
</>
Expand Down Expand Up @@ -304,7 +307,7 @@ const HomeSurveyedPage = () => {

<Box className={classes.groupCardBox}>
{groups.map((group, index) => (
<PersonCard numPerson={index} values={userDatas[group]} />
<PersonCard key={index} numPerson={index} values={userDatas[group]} />
))}
</Box>
<Box className={classes.navButtonsBox}>
Expand Down
13 changes: 9 additions & 4 deletions src/pages/surveys-overview/SurveysOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ const SurveysOverviewPage = () => {
homeIcon={home}
homeIconAlt={t("accessibility.asset.mui-icon.home")}
>
<Box className={cx(classes.title, isItMobile ? classes.titleMobile : "")}>
<Box className={cx(classes.title, getClassCondition(isItMobile, classes.titleMobile, ""))}>
<img src={stats} alt={t("accessibility.asset.stats-alt")} />
<Typography className={classes.label}>{t("page.surveys-overview.title")}</Typography>
</Box>
<Box className={classes.searchBox}>
<Box
className={cx(
classes.innerSearchBox,
getClassCondition(classes, isItMobile, classes.innerSearchMobileBox, ""),
getClassCondition(isItMobile, classes.innerSearchMobileBox, ""),
)}
>
<OutlinedInput
Expand Down Expand Up @@ -384,7 +384,7 @@ const SurveysOverviewPage = () => {
<Box
className={cx(
classes.refreshBox,
getClassCondition(classes, isItMobile, classes.refreshMobileBox, ""),
getClassCondition(isItMobile, classes.refreshMobileBox, ""),
)}
>
<Button
Expand All @@ -401,7 +401,12 @@ const SurveysOverviewPage = () => {
</Button>
</Box>
{process.env.REACT_APP_NODE_ENV !== "production" && (
<Box className={cx(classes.refreshBox, isItMobile ? classes.refreshMobileBox : "")}>
<Box
className={cx(
classes.refreshBox,
getClassCondition(isItMobile, classes.refreshMobileBox, ""),
)}
>
<Button
color="primary"
variant="contained"
Expand Down
15 changes: 5 additions & 10 deletions src/pages/work-time/weekly-planner/WeeklyPlanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ const WeeklyPlannerPage = () => {
dataResponse.COLLECTED?.[FieldNameEnum.FIRSTNAME].COLLECTED ==
callbackData.COLLECTED?.[FieldNameEnum.FIRSTNAME].COLLECTED
) {
saveData(idSurvey, callbackData).then(() => console.log("save"));
//saveData(idSurvey, callbackData);
saveData(idSurvey, callbackData);
}
}
};

const saveDuration = (idSurveyResponse: string, response: responsesHourChecker) => {
const callbackData = callbackHolder.getData();
const dataCopy = Object.assign({}, callbackData);
const dataCopy = { ...callbackData };
const dates = (dataCopy?.COLLECTED?.["DATES"].COLLECTED ??
getArrayFromSession("DATES")) as string[];
const currentDateIndex = dates.indexOf(response.date);
Expand All @@ -96,15 +95,11 @@ const WeeklyPlannerPage = () => {
let quartier = dataCopy?.COLLECTED?.[name].COLLECTED as string[];
quartier[currentDateIndex] = response.values[name] + "";

if (dataCopy && dataCopy.COLLECTED) {
if (dataCopy?.COLLECTED) {
dataCopy.COLLECTED[name].COLLECTED = quartier;
}
});
saveData(idSurveyResponse, dataCopy).then(data => {
//saveData(idSurveyResponse, dataCopy).then(data => {
console.log(idSurveyResponse, data, dataCopy);
//})
});
saveData(idSurveyResponse, dataCopy);
}
};

Expand Down Expand Up @@ -176,7 +171,7 @@ const WeeklyPlannerPage = () => {

const validateAndNav = (): void => {
if (displayDayOverview) {
//save(idSurvey);
save(idSurvey);
if (isPlaceWorkDisplayed) {
setDisplayDayOverview(true);
setIsPlaceWorkDisplayed(false);
Expand Down
24 changes: 12 additions & 12 deletions src/service/navigation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,19 @@ const setNamesOfGroup = (idSurvey: string, nameAct: string, idsSurveysOfGroup?:
const promises: any[] = [];
if (nameAct ?? (listNames && listNames?.length > 0)) {
idsSurveysOfGroup?.forEach(id => {
//let firstname = getValue(id, FieldNameEnum.FIRSTNAME);
//if (firstname == null) {
const name = nameAct ?? listNames?.[0];
let dataAct = setValue(id, FieldNameEnum.FIRSTNAME, name);
if (dataAct.COLLECTED == null || dataAct.COLLECTED?.[FieldNameEnum.FIRSTNAME] == null) {
dataAct = emptyDataSetFirstName(
callbackHolder.getData(),
name,
getModePersistence(callbackHolder.getData()),
);
let firstname = getValue(id, FieldNameEnum.FIRSTNAME);
if (firstname == null) {
const name = nameAct ?? listNames?.[0];
let dataAct = setValue(id, FieldNameEnum.FIRSTNAME, name);
if (dataAct.COLLECTED == null || dataAct.COLLECTED?.[FieldNameEnum.FIRSTNAME] == null) {
dataAct = emptyDataSetFirstName(
callbackHolder.getData(),
name,
getModePersistence(callbackHolder.getData()),
);
}
promises.push(saveData(id, dataAct));
}
promises.push(saveData(id, dataAct));
//}
});
}
return promises;
Expand Down
21 changes: 9 additions & 12 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ const getSurveyDataHousehold = (surveys: UserSurveys[]) => {
firstSurvey?.data?.surveyUnitId,
FieldNameEnum.SURVEYDATE,
) as string;
return firstSurveyDate && firstSurveyDate.length && firstSurveyDate.length > 0
return firstSurveyDate?.length && firstSurveyDate.length > 0
? dayjs(generateDateFromStringInput(firstSurveyDate)).format("DD/MM/YYYY")
: undefined;
};
Expand Down Expand Up @@ -1469,17 +1469,17 @@ const isDemoMode = () => {
const surveyLocked = (idSurvey: string) => {
const isLocked = getValue(idSurvey, FieldNameEnum.ISLOCKED) as boolean;
const variableEdited = existVariableEdited(idSurvey);
return (isLocked != null && isLocked == true) || variableEdited;
return (isLocked != null && isLocked) || variableEdited;
};

const surveyValidated = (idSurvey: string) => {
const isValidated = getValue(idSurvey, FieldNameEnum.ISVALIDATED) as boolean;
return isValidated != null && isValidated == true;
return isValidated != null && isValidated;
};

const surveyClosed = (idSurvey: string) => {
const isClosed = getValue(idSurvey, FieldNameEnum.ISCLOSED) as boolean;
return isClosed != null && isClosed == true;
return isClosed != null && isClosed;
};

const surveyStarted = (idSurvey: string) => {
Expand Down Expand Up @@ -1679,14 +1679,11 @@ const existVariableEdited = (idSurvey?: string, data?: LunaticData) => {
if (prop == FieldNameEnum.FIRSTNAME) continue;
const data = dataOfSurvey && dataOfSurvey[prop];
const ifArrayInputed =
data &&
data.EDITED &&
data?.EDITED &&
Array.isArray(data.EDITED) &&
data.EDITED.length > 0 &&
data.EDITED[0] != null;
if (data && data.EDITED && ifArrayInputed) {
return true;
} else if (data && data.EDITED && !Array.isArray(data.EDITED)) {
if ((data?.EDITED && ifArrayInputed) || (data?.EDITED && !Array.isArray(data.EDITED))) {
return true;
}
}
Expand All @@ -1695,7 +1692,7 @@ const existVariableEdited = (idSurvey?: string, data?: LunaticData) => {

const getModePersistence = (data: LunaticData | undefined): ModePersistenceEnum => {
const isReviewerMode = isReviewer();
const isLocked = (data?.COLLECTED?.[FieldNameEnum.ISLOCKED]?.COLLECTED as boolean) == true;
const isLocked = data?.COLLECTED?.[FieldNameEnum.ISLOCKED]?.COLLECTED as boolean;
const variableEdited = existVariableEdited(undefined, data);

return isReviewerMode || isLocked || variableEdited
Expand Down Expand Up @@ -1733,9 +1730,9 @@ const getStatutSurvey = (idSurvey: string) => {
const isLocked = getValue(idSurvey, FieldNameEnum.ISLOCKED) as boolean;
const isValidated = getValue(idSurvey, FieldNameEnum.ISVALIDATED) as boolean;
const variableEdited = existVariableEdited(idSurvey);
if (isValidated != null && isValidated == true) {
if (isValidated != null && isValidated) {
return StateSurveyEnum.VALIDATED;
} else if ((isLocked != null && isLocked == true) || variableEdited) {
} else if ((isLocked != null && isLocked) || variableEdited) {
return StateSurveyEnum.LOCKED;
} else return StateSurveyEnum.INIT;
};
Expand Down
Loading

0 comments on commit 049d4f3

Please sign in to comment.