synapse-react-client
Version:
[](https://travis-ci.com/Sage-Bionetworks/Synapse-React-Client) [](https://badge.fury.io/js/synaps
85 lines • 4.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsAllSelectedFromInfiniteList = void 0;
var react_1 = require("react");
var EntityTypeUtils_1 = require("../functions/EntityTypeUtils");
/**
* Given a list of entities and a map of which are selected, return a boolean indicating if all of the entities that can be selected
* are selected.
*
* This function is compatible with react-query's useInfiniteQuery pagination, in which case it's recommended to use the hook version
* of this function to get updates as pages are fetched and updated.
*
* We don't currently support an indeterminate checkbox state because that would require always fetching all pages of a given query, which does not scale.
* @param entities An arbitrary list of entities that are displayed
* @param selected A map where keys are selected entity IDs
* @param selectableTypes A list of entity types which can be selected. Will be used to filter the list of entities
* @param hasNextPage Whether or not there are additional pages of entities (typically supplied by useInfiniteQuery)
* @param fetchNextPage A function to be invoked to fetch an additional page of entities (typically supplied by useInfiniteQuery)
* @param isFetchingNextPage Indicates if the next page of entities is being fetched (typically supplied by useInfiniteQuery)
* @returns true iff all selectable entities have been selected, and there are no additional pages to fetch (because an unfetched page may include an unselected entity)
*/
function getIsAllSelectedFromInfiniteList(entities, selected, selectableTypes, hasNextPage, fetchNextPage, isFetchingNextPage) {
if (entities.length === 0 || selected.size === 0) {
return false;
}
else {
var allSelectableFetchedChildrenAreSelected = entities.every(function (e) {
return selected.has(e.id) ||
!selectableTypes.includes((0, EntityTypeUtils_1.getEntityTypeFromHeader)(e));
});
if (allSelectableFetchedChildrenAreSelected &&
hasNextPage &&
fetchNextPage &&
!isFetchingNextPage) {
// We don't yet know if the checkbox should be true or false, so get the next page
fetchNextPage();
return false;
}
else {
if (isFetchingNextPage) {
// Wait for the next page to be retrieved
return false;
}
// At this point, we've fetched all of the pages or encountered an unselected entity
// The checkbox should be true if there are no unselected entities, and there's at least one selectable entity
var selectableEntitiesArePresent = entities.some(function (e) {
return selectableTypes.includes((0, EntityTypeUtils_1.getEntityTypeFromHeader)(e));
});
return (allSelectableFetchedChildrenAreSelected && selectableEntitiesArePresent);
}
}
}
exports.getIsAllSelectedFromInfiniteList = getIsAllSelectedFromInfiniteList;
/**
/**
* Given a list of entities and a map of which are selected, return a boolean indicating if all of the entities that can be selected
* are selected.
*
* This function is compatible with react-query's useInfiniteQuery pagination.
*
* There is also a non-hook version of this function that can be called in contexts where hooks cannot be used.
* @param entities An arbitrary list of entities that are displayed
* @param selected A map where keys are selected entity IDs
* @param selectableTypes A list of entity types which can be selected. Will be used to filter the list of entities
* @param hasNextPage Whether or not there are additional pages of entities (typically supplied by useInfiniteQuery)
* @param fetchNextPage A function to be invoked to fetch an additional page of entities (typically supplied by useInfiniteQuery)
* @param isFetchingNextPage Indicates if the next page of entities is being fetched (typically supplied by useInfiniteQuery)
* @returns true iff all selectable entities have been selected, and there are no additional pages to fetch (because an unfetched page may include an unselected entity)
*/
function useGetIsAllSelectedFromInfiniteList(entities, selected, selectableTypes, hasNextPage, fetchNextPage, isFetchingNextPage) {
var _a = (0, react_1.useState)(false), isSelectAll = _a[0], setIsSelectAll = _a[1];
(0, react_1.useEffect)(function () {
setIsSelectAll(getIsAllSelectedFromInfiniteList(entities, selected, selectableTypes, hasNextPage, fetchNextPage, isFetchingNextPage));
}, [
entities,
fetchNextPage,
hasNextPage,
selected,
selectableTypes,
isFetchingNextPage,
]);
return isSelectAll;
}
exports.default = useGetIsAllSelectedFromInfiniteList;
//# sourceMappingURL=useGetIsAllSelectedInfiniteList.js.map