Skip to content

Commit

Permalink
Merge pull request #346 from mission-apprentissage/ui/fix-campagne-fo…
Browse files Browse the repository at this point in the history
…rmation-selector

[UI] Répare campagne et formation selector
  • Loading branch information
yohanngab authored Oct 30, 2024
2 parents ee2ee76 + f602ac5 commit e86cdc8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
25 changes: 19 additions & 6 deletions ui/src/campagnes/CreateCampagnes/FormationsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ const REMOTE_FORMATION_FIELDS = {
code_postal: 1,
};

const buildSiretQuery = (isAdmin, userSiret) => {
const formattedUserSiret = userSiret
.map((siret) => [{ etablissement_formateur_siret: siret }, { etablissement_gestionnaire_siret: siret }])
.flat();
return isAdmin ? {} : { $or: formattedUserSiret };
const buildSiretQuery = (isAdmin, userSiret, hasAlreadyFetch) => {
let formattedUserSiret = [];

if (!hasAlreadyFetch) {
formattedUserSiret = userSiret.map((siret) => [
{ etablissement_formateur_siret: siret },
{ etablissement_gestionnaire_siret: siret },
]);
} else {
formattedUserSiret = userSiret.map((siret) => [{ etablissement_formateur_siret: siret }]);
}

return isAdmin ? {} : { $or: formattedUserSiret.flat() };
};

const buildSearchQuery = (searchTerm) => {
Expand Down Expand Up @@ -81,11 +89,16 @@ const FormationsSelector = ({ selectedFormations, setSelectedFormations }) => {

const isAdmin = userContext.user?.role === USER_ROLES.ADMIN;
const userSiret = userContext.user?.etablissements?.map((etablissement) => etablissement.siret) || [];
const hasAlreadyFetch = etablissementsOptions.length && diplomesOptions.length;

const query = {
$and: [
REMOTE_FORMATION_BASE_QUERY,
buildSiretQuery(isAdmin, selectedEtablissementsSiret?.length ? selectedEtablissementsSiret : userSiret),
buildSiretQuery(
isAdmin,
selectedEtablissementsSiret?.length ? selectedEtablissementsSiret : userSiret,
hasAlreadyFetch
),
{
...(selectedDiplomesIntitule?.length && {
...buildDiplomeQuery(selectedDiplomesIntitule),
Expand Down
25 changes: 8 additions & 17 deletions ui/src/campagnes/Shared/CampagnesSelector/CampagnesSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,14 @@ const CampagnesSelector = ({
name: `selectAll`,
checked: hasSelectedAllCampagneFromCurrentPage,
onChange: () => {
if (isResults) {
if (hasSelectedAllCampagneFromCurrentPage) {
setSelectedCampagneIds([]);
} else {
setSelectedCampagneIds(campagnesIds);
}
}
if (isManage) {
if (hasSelectedAllCampagneFromCurrentPage) {
setSelectedCampagneIds((prevValues) => [
...prevValues.filter((id) => !currentPageCampagneIds.includes(id)),
]);
} else {
setSelectedCampagneIds((prevValues) => [
...new Set([...prevValues, ...currentPageCampagneIds]),
]);
}
if (hasSelectedAllCampagneFromCurrentPage) {
setSelectedCampagneIds((prevValues) => [
...prevValues.filter((id) => !currentPageCampagneIds.includes(id)),
]);
} else {
setSelectedCampagneIds((prevValues) => [
...new Set([...prevValues, ...currentPageCampagneIds]),
]);
}
},
},
Expand Down

0 comments on commit e86cdc8

Please sign in to comment.