Skip to content

Commit 0a41e49

Browse files
authored
Merge pull request #958 from IFRCGo/fix/endpoint-permission-fix
Update permission for local unit endpoints
2 parents 11e8d48 + f83c12b commit 0a41e49

File tree

4 files changed

+82
-56
lines changed

4 files changed

+82
-56
lines changed

.changeset/great-carrots-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"go-web-app": patch
3+
---
4+
5+
Show Local name when available and use English name as fallback for local units data

app/src/utils/common.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
compareNumber,
33
isNotDefined,
4+
isTruthyString,
45
} from '@togglecorp/fujs';
56

67
import type { GoApiResponse } from '#utils/restRequest';
@@ -75,3 +76,16 @@ export function downloadFile(
7576

7677
URL.revokeObjectURL(url);
7778
}
79+
80+
export function chooseName(
81+
primaryName: string | null | undefined,
82+
secondaryName: string | null | undefined,
83+
) {
84+
if (isTruthyString(primaryName)) {
85+
return primaryName;
86+
}
87+
if (isTruthyString(secondaryName)) {
88+
return secondaryName;
89+
}
90+
return '--';
91+
}

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/index.tsx

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import BaseMap from '#components/domain/BaseMap';
3434
import Link, { type Props as LinkProps } from '#components/Link';
3535
import MapContainerWithDisclaimer from '#components/MapContainerWithDisclaimer';
3636
import MapPopup from '#components/MapPopup';
37-
import usePermissions from '#hooks/domain/usePermissions';
37+
import useAuth from '#hooks/domain/useAuth';
3838
import useFilterState from '#hooks/useFilterState';
39+
import { chooseName } from '#utils/common';
3940
import {
4041
COLOR_DARK_GREY,
4142
COLOR_PRIMARY_BLUE,
@@ -131,21 +132,17 @@ function LocalUnitsMap() {
131132
[limit, filter, countryResponse],
132133
);
133134

134-
const { isCountryAdmin, isSuperUser } = usePermissions();
135+
const { isAuthenticated } = useAuth();
135136

136137
const requestType = useMemo(
137138
() => {
138-
if (isSuperUser) {
139-
return 'authenticated';
140-
}
141-
142-
if (isCountryAdmin(countryResponse?.id)) {
139+
if (isAuthenticated) {
143140
return 'authenticated';
144141
}
145142

146143
return 'public';
147144
},
148-
[countryResponse, isSuperUser, isCountryAdmin],
145+
[isAuthenticated],
149146
);
150147

151148
const {
@@ -166,7 +163,7 @@ function LocalUnitsMap() {
166163
query: urlQuery,
167164
});
168165

169-
const localUnits = (isSuperUser || isCountryAdmin(countryResponse?.id))
166+
const localUnits = requestType === AUTHENTICATED
170167
? localUnitsResponse : publicLocalUnitsResponse;
171168
const pending = publicLocalUnitsPending || localUnitsPending;
172169

@@ -334,6 +331,16 @@ function LocalUnitsMap() {
334331
&& countryResponse.emails.length > 0;
335332
const hasContactDetails = hasAddress || hasEmails;
336333

334+
const localUnitName = useMemo(() => chooseName(
335+
localUnitDetail?.local_branch_name,
336+
localUnitDetail?.english_branch_name,
337+
), [localUnitDetail?.local_branch_name, localUnitDetail?.english_branch_name]);
338+
339+
const localUnitAddress = useMemo(() => chooseName(
340+
localUnitDetail?.address_loc,
341+
localUnitDetail?.address_en,
342+
), [localUnitDetail?.address_loc, localUnitDetail?.address_en]);
343+
337344
return (
338345
<Container
339346
className={styles.localUnitsMap}
@@ -414,9 +421,7 @@ function LocalUnitsMap() {
414421
popupClassName={styles.mapPopup}
415422
coordinates={clickedPointProperties.lngLat}
416423
onCloseButtonClick={handlePointClose}
417-
heading={isTruthyString(localUnitDetail?.english_branch_name)
418-
? localUnitDetail?.english_branch_name
419-
: localUnitDetail?.local_branch_name ?? '--'}
424+
heading={localUnitName}
420425
contentViewType="vertical"
421426
pending={localUnitDetailPending}
422427
errored={isDefined(localUnitDetailError)}
@@ -433,8 +438,7 @@ function LocalUnitsMap() {
433438
<TextOutput
434439
label={strings.localUnitDetailAddress}
435440
strongLabel
436-
value={localUnitDetail?.address_en
437-
?? localUnitDetail?.address_loc}
441+
value={localUnitAddress}
438442
/>
439443
<TextOutput
440444
label={strings.localUnitLocalUnitType}
@@ -511,48 +515,50 @@ function LocalUnitsMap() {
511515
</Container>
512516
)}
513517
</div>
514-
{isDefined(localUnitsOptions) && (
515-
<Container
516-
contentViewType="vertical"
517-
spacing="comfortable"
518-
>
519-
<Container
520-
heading={strings.localUnitLegendLocalUnitTitle}
521-
headingLevel={4}
522-
contentViewType="grid"
523-
numPreferredGridContentColumns={5}
524-
spacing="compact"
525-
>
526-
{localUnitsOptions?.type.map((legendItem) => (
527-
<LegendItem
528-
key={legendItem.id}
529-
iconSrc={legendItem.image_url}
530-
iconClassName={styles.legendIcon}
531-
color={legendItem.colour ?? COLOR_DARK_GREY}
532-
label={legendItem.name}
533-
/>
534-
))}
535-
</Container>
518+
{
519+
isDefined(localUnitsOptions) && (
536520
<Container
537-
heading={strings.localUnitLegendHealthCareTitle}
538-
headingLevel={5}
539-
contentViewType="grid"
540-
numPreferredGridContentColumns={5}
541-
spacing="compact"
521+
contentViewType="vertical"
522+
spacing="comfortable"
542523
>
543-
{localUnitsOptions?.health_facility_type.map((legendItem) => (
544-
<LegendItem
545-
key={legendItem.id}
546-
// FIXME: use color from server
547-
color={COLOR_PRIMARY_BLUE}
548-
iconSrc={legendItem.image_url}
549-
iconClassName={styles.legendIcon}
550-
label={legendItem.name}
551-
/>
552-
))}
524+
<Container
525+
heading={strings.localUnitLegendLocalUnitTitle}
526+
headingLevel={4}
527+
contentViewType="grid"
528+
numPreferredGridContentColumns={5}
529+
spacing="compact"
530+
>
531+
{localUnitsOptions?.type.map((legendItem) => (
532+
<LegendItem
533+
key={legendItem.id}
534+
iconSrc={legendItem.image_url}
535+
iconClassName={styles.legendIcon}
536+
color={legendItem.colour ?? COLOR_DARK_GREY}
537+
label={legendItem.name}
538+
/>
539+
))}
540+
</Container>
541+
<Container
542+
heading={strings.localUnitLegendHealthCareTitle}
543+
headingLevel={5}
544+
contentViewType="grid"
545+
numPreferredGridContentColumns={5}
546+
spacing="compact"
547+
>
548+
{localUnitsOptions?.health_facility_type.map((legendItem) => (
549+
<LegendItem
550+
key={legendItem.id}
551+
// FIXME: use color from server
552+
color={COLOR_PRIMARY_BLUE}
553+
iconSrc={legendItem.image_url}
554+
iconClassName={styles.legendIcon}
555+
label={legendItem.name}
556+
/>
557+
))}
558+
</Container>
553559
</Container>
554-
</Container>
555-
)}
560+
)
561+
}
556562
</Container>
557563
);
558564
}

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '@togglecorp/fujs';
1919

2020
import useFilterState from '#hooks/useFilterState';
21+
import { chooseName } from '#utils/common';
2122
import { type CountryOutletContext } from '#utils/outletContext';
2223
import {
2324
type GoApiResponse,
@@ -84,12 +85,12 @@ function LocalUnitsTable() {
8485
createStringColumn<LocalUnitsTableListItem, number>(
8586
'branch_name',
8687
strings.localUnitsTableName,
87-
(item) => item.local_branch_name ?? item.english_branch_name,
88+
(item) => chooseName(item.local_branch_name, item.english_branch_name),
8889
),
8990
createStringColumn<LocalUnitsTableListItem, number>(
9091
'address',
9192
strings.localUnitsTableAddress,
92-
(item) => item.address_loc ?? item.address_en,
93+
(item) => chooseName(item.address_loc, item.address_en),
9394
),
9495
createStringColumn<LocalUnitsTableListItem, number>(
9596
'type',
@@ -100,7 +101,7 @@ function LocalUnitsTable() {
100101
createStringColumn<LocalUnitsTableListItem, number>(
101102
'focal',
102103
strings.localUnitsTableFocal,
103-
(item) => item.focal_person_en,
104+
(item) => chooseName(item.focal_person_loc, item.focal_person_en),
104105
),
105106
createStringColumn<LocalUnitsTableListItem, number>(
106107
'phone',

0 commit comments

Comments
 (0)