Skip to content

Commit e7804e9

Browse files
shreeyash07frozenhelium
authored andcommitted
fix(local-unit): update add and edit permission for local unit form
1 parent 2154678 commit e7804e9

File tree

10 files changed

+48
-32
lines changed

10 files changed

+48
-32
lines changed

app/src/hooks/domain/usePermissions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function usePermissions() {
2020
&& isDefined(countryId)
2121
&& !!userMe?.is_admin_for_countries?.includes(countryId)
2222
);
23-
const isRegionAdmin = (regionId: number | undefined) => (
23+
const isRegionAdmin = (regionId: number | null | undefined) => (
2424
!isGuestUser
2525
&& isDefined(regionId)
2626
&& !!userMe?.is_admin_for_regions?.includes(regionId)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ function ConfirmationModal(props: Props) {
111111
const handleConfirmButtonChange = useCallback(() => {
112112
if (isNewManageLocalUnit) {
113113
addManageLocalUnit(manageLocalUnitsValues);
114+
} else {
115+
updateManageLocalUnit(manageLocalUnitsValues);
114116
}
115-
updateManageLocalUnit(manageLocalUnitsValues);
116117
}, [isNewManageLocalUnit,
117118
addManageLocalUnit, manageLocalUnitsValues, updateManageLocalUnit]);
118119

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,13 @@ function ConfigureLocalUnitsModal(props: Props) {
6767

6868
const handleLocalUnitSwitchChange = useCallback((value: boolean, name: number) => {
6969
setLocalUnitType(name);
70-
if (isNotDefined(manageResponse)
71-
&& isDefined(countryResponse)
72-
) {
73-
setManageLocalUnitsValues({
74-
id: undefined,
75-
country: countryResponse.id,
76-
local_unit_type: name,
77-
enabled: value,
78-
});
79-
}
80-
if (isDefined(manageResponse)
81-
&& isDefined(manageResponse[name])
82-
&& isDefined(countryResponse)
83-
) {
70+
71+
if (isDefined(countryResponse)) {
72+
const isNew = isNotDefined(manageResponse) || isNotDefined(manageResponse[name]);
73+
const manageId = isNew ? undefined : manageResponse[name]?.externallyManagedId;
74+
8475
setManageLocalUnitsValues({
85-
id: manageResponse[name].externallyManagedId,
76+
id: manageId,
8677
country: countryResponse.id,
8778
local_unit_type: name,
8879
enabled: value,
@@ -97,8 +88,8 @@ function ConfigureLocalUnitsModal(props: Props) {
9788
]);
9889

9990
const isNewManageLocalUnit = useMemo(() => {
100-
if (isDefined(manageLocalUnitsValues)
101-
&& isDefined(manageLocalUnitsValues.id)) {
91+
if (isNotDefined(manageLocalUnitsValues)
92+
|| isNotDefined(manageLocalUnitsValues?.id)) {
10293
return true;
10394
}
10495
return false;

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitView/OtherProfilesDiffOutput/i18n.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"namespace": "countryNsOverviewContextAndStructure",
33
"strings": {
4-
"otherProfileOutputLabel": "Other Profiles",
54
"otherProfilePositionOutputLabel": "Position",
65
"otherProfileNumberOutputLabel": "Number"
76
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"localUnitViewDentist": "Dentist",
5656
"localUnitViewNursingAid": "Nursing aid",
5757
"localUnitViewMidwife": "Midwife",
58+
"localUnitViewPharmacists": "Pharmacists",
5859
"localUnitViewOtherProfiles": "Other profiles",
5960
"localUnitViewRemovedOtherProfiles": "Removed Other profiles",
6061
"localUnitViewOtherMedicalHeal": "Other medical heal",

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,18 @@ function LocalUnitView(props: Props) {
892892
value={newValue?.health?.midwife}
893893
/>
894894
</DiffWrapper>
895+
<DiffWrapper
896+
hideOnPristine
897+
value={newValue?.health?.pharmacists}
898+
previousValue={oldValue?.health?.pharmacists}
899+
diffViewEnabled
900+
>
901+
<TextOutput
902+
strongValue
903+
label={strings.localUnitViewPharmacists}
904+
value={newValue?.health?.pharmacists}
905+
/>
906+
</DiffWrapper>
895907
{isDefined(changedOtherProfiles) && changedOtherProfiles.length > 0 && (
896908
<>
897909
<span>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function LocalUnitsForm(props: Props) {
155155
const {
156156
isSuperUser,
157157
isCountryAdmin,
158+
isRegionAdmin,
158159
isLocalUnitGlobalValidatorByType,
159160
isLocalUnitRegionValidatorByType,
160161
isLocalUnitCountryValidatorByType,
@@ -388,7 +389,12 @@ function LocalUnitsForm(props: Props) {
388389
|| isLocalUnitRegionValidatorByType(countryResponse?.region, value.type)
389390
);
390391

391-
const hasUpdatePermission = isCountryAdmin(countryResponse?.id) || hasValidatePermission;
392+
const hasUpdatePermission = isCountryAdmin(countryResponse?.id)
393+
|| isRegionAdmin(countryResponse?.region)
394+
|| hasValidatePermission;
395+
396+
const hasDeletePermission = isCountryAdmin(countryResponse?.id)
397+
|| hasValidatePermission;
392398

393399
const handleFormSubmit = useCallback(
394400
() => {
@@ -537,7 +543,7 @@ function LocalUnitsForm(props: Props) {
537543
<Portal container={actionsContainerRef.current}>
538544
{isDefined(localUnitDetailsResponse) && (
539545
<>
540-
{hasUpdatePermission && (
546+
{hasDeletePermission && (
541547
<Button
542548
name={undefined}
543549
onClick={setShowDeleteLocalUnitModalTrue}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,30 @@ function LocalUnitsTableActions(props: Props) {
6464
isLocalUnitRegionValidatorByType,
6565
isSuperUser,
6666
isGuestUser,
67+
isCountryAdmin,
68+
isRegionAdmin,
6769
} = usePermissions();
6870

6971
const isLocked = status !== VALIDATED;
7072

73+
const countryAdmin = isCountryAdmin(countryDetails?.id);
74+
const regionAdmin = isRegionAdmin(countryDetails?.region);
75+
7176
const isExternallyManaged = status === EXTERNALLY_MANAGED
7277
|| (isDefined(localUnitType)
7378
&& isDefined(manageResponse)
7479
&& !!manageResponse[localUnitType]?.enabled);
7580

76-
const hasPermission = isAuthenticated
81+
const hasValidatePermission = isAuthenticated
7782
&& !isExternallyManaged
7883
&& (isSuperUser
7984
|| isLocalUnitGlobalValidatorByType(localUnitType)
8085
|| isLocalUnitCountryValidatorByType(countryDetails?.id, localUnitType)
8186
|| isLocalUnitRegionValidatorByType(countryDetails?.region, localUnitType));
8287

83-
const hasValidatePermission = isAuthenticated
84-
&& (isSuperUser
85-
|| isLocalUnitGlobalValidatorByType(localUnitType)
86-
|| isLocalUnitCountryValidatorByType(countryDetails?.id, localUnitType)
87-
|| isLocalUnitRegionValidatorByType(countryDetails?.region, localUnitType));
88+
const hasAddEditLocalUnitPermission = !isLocked && (
89+
(hasValidatePermission || countryAdmin || regionAdmin)
90+
&& !isBulkUploadLocalUnit);
8891

8992
const [readOnlyLocalUnitModal, setReadOnlyLocalUnitModal] = useState(false);
9093

@@ -165,7 +168,8 @@ function LocalUnitsTableActions(props: Props) {
165168
>
166169
{strings.localUnitActionsView}
167170
</DropdownMenuItem>
168-
{(hasPermission && !isBulkUploadLocalUnit) && (
171+
{((hasValidatePermission || countryAdmin)
172+
&& !isBulkUploadLocalUnit) && (
169173
<DropdownMenuItem
170174
type="button"
171175
name={undefined}
@@ -174,7 +178,7 @@ function LocalUnitsTableActions(props: Props) {
174178
{strings.localUnitActionsDelete}
175179
</DropdownMenuItem>
176180
)}
177-
{!isLocked && (hasPermission && !isBulkUploadLocalUnit) && (
181+
{hasAddEditLocalUnitPermission && (
178182
<DropdownMenuItem
179183
type="button"
180184
name={localUnitId}
@@ -190,7 +194,7 @@ function LocalUnitsTableActions(props: Props) {
190194
<LocalUnitValidateButton
191195
onClick={handleValidateLocalUnitClick}
192196
status={status}
193-
hasValidatePermission={hasPermission}
197+
hasValidatePermission={hasValidatePermission}
194198
/>
195199
)}
196200
</TableActions>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function LocalUnitsTable(props: Props) {
5858
const {
5959
isSuperUser,
6060
isCountryAdmin,
61+
isRegionAdmin,
6162
isLocalUnitGlobalValidator,
6263
isLocalUnitRegionValidator,
6364
isLocalUnitCountryValidator,
@@ -71,6 +72,7 @@ function LocalUnitsTable(props: Props) {
7172
|| isLocalUnitRegionValidator(countryResponse?.region ?? undefined);
7273

7374
const hasAddEditLocalUnitPermission = isCountryAdmin(countryResponse?.id)
75+
|| isRegionAdmin(countryResponse?.region)
7476
|| hasPermission;
7577

7678
const {

0 commit comments

Comments
 (0)