1- import react from 'react' ;
1+ import react , { useMemo } from 'react' ;
22import { useOutletContext } from 'react-router-dom' ;
33import {
44 LocationIcon ,
@@ -14,7 +14,6 @@ import { useTranslation } from '@ifrc-go/ui/hooks';
1414import { sumSafe } from '@ifrc-go/ui/utils' ;
1515import {
1616 isDefined ,
17- isNotDefined ,
1817 isTruthyString ,
1918} from '@togglecorp/fujs' ;
2019import {
@@ -34,7 +33,7 @@ import BaseMap from '#components/domain/BaseMap';
3433import Link , { type Props as LinkProps } from '#components/Link' ;
3534import MapContainerWithDisclaimer from '#components/MapContainerWithDisclaimer' ;
3635import MapPopup from '#components/MapPopup' ;
37- import useUserMe from '#hooks/domain/useUserMe ' ;
36+ import usePermissions from '#hooks/domain/usePermissions ' ;
3837import useFilterState from '#hooks/useFilterState' ;
3938import {
4039 COLOR_DARK_GREY ,
@@ -49,7 +48,11 @@ import {
4948 useRequest ,
5049} from '#utils/restRequest' ;
5150
52- import { VALIDATED } from '../common' ;
51+ import {
52+ AUTHENTICATED ,
53+ PUBLIC ,
54+ VALIDATED ,
55+ } from '../common' ;
5356import Filters , { FilterValue } from '../Filters' ;
5457
5558import i18n from './i18n.json' ;
@@ -127,13 +130,28 @@ function LocalUnitsMap() {
127130 [ limit , filter , countryResponse ] ,
128131 ) ;
129132
130- const meResponse = useUserMe ( ) ;
133+ const { isCountryAdmin, isSuperUser } = usePermissions ( ) ;
134+
135+ const requestType = useMemo (
136+ ( ) => {
137+ if ( isSuperUser ) {
138+ return 'authenticated' ;
139+ }
140+
141+ if ( isCountryAdmin ( countryResponse ?. id ) ) {
142+ return 'authenticated' ;
143+ }
144+
145+ return 'public' ;
146+ } ,
147+ [ countryResponse , isSuperUser , isCountryAdmin ] ,
148+ ) ;
131149
132150 const {
133151 response : publicLocalUnitsResponse ,
134152 pending : publicLocalUnitsPending ,
135153 } = useRequest ( {
136- skip : isNotDefined ( countryResponse ?. iso3 ) || meResponse ?. is_superuser ,
154+ skip : requestType !== PUBLIC ,
137155 url : '/api/v2/public-local-units/' ,
138156 query : urlQuery ,
139157 } ) ;
@@ -142,14 +160,13 @@ function LocalUnitsMap() {
142160 response : localUnitsResponse ,
143161 pending : localUnitsPending ,
144162 } = useRequest ( {
145- skip : isNotDefined ( countryResponse ?. iso3 )
146- || isNotDefined ( meResponse )
147- || ! meResponse . is_superuser ,
163+ skip : requestType !== AUTHENTICATED ,
148164 url : '/api/v2/local-units/' ,
149165 query : urlQuery ,
150166 } ) ;
151167
152- const localUnits = meResponse ?. is_superuser ? localUnitsResponse : publicLocalUnitsResponse ;
168+ const localUnits = ( isSuperUser || isCountryAdmin ( countryResponse ?. id ) )
169+ ? localUnitsResponse : publicLocalUnitsResponse ;
153170 const pending = publicLocalUnitsPending || localUnitsPending ;
154171
155172 const strings = useTranslation ( i18n ) ;
@@ -218,7 +235,7 @@ function LocalUnitsMap() {
218235 pending : publicLocalUnitDetailPending ,
219236 error : publicLocalUnitDetailError ,
220237 } = useRequest ( {
221- skip : isNotDefined ( clickedPointProperties ?. localUnitId ) || meResponse ?. is_superuser ,
238+ skip : requestType !== PUBLIC ,
222239 url : '/api/v2/public-local-units/{id}/' ,
223240 pathVariables : isDefined ( clickedPointProperties ) ? ( {
224241 id : clickedPointProperties . localUnitId ,
@@ -230,24 +247,22 @@ function LocalUnitsMap() {
230247 pending : superLocalUnitDetailPending ,
231248 error : superLocalUnitDetailError ,
232249 } = useRequest ( {
233- skip : isNotDefined ( clickedPointProperties ?. localUnitId )
234- || isNotDefined ( meResponse )
235- || ! meResponse . is_superuser ,
250+ skip : requestType !== AUTHENTICATED ,
236251 url : '/api/v2/local-units/{id}/' ,
237252 pathVariables : isDefined ( clickedPointProperties ) ? ( {
238253 id : clickedPointProperties . localUnitId ,
239254 } ) : undefined ,
240255 } ) ;
241256
242- const localUnitDetail = meResponse ?. is_superuser
257+ const localUnitDetail = requestType !== AUTHENTICATED
243258 ? superLocalUnitDetailResponse
244259 : publicLocalUnitDetailResponse ;
245260
246- const localUnitDetailPending = meResponse ?. is_superuser
261+ const localUnitDetailPending = requestType !== AUTHENTICATED
247262 ? superLocalUnitDetailPending
248263 : publicLocalUnitDetailPending ;
249264
250- const localUnitDetailError = meResponse ?. is_superuser
265+ const localUnitDetailError = requestType !== AUTHENTICATED
251266 ? superLocalUnitDetailError
252267 : publicLocalUnitDetailError ;
253268
0 commit comments