diff --git a/ui/src/campagnes/CreateCampagnes/FormationsSelector.js b/ui/src/campagnes/CreateCampagnes/FormationsSelector.js index 3d52029b..8d173654 100644 --- a/ui/src/campagnes/CreateCampagnes/FormationsSelector.js +++ b/ui/src/campagnes/CreateCampagnes/FormationsSelector.js @@ -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) => { @@ -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), diff --git a/ui/src/campagnes/Shared/CampagnesSelector/CampagnesSelector.js b/ui/src/campagnes/Shared/CampagnesSelector/CampagnesSelector.js index cde1cbdf..7fc0e469 100644 --- a/ui/src/campagnes/Shared/CampagnesSelector/CampagnesSelector.js +++ b/ui/src/campagnes/Shared/CampagnesSelector/CampagnesSelector.js @@ -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]), + ]); } }, },