Skip to content

Commit

Permalink
fix link Inscription from HTS
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric013 committed Oct 25, 2024
1 parent 023027b commit f4fb5d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
12 changes: 11 additions & 1 deletion admin/src/scenes/classe/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,23 @@ export const getHeaderActionList = ({ user, classe, setClasse, isLoading, setIsL
}

const isManualInscriptionActionDisabled = !(classe?.status === STATUS_CLASSE.OPEN || canPerformManualInscriptionActions);
const optionsInscriptionFiltered =
classe?.status !== STATUS_CLASSE.OPEN && canPerformManualInscriptionActions
? [
{
...optionsInscription[0],
// override items to keep only manual inscription
items: optionsInscription[0].items.filter((i) => i.key === "manual"),
},
]
: optionsInscription;

actionsList.push(
<DropdownButton
key="inscription"
title="Inscrire les élèves"
type="wired"
optionsGroup={optionsInscription}
optionsGroup={optionsInscriptionFiltered}
position="right"
buttonClassName={cx("mr-2", isManualInscriptionActionDisabled && "cursor-not-allowed")}
disabled={isManualInscriptionActionDisabled}
Expand Down
18 changes: 3 additions & 15 deletions admin/src/scenes/classe/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { toastr } from "react-redux-toastr";
import { Page, Header, Badge } from "@snu/ds/admin";
import { capture } from "@/sentry";
import api from "@/services/api";
import { translate, YOUNG_STATUS, STATUS_CLASSE, translateStatusClasse, COHORT_TYPE, FUNCTIONAL_ERRORS, LIMIT_DATE_ESTIMATED_SEATS, ClassesRoutes, ROLES } from "snu-lib";
import { translate, YOUNG_STATUS, STATUS_CLASSE, translateStatusClasse, COHORT_TYPE, FUNCTIONAL_ERRORS, LIMIT_DATE_ESTIMATED_SEATS, ClassesRoutes, canInviteYoung } from "snu-lib";
import { appURL } from "@/config";
import Loader from "@/components/Loader";
import { AuthState } from "@/redux/auth/reducer";
Expand Down Expand Up @@ -225,20 +225,8 @@ export default function View() {
};

const canPerformManualInscriptionActions = useMemo(() => {
if (!cohort) return false;

switch (user.role) {
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
return cohort.inscriptionOpenForReferentRegion === true;
case ROLES.REFERENT_CLASSE:
return cohort.inscriptionOpenForReferentClasse === true;
case ROLES.ADMINISTRATEUR_CLE:
return cohort.inscriptionOpenForAdministrateurCle === true;
default:
return false;
}
return canInviteYoung(user, cohort ?? null);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user.role, cohort]);

if (!classe) return <Loader />;
Expand Down
64 changes: 59 additions & 5 deletions admin/src/scenes/inscription/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from "dayjs";
import React, { Fragment, useEffect, useState } from "react";
import React, { Fragment, useCallback, useEffect, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import { Link } from "react-router-dom";
import useDocumentTitle from "../../hooks/useDocumentTitle";
Expand All @@ -8,7 +8,7 @@ import { Listbox, Transition } from "@headlessui/react";
import { AiOutlinePlus } from "react-icons/ai";
import { BsDownload } from "react-icons/bs";
import { HiOutlineChevronDown, HiOutlineChevronUp } from "react-icons/hi";
import { getDepartmentNumber, translateCniExpired, translateYoungSource } from "snu-lib";
import { canInviteYoung, getDepartmentNumber, translateCniExpired, translateYoungSource } from "snu-lib";
import Badge from "../../components/Badge";
import Breadcrumbs from "../../components/Breadcrumbs";
import { ExportComponent, Filters, ResultTable, Save, SelectedFilters, SortOption } from "../../components/filters-system-v2";
Expand Down Expand Up @@ -64,8 +64,61 @@ export default function Inscription() {
})();
}, []);

const baseInscriptionPath = useMemo(() => {
const hasFilterSelectedOneClass = selectedFilters?.classeId?.filter?.length === 1;
const selectedClassId = selectedFilters?.classeId?.filter[0];

if (!hasFilterSelectedOneClass) {
return "/inscription/create";
}

const classe = classes?.find((c) => c._id === selectedClassId);

if (!classe) {
toastr.error("Oups, une erreur est survenue lors de la récupération de la classe");
return "/inscription/create";
}

return `/inscription/create?classeId=${selectedClassId}`;
}, [classes, selectedFilters?.classeId?.filter]);

const fetchCohortAndResolveInscriptionPath = useCallback(async () => {
if (!baseInscriptionPath.includes("classeId")) {
return baseInscriptionPath;
}

const selectedClassId = selectedFilters?.classeId?.filter[0];
const classe = classes?.find((c) => c._id === selectedClassId);

try {
const { ok, data: cohort } = await api.get(`/cohort/${classe.cohortId}`);
if (!ok) {
throw new Error("Failed to fetch cohort");
}

if (!canInviteYoung(user, cohort)) {
return null;
}

return baseInscriptionPath;
} catch (error) {
toastr.error("Oups, une erreur est survenue lors de la récupération de la cohort");
return "/inscription/create";
}
}, [baseInscriptionPath, classes, selectedFilters?.classeId?.filter, user]);

if (!classes || !etablissements) return <Loader />;

const handleClickInscription = async (event) => {
event.preventDefault();
plausibleEvent("Inscriptions/CTA - Nouvelle inscription");

const inscriptionPath = await fetchCohortAndResolveInscriptionPath();
if (inscriptionPath) {
history.push(inscriptionPath);
}
};

const filterArray = [
{ title: "Cohorte", name: "cohort", parentGroup: "Général", missingLabel: "Non renseigné", sort: orderCohort },
{ title: "Autorisation de participation", name: "parentAllowSNU", parentGroup: "Général", missingLabel: "Non renseigné", translate: translate },
Expand Down Expand Up @@ -238,11 +291,12 @@ export default function Inscription() {
<Title>Inscriptions</Title>
<div className="flex items-center gap-2">
<Link
to={selectedFilters?.classeId?.filter?.length === 1 ? `/volontaire/create?classeId=${selectedFilters?.classeId?.filter[0]}` : "/volontaire/create"}
onClick={() => plausibleEvent("Inscriptions/CTA - Nouvelle inscription")}
disabled={true}
to={baseInscriptionPath}
onClick={handleClickInscription}
className="ml-auto flex items-center gap-3 rounded-lg border-[1px] text-white border-blue-600 bg-blue-600 px-3 py-2 text-sm hover:bg-white hover:!text-blue-600 transition ease-in-out">
<AiOutlinePlus className="text-white h-4 w-4 group-hover:!text-blue-600" />

{/* TODO: condition a ajouter ici : canInviteYoung */}
<p>{selectedFilters?.classeId?.filter?.length === 1 ? "Nouvelle inscription CLE" : "Nouvelle inscription HTS"}</p>
</Link>
<ExportComponent
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ function canInviteYoung(actor: UserDto, cohort: CohortDto | null) {
if (!cohort) return false;

switch (actor.role) {
case ROLES.ADMIN:
return true;
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
Expand Down

0 comments on commit f4fb5d0

Please sign in to comment.