From f7077599536141cbfe4f5beb261b30b29a4305c2 Mon Sep 17 00:00:00 2001 From: Martin Hradil Date: Sat, 11 Nov 2023 05:51:37 +0000 Subject: [PATCH] PR-in-a-box #4506 --- .../patternfly-wrappers/pagination.tsx | 64 ++++++----- src/components/shared/hub-list-toolbar.tsx | 35 ++++-- src/constants.tsx | 3 +- .../namespace-list/namespace-list.tsx | 101 +++++------------- src/containers/search/search.tsx | 4 +- 5 files changed, 83 insertions(+), 124 deletions(-) diff --git a/src/components/patternfly-wrappers/pagination.tsx b/src/components/patternfly-wrappers/pagination.tsx index 6bc459a05a..c07e852666 100644 --- a/src/components/patternfly-wrappers/pagination.tsx +++ b/src/components/patternfly-wrappers/pagination.tsx @@ -25,9 +25,6 @@ interface IProps { /** Applies styling to make pagination compact */ isCompact?: boolean; - - /** Options for the number of items that can be displayed per page */ - perPageOptions?: number[]; } // AAP-3737 - support both "1 - 2 of 3" and "3 的 1 - 2" @@ -56,44 +53,45 @@ export const Pagination = ({ params, updateParams, isTop, - perPageOptions, isCompact, }: IProps) => { - const extraProps = {}; - if (!isTop) { - extraProps['widgetId'] = 'pagination-options-menu-bottom'; - extraProps['variant'] = PaginationVariant.bottom; - } + const extraProps = isTop + ? {} + : { + widgetId: 'pagination-options-menu-bottom', + variant: PaginationVariant.bottom, + }; + + const onSetPage = (_, p) => + updateParams(ParamHelper.setParam(params, 'page', p)); + + const onPerPageSelect = (_, p) => { + updateParams({ ...params, page: 1, page_size: p }); + }; + + const perPageOptions = Constants.DEFAULT_PAGINATION_OPTIONS.map((option) => ({ + title: String(option), + value: option, + })); + + const titles = { + ofWord: t`of`, + perPageSuffix: t`per page`, + items: null, + }; return ( - updateParams(ParamHelper.setParam(params, 'page', p)) - } - onPerPageSelect={(_, p) => { - updateParams({ ...params, page: 1, page_size: p }); - }} - {...extraProps} - isCompact={isTop || isCompact} - perPageOptions={mapPerPageOptions( - perPageOptions || Constants.DEFAULT_PAGINATION_OPTIONS, - )} - titles={{ - ofWord: t`of`, - perPageSuffix: t`per page`, - items: null, - }} + perPage={params.page_size || Constants.DEFAULT_PAGE_SIZE} + perPageOptions={perPageOptions} + titles={titles} toggleTemplate={(props) => } + {...extraProps} /> ); }; - -function mapPerPageOptions(options) { - return options.map((option) => ({ - title: String(option), - value: option, - })); -} diff --git a/src/components/shared/hub-list-toolbar.tsx b/src/components/shared/hub-list-toolbar.tsx index aa4b330974..62f7eca535 100644 --- a/src/components/shared/hub-list-toolbar.tsx +++ b/src/components/shared/hub-list-toolbar.tsx @@ -4,7 +4,7 @@ import { ToolbarGroup, ToolbarItem, } from '@patternfly/react-core'; -import React, { useEffect, useState } from 'react'; +import React, { ReactNode, useEffect, useState } from 'react'; import { AppliedFilters, CompoundFilter, @@ -16,6 +16,7 @@ import { import { ParamType } from 'src/utilities'; interface IProps { + buttons?: ReactNode[]; count?: number; filterConfig: FilterOption[]; ignoredParams: string[]; @@ -49,8 +50,9 @@ function useTypeaheads(typeaheads, { inputText, selectedFilter }) { return options; } -// FIXME: missing Buttons & CardListSwitcher to be usable everywhere +// FIXME: missing CardListSwitcher to be usable everywhere export function HubListToolbar({ + buttons, count, filterConfig, ignoredParams, @@ -76,8 +78,14 @@ export function HubListToolbar({ : { ...item, options: item.options || typeaheadOptions[item.id] || [] }, ); + const renderedButtons = buttons?.length + ? buttons.map((button, i) => + button ? {button} : null, + ) + : null; + return ( - + {sortOptions ? ( - - - - ) : null} + + + + + {renderedButtons} + + ) : ( + renderedButtons + )} { hasPermission: true, isModalOpen: false, loading: true, - inputText: params['keywords'] || '', }; } @@ -144,8 +133,7 @@ export class NamespaceList extends React.Component { return ; } - const { alerts, namespaces, params, itemCount, loading, inputText } = - this.state; + const { alerts, namespaces, params, itemCount, loading } = this.state; const { filterOwner } = this.props; const { hasPermission } = this.context; @@ -164,6 +152,18 @@ export class NamespaceList extends React.Component { const updateParams = (p) => this.updateParams(p, () => this.loadNamespaces()); + const filterConfig = [{ id: 'keywords', title: t`keywords` }]; + const sortOptions = [ + { title: t`Name`, id: 'name', type: 'alpha' as const }, + ]; + const buttons = [ + hasPermission('galaxy.add_namespace') ? ( + + ) : null, + ]; + return (
{
)} - {noData ? null : ( -
- - - - - this.setState({ inputText: text })} - params={params} - updateParams={updateParams} - filterConfig={[{ id: 'keywords', title: t`keywords` }]} - /> - { - updateParams(p); - this.setState({ inputText: '' }); - }} - ignoredParams={['page_size', 'page', 'sort']} - niceNames={{ keywords: t`keywords` }} - /> - - - - - - - {hasPermission('galaxy.add_namespace') && ( - - - - )} - - - -
- -
-
- )} + {noData ? null : ( + + )}
{this.renderBody({ updateParams })}
@@ -271,7 +223,6 @@ export class NamespaceList extends React.Component { diff --git a/src/containers/search/search.tsx b/src/containers/search/search.tsx index 4dec34ee12..8c3cef5dcb 100644 --- a/src/containers/search/search.tsx +++ b/src/containers/search/search.tsx @@ -81,7 +81,7 @@ class Search extends React.Component { ]); if (!params['page_size']) { - params['page_size'] = Constants.CARD_DEFAULT_PAGE_SIZE; + params['page_size'] = Constants.DEFAULT_PAGE_SIZE; } // Load view type from local storage if it's not set. This allows a @@ -248,7 +248,6 @@ class Search extends React.Component { params={params} updateParams={updateParams} count={count} - perPageOptions={Constants.CARD_DEFAULT_PAGINATION_OPTIONS} isTop /> @@ -276,7 +275,6 @@ class Search extends React.Component {