();
+
+ const hasValidatePermission = isSuperUser
+ || isCountryAdmin(Number(countryId))
+ || isRegionAdmin(Number(countryResponse?.region));
+
+ const hasDeletePermission = isAuthenticated && !isGuestUser;
+
const {
value,
error: formError,
@@ -388,28 +419,28 @@ function LocalUnitsForm(props: Props) {
&& (environment !== 'production')
&& (
-
-
+ {hasDeletePermission && (
+
+ )}
+ {hasValidatePermission && (
+
+ )}
)}
@@ -1055,6 +1086,17 @@ function LocalUnitsForm(props: Props) {
>
)}
+ {showDeleteLocalUnitModal && isDefined(localUnitId) && (
+
+ )}
);
}
diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/index.tsx
index 8392238385..af6ce1c448 100644
--- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/index.tsx
+++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/index.tsx
@@ -16,6 +16,7 @@ interface Props {
readOnly?: boolean;
setReadOnly?: React.Dispatch>;
onClose: (requestDone?: boolean) => void;
+ onDeleteActionSuccess?: () => void;
}
function LocalUnitsFormModal(props: Props) {
@@ -24,6 +25,7 @@ function LocalUnitsFormModal(props: Props) {
localUnitId,
readOnly,
setReadOnly,
+ onDeleteActionSuccess,
} = props;
const strings = useTranslation(i18n);
@@ -74,6 +76,7 @@ function LocalUnitsFormModal(props: Props) {
actionsContainerRef={actionsContainerRef}
headingDescriptionRef={headingDescriptionRef}
headerDescriptionRef={headerDescriptionRef}
+ onDeleteActionSuccess={onDeleteActionSuccess}
/>
);
diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/index.tsx
index eb58cc4956..4a2442f162 100644
--- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/index.tsx
+++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsMap/index.tsx
@@ -184,6 +184,7 @@ function LocalUnitsMap(props: Props) {
const {
response: localUnitsResponse,
pending: localUnitsPending,
+ retrigger: refetchLocalUnits,
} = useRequest({
skip: requestType !== AUTHENTICATED || isNotDefined(countryResponse),
url: '/api/v2/local-units/',
@@ -583,6 +584,7 @@ function LocalUnitsMap(props: Props) {
localUnitId={clickedPointProperties?.localUnitId}
readOnly={readOnlyLocalUnitModal}
setReadOnly={setReadOnlyLocalUnitModal}
+ onDeleteActionSuccess={refetchLocalUnits}
/>
))}
diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx
index cec03b2d7d..075a43ff98 100644
--- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx
+++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx
@@ -13,9 +13,11 @@ import {
import DropdownMenuItem from '#components/DropdownMenuItem';
import { environment } from '#config';
+import useAuth from '#hooks/domain/useAuth';
+import useCountry from '#hooks/domain/useCountry';
import usePermissions from '#hooks/domain/usePermissions';
-import LocalUnitDeleteButton from '../../LocalUnitDeleteButton';
+import LocalUnitDeleteModal from '../../LocalUnitDeleteModal';
import LocalUnitsFormModal from '../../LocalUnitsFormModal';
import LocalUnitValidateButton from '../../LocalUnitValidateButton';
@@ -23,10 +25,11 @@ import i18n from './i18n.json';
export interface Props {
countryId: number;
- localUnitName: string | null | undefined;
+ localUnitName: string;
localUnitId: number;
isValidated: boolean;
- onActionSuccess: () => void;
+ onDeleteActionSuccess: () => void;
+ onValidationActionSuccess: () => void;
}
function LocalUnitsTableActions(props: Props) {
@@ -35,30 +38,56 @@ function LocalUnitsTableActions(props: Props) {
localUnitName,
localUnitId,
isValidated,
- onActionSuccess,
+ onValidationActionSuccess,
+ onDeleteActionSuccess,
} = props;
- const { isCountryAdmin, isSuperUser } = usePermissions();
const strings = useTranslation(i18n);
- const hasValidatePermission = isSuperUser || isCountryAdmin(countryId);
+ const countryDetails = useCountry({ id: Number(countryId) });
+
+ const {
+ isSuperUser,
+ isRegionAdmin,
+ isCountryAdmin,
+ isGuestUser,
+ } = usePermissions();
+
+ const { isAuthenticated } = useAuth();
+
+ const hasValidatePermission = isSuperUser
+ || isCountryAdmin(Number(countryId))
+ || isRegionAdmin(Number(countryDetails?.region));
+
+ const hasDeletePermission = isAuthenticated && !isGuestUser;
const [readOnlyLocalUnitModal, setReadOnlyLocalUnitModal] = useState(false);
- const [showLocalUnitModal, {
- setTrue: setShowLocalUnitModalTrue,
- setFalse: setShowLocalUnitModalFalse,
- }] = useBooleanState(false);
+ const [
+ showLocalUnitModal,
+ {
+ setTrue: setShowLocalUnitModalTrue,
+ setFalse: setShowLocalUnitModalFalse,
+ },
+ ] = useBooleanState(false);
+
+ const [
+ showDeleteLocalUnitModal,
+ {
+ setTrue: setShowDeleteLocalUnitModalTrue,
+ setFalse: setShowDeleteLocalUnitModalFalse,
+ },
+ ] = useBooleanState(false);
const handleLocalUnitsFormModalClose = useCallback(
(shouldUpdate?: boolean) => {
setShowLocalUnitModalFalse();
if (shouldUpdate) {
- onActionSuccess();
+ onDeleteActionSuccess();
}
},
- [setShowLocalUnitModalFalse, onActionSuccess],
+ [setShowLocalUnitModalFalse, onDeleteActionSuccess],
);
const handleViewLocalUnitClick = useCallback(
@@ -68,6 +97,7 @@ function LocalUnitsTableActions(props: Props) {
},
[setShowLocalUnitModalTrue],
);
+
const handleEditLocalUnitClick = useCallback(
() => {
setReadOnlyLocalUnitModal(false);
@@ -86,7 +116,7 @@ function LocalUnitsTableActions(props: Props) {
type="button"
name={localUnitId}
onClick={handleViewLocalUnitClick}
- disabled={!hasValidatePermission}
+ disabled={isGuestUser}
>
{strings.localUnitsView}
@@ -94,26 +124,28 @@ function LocalUnitsTableActions(props: Props) {
type="button"
name={localUnitId}
onClick={handleEditLocalUnitClick}
- disabled={!hasValidatePermission}
+ disabled={isGuestUser}
>
{strings.localUnitsEdit}
-
+ {hasDeletePermission && (
+
+ {strings.localUnitsDelete}
+
+ )}
>
)}
>
- {environment !== 'production' ? (
+ {hasValidatePermission && environment !== 'production' ? (
) : (
@@ -121,7 +153,6 @@ function LocalUnitsTableActions(props: Props) {
name={localUnitId}
variant="tertiary"
onClick={handleViewLocalUnitClick}
- disabled={!hasValidatePermission}
>
{strings.localUnitsView}
@@ -133,6 +164,15 @@ function LocalUnitsTableActions(props: Props) {
localUnitId={localUnitId}
readOnly={readOnlyLocalUnitModal}
setReadOnly={setReadOnlyLocalUnitModal}
+ onDeleteActionSuccess={onDeleteActionSuccess}
+ />
+ )}
+ {showDeleteLocalUnitModal && (
+
)}
>
diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/index.tsx
index 9886f24e6b..faedefdb61 100644
--- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/index.tsx
+++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/index.tsx
@@ -134,7 +134,8 @@ function LocalUnitsTable(props: Props) {
item.local_branch_name,
item.english_branch_name,
),
- onActionSuccess: refetchLocalUnits,
+ onDeleteActionSuccess: refetchLocalUnits,
+ onValidationActionSuccess: refetchLocalUnits,
}),
{ columnClassName: styles.actions },
),