Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/hooks/domain/usePermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function usePermissions() {
&& ((userMe?.is_admin_for_countries.length ?? 0) > 0
|| (userMe?.is_admin_for_regions.length ?? 0) > 0);

const isGlobalValidator = !isGuestUser && !!userMe?.is_global_validator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this role generic or specific to local units?

Also I feel like this should be permission instead of role @thenav56 @tnagorra @samshara

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frozenhelium this is specific to local units.


return {
isDrefRegionalCoordinator,
isRegionAdmin,
Expand All @@ -60,6 +62,7 @@ function usePermissions() {
isSuperUser,
isGuestUser,
isRegionalOrCountryAdmin,
isGlobalValidator,
};
},
[userMe],
Expand Down
89 changes: 89 additions & 0 deletions app/src/utils/localUnits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
isNotDefined,
isObject,
} from '@togglecorp/fujs';
import { removeNull } from '@togglecorp/toggle-form';

import { type PartialLocalUnits } from '#views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsFormModal/LocalUnitsForm/schema';

import { type GoApiResponse } from './restRequest';

type LocalUnitResponse = NonNullable<GoApiResponse<'/api/v2/local-units/{id}/'>>;

export function getFormFields(value: LocalUnitResponse | PartialLocalUnits) {
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_at,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_by_details,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_at,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by_details,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
is_locked,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
validated,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
version_id,
health,
...formValues
// Note: the cast is safe as we're only trying to
// remove fields if they exist
} = removeNull(value) as LocalUnitResponse;

const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by_details: healthModifiedByDetails,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_at: healthModifiedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by: healthModifiedby,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_at: healthCreatedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_by_details: healthCreatedByDetails,
...formHealthValues
} = health ?? {};
Comment on lines +31 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
health,
...formValues
// Note: the cast is safe as we're only trying to
// remove fields if they exist
} = removeNull(value) as LocalUnitResponse;
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by_details: healthModifiedByDetails,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_at: healthModifiedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by: healthModifiedby,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_at: healthCreatedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_by_details: healthCreatedByDetails,
...formHealthValues
} = health ?? {};
health = {},
...formValues
// Note: the cast is safe as we're only trying to
// remove fields if they exist
} = removeNull(value) as LocalUnitResponse;
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by_details: healthModifiedByDetails,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_at: healthModifiedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modified_by: healthModifiedby,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_at: healthCreatedAt,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
created_by_details: healthCreatedByDetails,
...formHealthValues
} = health;


return { ...formValues, health: { ...formHealthValues } };
}

// FIXME: this should be gracefully handled
function isObjectWithStringKey(obj: unknown): obj is Record<string, unknown> {
return isObject(obj);
}

export default function hasDifferences(newValue: unknown, oldValue: unknown): boolean {
if (isNotDefined(newValue) && isNotDefined(oldValue)) {
return false;
}

// FIXME: we might need to also consider the order for array
if (Array.isArray(newValue) && Array.isArray(oldValue)) {
if (newValue.length !== oldValue.length) {
return true;
}

return newValue.some(
(_, i) => hasDifferences(newValue[i], oldValue[i]),
);
}

if (isObjectWithStringKey(newValue) && isObjectWithStringKey(oldValue)) {
const newValueKeys = Object.keys(removeNull(newValue));
const oldValueKeys = Object.keys(removeNull(oldValue));

if (newValueKeys.length !== oldValueKeys.length) {
return true;
}
Comment on lines +76 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets verify removeNull might not actually remove null, lets verify it

Alternatively we can check length for list of defined values only


return newValueKeys.some(
(key) => hasDifferences(newValue[key], oldValue[key]),
);
}

return newValue !== oldValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"localUnitViewConfirmChangesContentQuestion": "Are you sure you want to have these changes in this local unit?",
"localUnitViewNewLocalUnitDescription": "New local unit",
"localUnitViewLatitude": "Latitude",
"localUnitViewLongitude": "Longitude"
"localUnitViewLongitude": "Longitude",
"localUnitViewNoChanges": "No changes found"
}
}
Loading
Loading