Skip to content

Commit

Permalink
deviceHasMatchingFacility treats empty filter as wildcard
Browse files Browse the repository at this point in the history
{} => "are there any facilities at all?"
{ id: <val> } => "are there any facilities that match this id?"

this ensures that we properly handle the case where there are no
facilities
  • Loading branch information
nucleogenesis committed Jul 26, 2024
1 parent 5462947 commit 50dca78
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@
import UiAlert from 'kolibri-design-system/lib/keen/UiAlert';
import commonCoreStrings from 'kolibri.coreVue.mixins.commonCoreStrings';
import commonSyncElements from 'kolibri.coreVue.mixins.commonSyncElements';
import pickBy from 'lodash/pickBy';
import { UnreachableConnectionStatuses } from './constants';
import useDeviceDeletion from './useDeviceDeletion.js';
import {
useDevicesWithFilter,
useDeviceChannelFilter,
useDeviceFacilityFilter,
useDeviceHasFacilitiesFilter,
useDeviceMinimumVersionFilter,
} from './useDevices.js';
import useConnectionChecker from './useConnectionChecker.js';
Expand All @@ -164,27 +164,25 @@
deviceFilters.push(useDeviceChannelFilter({ id: props.filterByChannelId }));
}
if (
props.filterByFacilityId !== null ||
props.filterByFacilityCanSignUp !== null ||
props.filterByOnMyOwnFacility !== null ||
props.filterByHasFacilities !== null
) {
apiParams.subset_of_users_device = false;
if (props.filterByHasFacilities === true) {
deviceFilters.push(useDeviceHasFacilitiesFilter());
}
const pickNotNull = v => v !== null;
// Either we build a facility filter or an empty object.
// Passing the empty object to useDeviceFacilityFilter is asking "are there ANY facilities?"
const facilityFilter = pickBy(
{
id: props.filterByFacilityId,
learner_can_sign_up: props.filterByFacilityCanSignUp,
on_my_own_setup: props.filterByOnMyOwnFacility,
},
pickNotNull,
);
deviceFilters.push(
useDeviceFacilityFilter({
id: props.filterByFacilityId,
learner_can_sign_up: props.filterByFacilityCanSignUp,
on_my_own_setup: props.filterByOnMyOwnFacility,
}),
);
// If we're filtering a particular facility
if (Object.keys(facilityFilter).length > 0) {
apiParams.subset_of_users_device = false;
}
deviceFilters.push(useDeviceFacilityFilter(facilityFilter));
if (props.filterLODAvailable) {
apiParams.subset_of_users_device = false;
deviceFilters.push(useDeviceMinimumVersionFilter(0, 15, 0));
Expand Down
10 changes: 0 additions & 10 deletions kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ export function deviceHasMatchingFacility(device, facility) {
});
}

/**
* @param {NetworkLocation} device
* @return {Promise<boolean>}
*/
export function deviceHasAnyFacilities(device) {
return NetworkLocationResource.fetchFacilities(device.id).then(({ facilities }) => {
return Boolean(facilities.length);
});
}

/**
* @param {string} channelId
* @param {NetworkLocation} device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,6 @@ function useAsyncDeviceFilter(filterFunction) {
};
}

/**
*/
export function useDeviceHasFacilitiesFilter() {
return useAsyncDeviceFilter(function deviceHasFacilitiesFilter(device) {
return deviceHasAnyFacilities(device);
});
}

/**
* Produces a function that resolves with a boolean for a device that has the specified facility
* @param {string|null} [id]
Expand Down Expand Up @@ -208,10 +200,6 @@ export function useDeviceFacilityFilter({
filters.on_my_own_setup = on_my_own_setup;
}

if (Object.keys(filters).length === 0) {
return () => Promise.resolve(true);
}

return useAsyncDeviceFilter(function deviceFacilityFilter(device) {
return deviceHasMatchingFacility(device, filters);
});
Expand Down

0 comments on commit 50dca78

Please sign in to comment.