Skip to content

Commit 2b357e3

Browse files
committed
Validate endpoint with super user and country admin
1 parent f3c397a commit 2b357e3

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import react from 'react';
1+
import react, { useMemo } from 'react';
22
import { useOutletContext } from 'react-router-dom';
33
import {
44
LocationIcon,
@@ -14,7 +14,6 @@ import { useTranslation } from '@ifrc-go/ui/hooks';
1414
import { sumSafe } from '@ifrc-go/ui/utils';
1515
import {
1616
isDefined,
17-
isNotDefined,
1817
isTruthyString,
1918
} from '@togglecorp/fujs';
2019
import {
@@ -34,7 +33,7 @@ import BaseMap from '#components/domain/BaseMap';
3433
import Link, { type Props as LinkProps } from '#components/Link';
3534
import MapContainerWithDisclaimer from '#components/MapContainerWithDisclaimer';
3635
import MapPopup from '#components/MapPopup';
37-
import useUserMe from '#hooks/domain/useUserMe';
36+
import usePermissions from '#hooks/domain/usePermissions';
3837
import useFilterState from '#hooks/useFilterState';
3938
import {
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';
5356
import Filters, { FilterValue } from '../Filters';
5457

5558
import 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

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function LocalUnitsTableActions(props: Props) {
3838
const alert = useAlert();
3939

4040
const hasValidatePermission = isSuperUser || isCountryAdmin(countryId);
41+
4142
const {
4243
pending: validateLocalUnitPending,
4344
trigger: validateLocalUnit,
@@ -82,24 +83,26 @@ function LocalUnitsTableActions(props: Props) {
8283
>
8384
{strings.localUnitsView}
8485
</DropdownMenuItem>
85-
{!(isValidated) && hasValidatePermission && (
86-
<DropdownMenuItem
87-
persist
88-
name={undefined}
89-
type="confirm-button"
90-
variant="tertiary"
91-
className={styles.button}
92-
confirmHeading={strings.validateLocalUnitHeading}
93-
confirmMessage={resolveToString(
94-
strings.validateLocalUnitMessage,
95-
{ localUnitName: localUnitName ?? '' },
96-
)}
97-
onConfirm={handleLocalUnitValidate}
98-
disabled={validateLocalUnitPending}
99-
>
100-
{strings.localUnitsValidate}
101-
</DropdownMenuItem>
102-
)}
86+
<DropdownMenuItem
87+
persist
88+
name={undefined}
89+
type="confirm-button"
90+
variant="tertiary"
91+
className={styles.button}
92+
confirmHeading={strings.validateLocalUnitHeading}
93+
confirmMessage={resolveToString(
94+
strings.validateLocalUnitMessage,
95+
{ localUnitName: localUnitName ?? '' },
96+
)}
97+
onConfirm={handleLocalUnitValidate}
98+
disabled={
99+
validateLocalUnitPending
100+
|| !hasValidatePermission
101+
|| isValidated
102+
}
103+
>
104+
{strings.localUnitsValidate}
105+
</DropdownMenuItem>
103106
</>
104107
)}
105108
/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function LocalUnitsTable() {
122122
'',
123123
LocalUnitsTableActions,
124124
(_, item) => ({
125-
countryId: item.id,
125+
countryId: item.country,
126126
localUnitId: item.id,
127127
isValidated: item.validated,
128128
localUnitName: item.local_branch_name ?? item.english_branch_name,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export type Validation = 'Validated' | 'Not Validated';
2+
export type RequestType = 'authenticated' | 'public';
23

34
export const VALIDATED = 'Validated' satisfies Validation;
45
export const NOT_VALIDATED = 'Not Validated' satisfies Validation;
6+
export const AUTHENTICATED = 'authenticated' satisfies RequestType;
7+
export const PUBLIC = 'public' satisfies RequestType;

0 commit comments

Comments
 (0)