Skip to content

Commit 51b1ad2

Browse files
committed
fix: filter out national societies based on per permissions
1 parent 2dd1574 commit 51b1ad2

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/App/routes/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ interface Perms {
1919
isDrefRegionalCoordinator: (regionId: number | undefined) => boolean,
2020
isRegionAdmin: (regionId: number | undefined) => boolean,
2121
isCountryAdmin: (countryId: number | undefined) => boolean,
22+
isRegionPerAdmin: (regionId: number | undefined) => boolean,
23+
isCountryPerAdmin: (countryId: number | undefined) => boolean,
24+
isPerAdmin: boolean,
2225
isIfrcAdmin: boolean,
2326
isSuperUser: boolean,
2427
}
@@ -2537,6 +2540,10 @@ const newPerOverviewForm = customWrapRoute({
25372540
context: {
25382541
title: 'New PER Overview',
25392542
visibility: 'is-authenticated',
2543+
permissions: ({
2544+
isSuperUser,
2545+
isPerAdmin,
2546+
}) => isSuperUser || isPerAdmin,
25402547
},
25412548
});
25422549

src/components/domain/NationalSocietySelectInput.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Props as SelectInputProps } from '#components/SelectInput';
22
import SelectInput from '#components/SelectInput';
33
import { numericIdSelector } from '#utils/selectors';
44
import useNationalSociety, { NationalSociety } from '#hooks/domain/useNationalSociety';
5+
import { isDefined } from '@togglecorp/fujs';
56

67
function countrySocietyNameSelector(country: NationalSociety) {
78
return country.society_name;
@@ -19,6 +20,8 @@ type Props<NAME> = SelectInputProps<
1920
name: NAME;
2021
onChange: (newValue: number | undefined, name: NAME) => void;
2122
value: number | undefined | null;
23+
regions?: number[];
24+
countries?: number[];
2225
}
2326

2427
function NationalSocietySelectInput<const NAME>(props: Props<NAME>) {
@@ -27,18 +30,30 @@ function NationalSocietySelectInput<const NAME>(props: Props<NAME>) {
2730
name,
2831
onChange,
2932
value,
33+
regions,
34+
countries,
3035
...otherProps
3136
} = props;
3237

38+
console.warn('regions', regions, countries);
3339
const nationalSocieties = useNationalSociety();
40+
let options: NationalSociety[] = nationalSocieties;
41+
42+
if (isDefined(regions) || isDefined(countries)) {
43+
options = nationalSocieties
44+
?.filter((nationalSociety) => (
45+
(isDefined(nationalSociety.region) && (regions?.includes(nationalSociety.region)))
46+
|| countries?.includes(nationalSociety.id)
47+
));
48+
}
3449

3550
return (
3651
<SelectInput
3752
// eslint-disable-next-line react/jsx-props-no-spreading
3853
{...otherProps}
3954
className={className}
4055
name={name}
41-
options={nationalSocieties}
56+
options={options}
4257
keySelector={numericIdSelector}
4358
labelSelector={countrySocietyNameSelector}
4459
value={value}

src/hooks/domain/usePermissions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@ function usePermissions() {
1717
const isRegionAdmin = (regionId: number | undefined) => (
1818
isDefined(regionId) && !!userMe?.is_admin_for_regions?.includes(regionId)
1919
);
20+
const isRegionPerAdmin = (regionId: number | undefined) => (
21+
isDefined(regionId) && !!userMe?.is_per_admin_for_regions.includes(regionId)
22+
);
23+
const isCountryPerAdmin = (countryId: number | undefined) => (
24+
isDefined(countryId) && !!userMe?.is_per_admin_for_countries.includes(countryId)
25+
);
26+
27+
const isPerAdmin = (userMe?.is_per_admin_for_countries.length ?? 0) > 0
28+
|| (userMe?.is_admin_for_regions.length ?? 0) > 0;
29+
2030
return {
2131
isDrefRegionalCoordinator,
2232
isRegionAdmin,
2333
isCountryAdmin,
34+
isRegionPerAdmin,
35+
isCountryPerAdmin,
36+
isPerAdmin,
2437
isIfrcAdmin: !!userMe?.is_ifrc_admin || !!userMe?.email?.toLowerCase().endsWith('@ifrc.org'),
2538
isSuperUser: !!userMe?.is_superuser,
2639
};

src/views/PerOverviewForm/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import useTranslation from '#hooks/useTranslation';
3636
import useAlertContext from '#hooks/useAlert';
3737
import { useLazyRequest, useRequest, type GoApiResponse } from '#utils/restRequest';
3838
import useGlobalEnums from '#hooks/domain/useGlobalEnums';
39+
import useUserMe from '#hooks/domain/useUserMe';
3940
import { PER_PHASE_OVERVIEW, PER_PHASE_ASSESSMENT } from '#utils/domain/per';
4041
import type { PerProcessOutletContext } from '#utils/outletContext';
4142
import {
@@ -73,6 +74,7 @@ export function Component() {
7374
refetchStatusResponse,
7475
} = useOutletContext<PerProcessOutletContext>();
7576
const { per_overviewassessmentmethods } = useGlobalEnums();
77+
const userMe = useUserMe();
7678

7779
const formContentRef = useRef<ElementRef<'div'>>(null);
7880
const isSettingUpProcess = useRef(false);
@@ -407,6 +409,10 @@ export function Component() {
407409
error={getErrorString(error?.country)}
408410
readOnly={partialReadonlyMode}
409411
disabled={disabled}
412+
regions={!userMe?.is_superuser
413+
? userMe?.is_per_admin_for_regions : undefined}
414+
countries={!userMe?.is_superuser
415+
? userMe?.is_per_admin_for_countries : undefined}
410416
autoFocus
411417
/>
412418
</InputSection>

0 commit comments

Comments
 (0)