Skip to content

Commit

Permalink
Merge pull request #510 from mission-apprentissage/fix/page-formation…
Browse files Browse the repository at this point in the history
…-erreur-500

fix: mise à jour du cfd selectionné quand la liste des formations est vide
  • Loading branch information
gBusato authored Dec 19, 2024
2 parents 53ee0a0 + 60aa41c commit d1aa7f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ const TabContent = forwardRef<TabContentProps, "div">(({ tab, ...rest }, ref) =>
);
});

const getFirstFormation = (formationsByLibelleNiveauDiplome: Record<string, FormationListItem[]>) => {
const libellesNiveauDiplome = Object.keys(formationsByLibelleNiveauDiplome);

if (libellesNiveauDiplome.length === 0) {
return "";
}

const firstFormation = formationsByLibelleNiveauDiplome[libellesNiveauDiplome[0]][0];
return firstFormation.cfd;
};

const useFormationSection = (
formations: FormationListItem[],
formationsByLibelleNiveauDiplome: Record<string, FormationListItem[]>
Expand All @@ -61,11 +72,14 @@ const useFormationSection = (
}, [tabContentRef]);

useEffect(() => {
const cfdInListOfFormations = formations.find((f) => f.cfd === currentFilters.cfd);
if (currentFilters.cfd !== "") {
const cfdInListOfFormations = formations.find((f) => f.cfd === currentFilters.cfd);

if (!cfdInListOfFormations) {
const firstFormation = formationsByLibelleNiveauDiplome[Object.keys(formationsByLibelleNiveauDiplome)[0]][0];
handleCfdChange(firstFormation.cfd);
if (!cfdInListOfFormations) {
handleCfdChange(getFirstFormation(formationsByLibelleNiveauDiplome));
}
} else if (currentFilters.cfd === "" && Object.keys(formationsByLibelleNiveauDiplome).length > 0) {
handleCfdChange(getFirstFormation(formationsByLibelleNiveauDiplome));
}
}, [currentFilters, formations, handleCfdChange, formationsByLibelleNiveauDiplome]);

Expand Down
7 changes: 4 additions & 3 deletions ui/app/(wrapped)/domaine-de-formation/[codeNsf]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isAxiosError } from "axios";
import _ from "lodash";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import type { ScopeZone } from "shared";
import { ScopeEnum } from "shared";

import { serverClient } from "@/api.client";
Expand Down Expand Up @@ -53,16 +54,16 @@ const findDefaultCfd = (
}
const firstFormations = formationByCodeNiveauDiplome[Object.keys(formationByCodeNiveauDiplome)[0]];

const formationWithAtLeastOneEtab = firstFormations.filter((f) => f.nbEtab > 0);
const formationWithAtLeastOneEtab = firstFormations?.filter((f) => f.nbEtab > 0);

return formationWithAtLeastOneEtab[0]?.cfd;
return formationWithAtLeastOneEtab?.[0]?.cfd ?? "";
};

const defineScope = (
codeRegion: string | undefined,
codeAcademie: string | undefined,
codeDepartement: string | undefined
) => {
): ScopeZone => {
if (codeDepartement) {
return ScopeEnum.département;
}
Expand Down

0 comments on commit d1aa7f7

Please sign in to comment.