Skip to content

Commit 8c92429

Browse files
samsharafrozenhelium
authored andcommitted
fix: conditionally display view/edit options based on permissions and hide contact details
1 parent 968c8df commit 8c92429

File tree

6 files changed

+139
-60
lines changed

6 files changed

+139
-60
lines changed

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/LocalUnitsForm/index.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import CountrySelectInput from '#components/domain/CountrySelectInput';
4242
import NonFieldError from '#components/NonFieldError';
4343
import { environment } from '#config';
4444
import useGlobalEnums from '#hooks/domain/useGlobalEnums';
45+
import usePermissions from '#hooks/domain/usePermissions';
4546
import useAlert from '#hooks/useAlert';
4647
import { getFirstTruthyString } from '#utils/common';
4748
import { VISIBILITY_PUBLIC } from '#utils/constants';
@@ -129,10 +130,13 @@ function LocalUnitsForm(props: Props) {
129130

130131
const alert = useAlert();
131132
const strings = useTranslation(i18n);
133+
const { isSuperUser, isCountryAdmin } = usePermissions();
132134
const formFieldsContainerRef = useRef<HTMLDivElement>(null);
133135

134136
const { api_visibility_choices: visibilityOptions } = useGlobalEnums();
135137
const { countryId } = useOutletContext<CountryOutletContext>();
138+
const hasAddEditLocalUnitPermission = isCountryAdmin(Number(countryId)) || isSuperUser;
139+
136140
const {
137141
value,
138142
error: formError,
@@ -317,7 +321,7 @@ function LocalUnitsForm(props: Props) {
317321
<div className={styles.localUnitsForm}>
318322
{readOnly && isDefined(actionsContainerRef.current) && (
319323
<Portal container={actionsContainerRef.current}>
320-
{(environment !== 'production') && (
324+
{hasAddEditLocalUnitPermission && environment !== 'production' && (
321325
<Button
322326
name={undefined}
323327
onClick={onEditButtonClick}
@@ -483,7 +487,7 @@ function LocalUnitsForm(props: Props) {
483487
error={error?.level}
484488
/>
485489
)}
486-
{value.type !== TYPE_HEALTH_CARE && (
490+
{hasAddEditLocalUnitPermission && value.type !== TYPE_HEALTH_CARE && (
487491
<>
488492
<TextInput
489493
name="focal_person_en"
@@ -705,8 +709,25 @@ function LocalUnitsForm(props: Props) {
705709
/>
706710
</>
707711
)}
708-
{value.type === TYPE_HEALTH_CARE && (
712+
{hasAddEditLocalUnitPermission && value.type === TYPE_HEALTH_CARE && (
709713
<>
714+
<TextInput
715+
name="focal_person_en"
716+
label={strings.focalPersonEn}
717+
value={value.focal_person_en}
718+
onChange={setFieldValue}
719+
readOnly={readOnly}
720+
error={error?.focal_person_en}
721+
/>
722+
<TextInput
723+
required
724+
label={strings.focalPersonLocal}
725+
name="focal_person_loc"
726+
value={value.focal_person_loc}
727+
onChange={setFieldValue}
728+
readOnly={readOnly}
729+
error={error?.focal_person_loc}
730+
/>
710731
<TextInput
711732
label={strings.focalPointPosition}
712733
name="focal_point_position"

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/i18n.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"localUnitDetailLastUpdate": "Last updated",
77
"localUnitDetailFocalPerson": "Focal person",
88
"localUnitTooltipMoreDetails": "More details",
9-
"localUnitLocalUnitType": "Local unit types",
10-
"localUnitHealthFacilityType": "Health facility types",
9+
"localUnitLocalUnitType": "Local unit type",
10+
"localUnitHealthFacilityType": "Health facility type",
1111
"presentationModeButton": "Presentation mode",
1212
"localUnitDetailEmail": "Email"
1313
}
14-
}
14+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function LocalUnitsMap(props: Props) {
135135
localUnitsOptions,
136136
} = props;
137137
const { countryResponse } = useOutletContext<CountryOutletContext>();
138-
138+
const { isAuthenticated } = useAuth();
139139
const [showLocalUnitModal, {
140140
setTrue: setShowLocalUnitViewModalTrue,
141141
setFalse: setShowLocalUnitViewModalFalse,
@@ -154,7 +154,6 @@ function LocalUnitsMap(props: Props) {
154154
[filter, countryResponse],
155155
);
156156

157-
const { isAuthenticated } = useAuth();
158157
const { isGuestUser } = usePermissions();
159158

160159
const requestType = useMemo(
@@ -448,6 +447,7 @@ function LocalUnitsMap(props: Props) {
448447
name=""
449448
variant="tertiary"
450449
onClick={handleLocalUnitHeadingClick}
450+
disabled={!isAuthenticated}
451451
>
452452
{localUnitName}
453453
</Button>
@@ -470,7 +470,7 @@ function LocalUnitsMap(props: Props) {
470470
value={localUnitAddress}
471471
/>
472472
<TextOutput
473-
label={strings.localUnitLocalUnitType}
473+
label={strings.localUnitType}
474474
strongLabel
475475
value={localUnitDetail?.type_details.name}
476476
/>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface Props {
2727
localUnitId: number;
2828
isValidated: boolean;
2929
onActionSuccess: () => void;
30+
hasAddEditLocalUnitPermission: boolean;
3031
}
3132

3233
function LocalUnitsTableActions(props: Props) {
@@ -36,6 +37,7 @@ function LocalUnitsTableActions(props: Props) {
3637
localUnitId,
3738
isValidated,
3839
onActionSuccess,
40+
hasAddEditLocalUnitPermission,
3941
} = props;
4042

4143
const { isCountryAdmin, isSuperUser } = usePermissions();
@@ -86,15 +88,14 @@ function LocalUnitsTableActions(props: Props) {
8688
type="button"
8789
name={localUnitId}
8890
onClick={handleViewLocalUnitClick}
89-
disabled={!hasValidatePermission}
9091
>
9192
{strings.localUnitsView}
9293
</DropdownMenuItem>
9394
<DropdownMenuItem
9495
type="button"
9596
name={localUnitId}
9697
onClick={handleEditLocalUnitClick}
97-
disabled={!hasValidatePermission}
98+
disabled={!hasAddEditLocalUnitPermission}
9899
>
99100
{strings.localUnitsEdit}
100101
</DropdownMenuItem>

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

Lines changed: 104 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
isNotDefined,
2020
} from '@togglecorp/fujs';
2121

22+
import usePermissions from '#hooks/domain/usePermissions';
2223
import useFilterState from '#hooks/useFilterState';
2324
import { getFirstTruthyString } from '#utils/common';
2425
import { type CountryOutletContext } from '#utils/outletContext';
@@ -52,6 +53,8 @@ function LocalUnitsTable(props: Props) {
5253

5354
const strings = useTranslation(i18n);
5455
const { countryResponse } = useOutletContext<CountryOutletContext>();
56+
const { isSuperUser, isCountryAdmin } = usePermissions();
57+
const hasAddEditLocalUnitPermission = isCountryAdmin(countryResponse?.id) || isSuperUser;
5558

5659
const {
5760
limit,
@@ -89,57 +92,111 @@ function LocalUnitsTable(props: Props) {
8992
});
9093

9194
const columns = useMemo(
92-
() => ([
93-
createStringColumn<LocalUnitsTableListItem, number>(
94-
'branch_name',
95-
strings.localUnitsTableName,
96-
(item) => getFirstTruthyString(item.local_branch_name, item.english_branch_name),
97-
),
98-
createStringColumn<LocalUnitsTableListItem, number>(
99-
'address',
100-
strings.localUnitsTableAddress,
101-
(item) => getFirstTruthyString(item.address_loc, item.address_en),
102-
),
103-
createStringColumn<LocalUnitsTableListItem, number>(
104-
'type',
105-
strings.localUnitsTableType,
106-
(item) => item.type_details.name,
107-
{ columnClassName: styles.type },
108-
),
109-
createStringColumn<LocalUnitsTableListItem, number>(
110-
'focal',
111-
strings.localUnitsTableFocal,
112-
(item) => getFirstTruthyString(item.focal_person_loc, item.focal_person_en),
113-
),
114-
createStringColumn<LocalUnitsTableListItem, number>(
115-
'phone',
116-
strings.localUnitsTablePhoneNumber,
117-
(item) => item.phone,
118-
),
119-
createStringColumn<LocalUnitsTableListItem, number>(
120-
'email',
121-
strings.localUnitsTableEmail,
122-
(item) => item.email,
123-
),
124-
createElementColumn<LocalUnitsTableListItem, number, LocalUnitsTableActionsProps>(
125-
'actions',
126-
'',
127-
LocalUnitsTableActions,
128-
// FIXME: this should be added to a callback
129-
(_, item) => ({
130-
countryId: item.country,
131-
localUnitId: item.id,
132-
isValidated: item.validated,
133-
localUnitName: getFirstTruthyString(
95+
() => {
96+
if (hasAddEditLocalUnitPermission) {
97+
return [
98+
createStringColumn<LocalUnitsTableListItem, number>(
99+
'branch_name',
100+
strings.localUnitsTableName,
101+
(item) => getFirstTruthyString(
102+
item.local_branch_name,
103+
item.english_branch_name,
104+
),
105+
),
106+
createStringColumn<LocalUnitsTableListItem, number>(
107+
'address',
108+
strings.localUnitsTableAddress,
109+
(item) => getFirstTruthyString(item.address_loc, item.address_en),
110+
),
111+
createStringColumn<LocalUnitsTableListItem, number>(
112+
'type',
113+
strings.localUnitsTableType,
114+
(item) => item.type_details.name,
115+
{ columnClassName: styles.type },
116+
),
117+
createStringColumn<LocalUnitsTableListItem, number>(
118+
'focal',
119+
strings.localUnitsTableFocal,
120+
(item) => getFirstTruthyString(
121+
item.focal_person_loc,
122+
item.focal_person_en,
123+
),
124+
),
125+
createStringColumn<LocalUnitsTableListItem, number>(
126+
'phone',
127+
strings.localUnitsTablePhoneNumber,
128+
(item) => item.phone,
129+
),
130+
createStringColumn<LocalUnitsTableListItem, number>(
131+
'email',
132+
strings.localUnitsTableEmail,
133+
(item) => item.email,
134+
),
135+
createElementColumn<
136+
LocalUnitsTableListItem,
137+
number,
138+
LocalUnitsTableActionsProps
139+
>(
140+
'actions',
141+
'',
142+
LocalUnitsTableActions,
143+
// FIXME: this should be added to a callback
144+
(_, item) => ({
145+
countryId: item.country,
146+
localUnitId: item.id,
147+
isValidated: item.validated,
148+
localUnitName: getFirstTruthyString(
149+
item.local_branch_name,
150+
item.english_branch_name,
151+
),
152+
onActionSuccess: refetchLocalUnits,
153+
}),
154+
{ columnClassName: styles.actions },
155+
),
156+
];
157+
}
158+
return [
159+
createStringColumn<LocalUnitsTableListItem, number>(
160+
'branch_name',
161+
strings.localUnitsTableName,
162+
(item) => getFirstTruthyString(
134163
item.local_branch_name,
135164
item.english_branch_name,
136165
),
137-
onActionSuccess: refetchLocalUnits,
138-
}),
139-
{ columnClassName: styles.actions },
140-
),
141-
]),
166+
),
167+
createStringColumn<LocalUnitsTableListItem, number>(
168+
'address',
169+
strings.localUnitsTableAddress,
170+
(item) => getFirstTruthyString(item.address_loc, item.address_en),
171+
),
172+
createStringColumn<LocalUnitsTableListItem, number>(
173+
'type',
174+
strings.localUnitsTableType,
175+
(item) => item.type_details.name,
176+
{ columnClassName: styles.type },
177+
),
178+
createElementColumn<LocalUnitsTableListItem, number, LocalUnitsTableActionsProps>(
179+
'actions',
180+
'',
181+
LocalUnitsTableActions,
182+
// FIXME: this should be added to a callback
183+
(_, item) => ({
184+
countryId: item.country,
185+
localUnitId: item.id,
186+
isValidated: item.validated,
187+
localUnitName: getFirstTruthyString(
188+
item.local_branch_name,
189+
item.english_branch_name,
190+
),
191+
onActionSuccess: refetchLocalUnits,
192+
hasAddEditLocalUnitPermission,
193+
}),
194+
{ columnClassName: styles.actions },
195+
),
196+
];
197+
},
142198
[
199+
hasAddEditLocalUnitPermission,
143200
strings.localUnitsTableAddress,
144201
strings.localUnitsTableName,
145202
strings.localUnitsTableType,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function NationalSocietyLocalUnits(props: Props) {
111111

112112
const strings = useTranslation(i18n);
113113

114-
const hasAddLocalUnitPermission = isCountryAdmin(countryResponse?.id) || isSuperUser;
114+
const hasAddEditLocalUnitPermission = isCountryAdmin(countryResponse?.id) || isSuperUser;
115115

116116
useEffect(() => {
117117
document.addEventListener('fullscreenchange', handleFullScreenChange);
@@ -148,7 +148,7 @@ function NationalSocietyLocalUnits(props: Props) {
148148
/>
149149
)}
150150
// NOTE: disable local units add/edit for now
151-
actions={hasAddLocalUnitPermission && (environment !== 'production') && (
151+
actions={hasAddEditLocalUnitPermission && (environment !== 'production') && (
152152
<Button
153153
name={undefined}
154154
variant="secondary"

0 commit comments

Comments
 (0)