Skip to content

Commit

Permalink
Merge pull request #1254 from BLSQ/POLIO-1102-scope-valid-org-unit
Browse files Browse the repository at this point in the history
POLIO-1102: scope only valid org units
  • Loading branch information
beygorghor authored May 6, 2024
2 parents ae16aad + 3456aae commit e396d99
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
61 changes: 30 additions & 31 deletions plugins/polio/js/src/domains/Campaigns/Scope/ScopeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,43 @@ export const ScopeForm: FunctionComponent = () => {
}, [currentTab, rounds, scopePerRound, sortedRounds, values.scopes]);

const filteredDistricts: FilteredDistricts[] | undefined = useMemo(() => {
if (districtShapes && regionShapes) {
let filtered: FilteredDistricts[] = districtShapes.map(district => {
if (!districtShapes || !regionShapes) return undefined;

const orgUnitIdToVaccine = new Map();
if (isPolio && scopes) {
scopes.forEach(scope => {
scope.group.org_units.forEach(ouId => {
orgUnitIdToVaccine.set(ouId, scope.vaccine);
});
});
}
const filtered = districtShapes
.filter(district => {
const isInScope = scopes.some(sc =>
sc.group.org_units.includes(district.id),
);
return (
(district.validation_status === 'VALID' || isInScope) &&
(!searchScope || isInScope) &&
(!debouncedSearch ||
district.name
.toLowerCase()
.includes(debouncedSearch.toLowerCase()))
);
})
.map(district => {
const scope = findScopeWithOrgUnit(scopes, district.id);
const vaccineName =
orgUnitIdToVaccine.get(district.id) || undefined;
return {
...cloneDeep(district),
region: findRegion(district, regionShapes),
scope,
vaccineName: scope?.vaccine,
vaccineName,
};
}) as FilteredDistricts[];
if (scopes && isPolio) {
filtered.forEach((d, index) => {
scopes.forEach(scope => {
scope.group.org_units.forEach(ouId => {
if (d.id === ouId) {
filtered[index].vaccineName = scope.vaccine;
}
});
});
});
}
});

if (searchScope) {
filtered = filtered.filter(d =>
scopes.some(scope => scope.group.org_units.includes(d.id)),
);
}

if (debouncedSearch !== '') {
filtered = filtered.filter(d =>
d.name
.toLowerCase()
.includes(debouncedSearch.toLowerCase()),
);
}
return filtered;
}
return undefined;
return filtered;
}, [
districtShapes,
regionShapes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Props = {
isFetching: boolean;
districtShapes: OrgUnit[];
selectedVaccine: string;
isPolio: boolean;
isPolio?: boolean;
};

export const DistrictScopeTable: FunctionComponent<Props> = ({
Expand Down
30 changes: 16 additions & 14 deletions plugins/polio/js/src/domains/Campaigns/Scope/Scopes/MapScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,26 @@ export const MapScope: FunctionComponent<Props> = ({
},
[values.org_unit?.id, scopes, theme],
);
const filterOrgUnits = useCallback(
(orgUnits: OrgUnit[]) =>
orgUnits.filter(
orgUnit =>
(orgUnit.has_geo_json ||
(orgUnit.latitude && orgUnit.longitude)) &&
(orgUnit.validation_status === 'VALID' ||
// display REJECTED or NEW org unit if already present in a scope
findScopeWithOrgUnit(scopes, orgUnit.id)),
),
[scopes],
);

const districts = useMemo(
() =>
districtShapes.filter(
ogrUnit =>
ogrUnit.has_geo_json ||
(ogrUnit.latitude && ogrUnit.longitude),
),
[districtShapes],
() => filterOrgUnits(districtShapes),
[districtShapes, filterOrgUnits],
);
const regions = useMemo(
() =>
regionShapes.filter(
ogrUnit =>
ogrUnit.has_geo_json ||
(ogrUnit.latitude && ogrUnit.longitude),
),
[regionShapes],
() => filterOrgUnits(regionShapes),
[regionShapes, filterOrgUnits],
);

return (
Expand Down
6 changes: 5 additions & 1 deletion plugins/polio/js/src/domains/Campaigns/Scope/Scopes/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable camelcase */
import { OrgUnit } from '../../../../../../../../hat/assets/js/apps/Iaso/domains/orgUnits/types/orgUnit';
import {
OrgUnit,
OrgUnitStatus,
} from '../../../../../../../../hat/assets/js/apps/Iaso/domains/orgUnits/types/orgUnit';
import { Scope } from '../../../../constants/types';

export type Shape = OrgUnit & {
Expand All @@ -21,6 +24,7 @@ export type FilteredDistricts = {
geo_json?: Shape | undefined;
has_geo_json: boolean;
scope?: Scope;
validation_status: OrgUnitStatus;
};

export type ShapeRow = Shape & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type Params = {
export const useGetGeoJson = (
topParentId: number | undefined,
orgUnitCategory: string,
validationStatus: string | undefined = 'all',
): UseQueryResult<any> => {
const params: Params = {
validation_status: 'all',
validation_status: validationStatus,
withShapes: 'true',
order: 'id',
orgUnitTypeCategory: orgUnitCategory,
Expand Down

0 comments on commit e396d99

Please sign in to comment.