Skip to content

Commit

Permalink
Merge pull request RedHatInsights#734 from karelhala/inventory-custom…
Browse files Browse the repository at this point in the history
…-filters

Allow custom filters to be passed from parent
  • Loading branch information
Hyperkid123 committed Sep 22, 2020
2 parents 2fb1a32 + cdc4cdd commit 96d6ceb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Expand Up @@ -24,6 +24,7 @@ const frontendComponentsMappe = {
NullBattery: 'Battery',
ConnectedBreadcrumbs: 'Breadcrumbs',
ConditionalFilterType: 'ConditionalFilter',
conditionalFilterType: 'ConditionalFilter',
groupType: 'ConditionalFilter',
DarkContext: 'Dark',
FilterDropdown: 'Filters',
Expand Down
15 changes: 11 additions & 4 deletions packages/inventory/src/api/api.js
Expand Up @@ -72,7 +72,8 @@ export function getEntities(items, {
per_page: perPage,
page,
orderBy = 'updated',
orderDirection = 'DESC'
orderDirection = 'DESC',
...options
}, showTags) {
const {
hostnameOrId,
Expand Down Expand Up @@ -106,7 +107,10 @@ export function getEntities(items, {
orderBy,
orderDirection,
staleFilter,
constructTags(tagFilters),
[
...constructTags(tagFilters),
...options.tags || []
],
registeredWithFilter,
{ cancelToken: controller && controller.token }
)
Expand Down Expand Up @@ -143,14 +147,17 @@ export function getTags(systemId, search, { pagination } = { pagination: {} }) {
);
}

export function getAllTags(search, { filters, pagination } = { pagination: {} }) {
export function getAllTags(search, { filters, pagination, ...options } = { pagination: {} }) {
const {
tagFilters,
staleFilter,
registeredWithFilter
} = filters ? filters.reduce(filtersReducer, defaultFilters) : defaultFilters;
return tags.apiTagGetTags(
tagFilters ? constructTags(tagFilters) : undefined,
[
...tagFilters ? constructTags(tagFilters) : [],
...options.tags || []
],
'tag',
'ASC',
(pagination && pagination.perPage) || 10,
Expand Down
24 changes: 19 additions & 5 deletions packages/inventory/src/components/table/EntityTableToolbar.js
Expand Up @@ -56,6 +56,7 @@ const EntityTableToolbar = ({
isLoaded,
items,
sortBy,
customFilters,
...props
}) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -92,7 +93,10 @@ const EntityTableToolbar = ({
*/
const debounceGetAllTags = useCallback(debounce((config, options) => {
if (showTags && !hasItems) {
dispatch(fetchAllTags(config, options));
dispatch(fetchAllTags({
...customFilters,
...config
}, options));
}
}, 800), []);

Expand All @@ -102,7 +106,7 @@ const EntityTableToolbar = ({
const onRefreshData = useCallback((options) => {
dispatch(loadSystems(options, showTags));
if (showTags && !hasItems) {
dispatch(fetchAllTags(filterTagsBy, { filters: options.filters }));
dispatch(fetchAllTags(filterTagsBy, { ...customFilters, filters: options.filters }));
}
});

Expand All @@ -124,8 +128,11 @@ const EntityTableToolbar = ({
};
onRefresh ? onRefresh(params, (options) => {
dispatch(entitiesLoading());
onRefreshData({ ...params, ...options });
}) : onRefreshData(params);
onRefreshData({ ...params, ...customFilters, ...options });
}) : onRefreshData({
...customFilters,
...params
});
};

/**
Expand Down Expand Up @@ -302,6 +309,7 @@ const EntityTableToolbar = ({
{
showTags &&
<TagsModal
customFilters={customFilters}
filterTagsBy={filterTagsBy}
onApply={(selected) => setSelectedTags(arrayToSelection(selected))}
onToggleModal={() => seFilterTagsBy('')}
Expand All @@ -324,7 +332,13 @@ EntityTableToolbar.propTypes = {
pagination: PrimaryToolbar.propTypes.pagination,
actionsConfig: PrimaryToolbar.propTypes.actionsConfig,
activeFiltersConfig: PrimaryToolbar.propTypes.activeFiltersConfig,
onRefreshData: PropTypes.func
onRefreshData: PropTypes.func,
customFilters: PropTypes.shape({
tags: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.string)
])
})
};

EntityTableToolbar.defaultProps = {
Expand Down
11 changes: 9 additions & 2 deletions packages/inventory/src/components/table/InventoryList.js
Expand Up @@ -16,14 +16,15 @@ class ContextInventoryList extends React.Component {
* @param {*} options new options to be applied, like pagination, filters, etc.
*/
onRefreshData = (options = {}) => {
const { page, perPage, items, hasItems, sortBy, activeFilters, showTags } = this.props;
const { page, perPage, items, hasItems, sortBy, activeFilters, showTags, customFilters } = this.props;
this.props.loadEntities && this.props.loadEntities({
page,
perPage,
items,
hasItems,
sortBy,
activeFilters,
...customFilters,
...options
}, showTags);
}
Expand Down Expand Up @@ -111,7 +112,13 @@ InventoryList.propTypes = {
title: PropTypes.node
})
]),
entities: PropTypes.arrayOf(PropTypes.any)
entities: PropTypes.arrayOf(PropTypes.any),
customFilters: PropTypes.shape({
tags: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.string)
])
})
};

export default InventoryList;
4 changes: 4 additions & 0 deletions packages/inventory/src/components/table/InventoryTable.js
Expand Up @@ -23,6 +23,7 @@ const InventoryTable = forwardRef(({
filters,
showTags,
sortBy: propsSortBy,
customFilters,
...props
}, ref) => {
const hasItems = Boolean(items);
Expand Down Expand Up @@ -54,6 +55,7 @@ const InventoryTable = forwardRef(({
<Fragment>
<EntityTableToolbar
{ ...props }
customFilters={customFilters}
items={ items }
onRefresh={onRefresh}
filters={ filters }
Expand All @@ -67,6 +69,7 @@ const InventoryTable = forwardRef(({
</EntityTableToolbar>
<InventoryList
{ ...props }
customFilters={customFilters}
ref={ref}
hasItems={ hasItems }
onRefresh={ onRefresh }
Expand All @@ -78,6 +81,7 @@ const InventoryTable = forwardRef(({
/>
<TableToolbar isFooter className="ins-c-inventory__table--toolbar">
<Pagination
customFilters={customFilters}
isFull
items={ items }
total={ pagination.total }
Expand Down
20 changes: 16 additions & 4 deletions packages/inventory/src/components/table/Pagination.js
Expand Up @@ -21,12 +21,18 @@ class FooterPagination extends Component {
* @param {*} pagination contains new pagination config.
*/
updatePagination = (pagination) => {
const { onRefresh, dataLoading, loadEntities, showTags } = this.props;
const { onRefresh, dataLoading, loadEntities, showTags, customFilters } = this.props;
if (onRefresh) {
dataLoading();
onRefresh(pagination, (options) => loadEntities(options, showTags));
onRefresh(pagination, (options) => loadEntities({
...customFilters,
...options
}, showTags));
} else {
loadEntities(pagination, showTags);
loadEntities({
...customFilters,
...pagination
}, showTags);
}
}

Expand Down Expand Up @@ -96,7 +102,13 @@ const propTypes = {
loaded: PropTypes.bool,
isFull: PropTypes.bool,
onRefresh: PropTypes.func,
direction: PropTypes.string
direction: PropTypes.string,
customFilters: PropTypes.shape({
tags: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.string)
])
})
};

FooterPagination.propTypes = propTypes;
Expand Down
12 changes: 9 additions & 3 deletions packages/inventory/src/shared/TagsModal.js
Expand Up @@ -17,9 +17,9 @@ class TagsModal extends React.Component {

fetchTags = (pagination) => {
const { filterTagsBy } = this.state;
const { fetchAllTags, fetchTagsForSystem, activeSystemTag, tagsCount, filters } = this.props;
const { fetchAllTags, fetchTagsForSystem, activeSystemTag, tagsCount, filters, customFilters } = this.props;
if (!activeSystemTag) {
fetchAllTags(filterTagsBy, { pagination, filters });
fetchAllTags(filterTagsBy, { ...customFilters, pagination, filters });
} else {
fetchTagsForSystem(activeSystemTag.id, filterTagsBy, { pagination }, tagsCount);
}
Expand Down Expand Up @@ -135,7 +135,13 @@ TagsModal.propTypes = {
onToggleTagModal: PropTypes.func,
fetchAllTags: PropTypes.func,
onApply: PropTypes.func,
onToggleModal: PropTypes.func
onToggleModal: PropTypes.func,
customFilters: PropTypes.shape({
tags: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.string)
])
})
};

TagsModal.defaultProps = {
Expand Down

0 comments on commit 96d6ceb

Please sign in to comment.