11import { OrgUnitsSelector } from "@eyeseetea/d2-ui-components" ;
2- import { useEffect } from "react" ;
2+ import { useEffect , useMemo } from "react" ;
33import { COUNTRY_OU_LEVEL , HOSPITAL_OU_LEVELS } from "../../../data/repositories/UserD2Repository" ;
44import { Id } from "../../../domain/entities/Ref" ;
55import { SURVEYS_WITH_ORG_UNIT_SELECTOR , SURVEY_FORM_TYPES } from "../../../domain/entities/Survey" ;
6- import { OrgUnitAccess } from "../../../domain/entities/User" ;
6+ import { OrgUnitAccess , UserOrgUnit } from "../../../domain/entities/User" ;
77import { GLOBAL_OU_ID } from "../../../domain/usecases/SaveFormDataUseCase" ;
88import { getParentOUIdFromPath } from "../../../domain/utils/PPSProgramsHelper" ;
99import { useAppContext } from "../../contexts/app-context" ;
1010import { useCurrentSurveys } from "../../contexts/current-surveys-context" ;
1111import { useSurveyFormOUSelector } from "./hook/useSurveyFormOUSelector" ;
1212import { useOfflineSnackbar } from "../../hooks/useOfflineSnackbar" ;
13+ import _c from "../../../domain/entities/generic/Collection" ;
1314
1415export interface SurveyFormOUSelectorProps {
1516 formType : SURVEY_FORM_TYPES ;
@@ -24,7 +25,7 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
2425 setCurrentOrgUnit,
2526 currentSurveyId,
2627} ) => {
27- const { api } = useAppContext ( ) ;
28+ const { api, currentUser } = useAppContext ( ) ;
2829 const { currentPPSSurveyForm, currentCountryQuestionnaire, currentPrevalenceSurveyForm } =
2930 useCurrentSurveys ( ) ;
3031 const { onOrgUnitChange, ouSelectorErrMsg, shouldRefresh } = useSurveyFormOUSelector (
@@ -40,6 +41,47 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
4041 }
4142 } , [ ouSelectorErrMsg , snackbar , shouldRefresh , offlineError ] ) ;
4243
44+ const rootIds = useMemo ( ( ) => {
45+ if ( formType === "PPSHospitalForm" ) {
46+ // For HOSP PPS surveys, show all hospitals across all OUs
47+ if ( currentPPSSurveyForm ?. surveyType === "HOSP" ) {
48+ return [ GLOBAL_OU_ID ] ;
49+ }
50+ // For non-admin user, currentCountryQuestionnaire won't be set. Get parent id from path
51+ return currentCountryQuestionnaire ?. orgUnitId
52+ ? [ currentCountryQuestionnaire . orgUnitId ]
53+ : [ getParentOUIdFromPath ( currentOrgUnit ?. orgUnitPath ) ] ;
54+ }
55+
56+ if ( formType === "PrevalenceFacilityLevelForm" ) {
57+ return [ currentPrevalenceSurveyForm ?. orgUnitId ] ;
58+ }
59+
60+ if ( formType === "WardSummaryStatisticsForm" ) {
61+ return getRootIds ( currentUser . organisationUnits ) ;
62+ }
63+
64+ return [ GLOBAL_OU_ID ] ;
65+ } , [
66+ formType ,
67+ currentPPSSurveyForm ?. surveyType ,
68+ currentCountryQuestionnaire ?. orgUnitId ,
69+ currentOrgUnit ?. orgUnitPath ,
70+ currentPrevalenceSurveyForm ?. orgUnitId ,
71+ currentUser . organisationUnits ,
72+ ] ) ;
73+
74+ const selectableLevels = useMemo ( ( ) => {
75+ switch ( formType ) {
76+ case "PPSCountryQuestionnaire" :
77+ case "PrevalenceSurveyForm" :
78+ case "WardSummaryStatisticsForm" :
79+ return HOSPITAL_OU_LEVELS ;
80+ default :
81+ return [ COUNTRY_OU_LEVEL ] ;
82+ }
83+ } , [ formType ] ) ;
84+
4385 return (
4486 < >
4587 { SURVEYS_WITH_ORG_UNIT_SELECTOR . includes ( formType ) && (
@@ -54,33 +96,30 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
5496 singleSelection = { true }
5597 typeInput = { "radio" }
5698 hideMemberCount = { false }
57- selectableLevels = {
58- formType === "PPSCountryQuestionnaire" ||
59- formType === "PrevalenceSurveyForm"
60- ? [ COUNTRY_OU_LEVEL ]
61- : HOSPITAL_OU_LEVELS
62- }
99+ selectableLevels = { selectableLevels }
63100 controls = { {
64101 filterByLevel : false ,
65102 filterByGroup : false ,
66103 filterByProgram : false ,
67104 selectAll : false ,
68105 } }
69- rootIds = {
70- formType === "PPSHospitalForm"
71- ? currentPPSSurveyForm ?. surveyType === "HOSP" //For HOSP PPS surveys, show all hospitals across all OUs
72- ? [ GLOBAL_OU_ID ]
73- : currentCountryQuestionnaire ?. orgUnitId
74- ? [ currentCountryQuestionnaire ?. orgUnitId ] //For non-admin user, currentCountryQuestionnaire wont be set. Get parent id from path
75- : [ getParentOUIdFromPath ( currentOrgUnit ?. orgUnitPath ) ]
76- : formType === "PrevalenceFacilityLevelForm"
77- ? [ currentPrevalenceSurveyForm ?. orgUnitId ]
78- : [ GLOBAL_OU_ID ]
79- }
106+ rootIds = { rootIds }
80107 showShortName = { true }
81108 showNameSetting = { true }
82109 />
83110 ) }
84111 </ >
85112 ) ;
86113} ;
114+
115+ function getRoots ( orgUnits : UserOrgUnit [ ] ) : UserOrgUnit [ ] {
116+ const minLevel = Math . min ( ...orgUnits . map ( ou => ou . level ) ) ;
117+ return _c ( orgUnits )
118+ . filter ( ou => ou . level === minLevel )
119+ . sortBy ( ou => ou . name )
120+ . value ( ) ;
121+ }
122+
123+ export function getRootIds ( orgUnits : UserOrgUnit [ ] ) : Id [ ] {
124+ return getRoots ( orgUnits ) . map ( ou => ou . id ) ;
125+ }
0 commit comments