From 766b2f774d2fa81aadf9631f5bae48b98a0cfa9c Mon Sep 17 00:00:00 2001 From: Alex Corey Date: Thu, 19 Nov 2020 15:28:34 -0500 Subject: [PATCH] convert PaginatedDataList to function --- .../PaginatedDataList/PaginatedDataList.jsx | 273 +++++++++--------- 1 file changed, 129 insertions(+), 144 deletions(-) diff --git a/awx/ui_next/src/components/PaginatedDataList/PaginatedDataList.jsx b/awx/ui_next/src/components/PaginatedDataList/PaginatedDataList.jsx index 8bd449e851..ab60173e75 100644 --- a/awx/ui_next/src/components/PaginatedDataList/PaginatedDataList.jsx +++ b/awx/ui_next/src/components/PaginatedDataList/PaginatedDataList.jsx @@ -1,9 +1,10 @@ import React, { Fragment } from 'react'; + import PropTypes from 'prop-types'; import { DataList } from '@patternfly/react-core'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; -import { withRouter } from 'react-router-dom'; +import { withRouter, useHistory, useLocation } from 'react-router-dom'; import ListHeader from '../ListHeader'; import ContentEmpty from '../ContentEmpty'; @@ -22,166 +23,150 @@ import { QSConfig, SearchColumns, SortColumns } from '../../types'; import PaginatedDataListItem from './PaginatedDataListItem'; -class PaginatedDataList extends React.Component { - constructor(props) { - super(props); - this.handleSetPage = this.handleSetPage.bind(this); - this.handleSetPageSize = this.handleSetPageSize.bind(this); - this.handleListItemSelect = this.handleListItemSelect.bind(this); - } - - handleListItemSelect = (id = 0) => { - const { items, onRowClick } = this.props; +function PaginatedDataList({ + items, + onRowClick, + contentError, + hasContentLoading, + emptyStateControls, + itemCount, + qsConfig, + renderItem, + toolbarSearchColumns, + toolbarSearchableKeys, + toolbarRelatedSearchableKeys, + toolbarSortColumns, + pluralizedItemName, + showPageSizeOptions, + location, + i18n, + renderToolbar, +}) { + const { search, pathname } = useLocation(); + const history = useHistory(); + const handleListItemSelect = (id = 0) => { const match = items.find(item => item.id === Number(id)); onRowClick(match); }; - handleSetPage(event, pageNumber) { - const { history, qsConfig } = this.props; - const { search } = history.location; + const handleSetPage = (event, pageNumber) => { const oldParams = parseQueryString(qsConfig, search); - this.pushHistoryState(replaceParams(oldParams, { page: pageNumber })); - } + pushHistoryState(replaceParams(oldParams, { page: pageNumber })); + }; - handleSetPageSize(event, pageSize, page) { - const { history, qsConfig } = this.props; - const { search } = history.location; + const handleSetPageSize = (event, pageSize, page) => { const oldParams = parseQueryString(qsConfig, search); - this.pushHistoryState( - replaceParams(oldParams, { page_size: pageSize, page }) - ); - } + pushHistoryState(replaceParams(oldParams, { page_size: pageSize, page })); + }; - pushHistoryState(params) { - const { history, qsConfig } = this.props; - const { pathname } = history.location; + const pushHistoryState = params => { const encodedParams = encodeNonDefaultQueryString(qsConfig, params); history.push(encodedParams ? `${pathname}?${encodedParams}` : pathname); + }; + + const searchColumns = toolbarSearchColumns.length + ? toolbarSearchColumns + : [ + { + name: i18n._(t`Name`), + key: 'name', + isDefault: true, + }, + ]; + const sortColumns = toolbarSortColumns.length + ? toolbarSortColumns + : [ + { + name: i18n._(t`Name`), + key: 'name', + }, + ]; + const queryParams = parseQueryString(qsConfig, location.search); + + const dataListLabel = i18n._(t`${pluralizedItemName} List`); + const emptyContentMessage = i18n._( + t`Please add ${pluralizedItemName} to populate this list ` + ); + const emptyContentTitle = i18n._(t`No ${pluralizedItemName} Found `); + + let Content; + if (hasContentLoading && items.length <= 0) { + Content = ; + } else if (contentError) { + Content = ; + } else if (items.length <= 0) { + Content = ( + + ); + } else { + Content = ( + handleListItemSelect(id)} + > + {items.map(renderItem)} + + ); } - render() { - const { - contentError, - hasContentLoading, - emptyStateControls, - items, - itemCount, - qsConfig, - renderItem, - toolbarSearchColumns, - toolbarSearchableKeys, - toolbarRelatedSearchableKeys, - toolbarSortColumns, - pluralizedItemName, - showPageSizeOptions, - location, - i18n, - renderToolbar, - } = this.props; - const searchColumns = toolbarSearchColumns.length - ? toolbarSearchColumns - : [ - { - name: i18n._(t`Name`), - key: 'name', - isDefault: true, - }, - ]; - const sortColumns = toolbarSortColumns.length - ? toolbarSortColumns - : [ - { - name: i18n._(t`Name`), - key: 'name', - }, - ]; - const queryParams = parseQueryString(qsConfig, location.search); + const ToolbarPagination = ( + + ); - const dataListLabel = i18n._(t`${pluralizedItemName} List`); - const emptyContentMessage = i18n._( - t`Please add ${pluralizedItemName} to populate this list ` - ); - const emptyContentTitle = i18n._(t`No ${pluralizedItemName} Found `); - - let Content; - if (hasContentLoading && items.length <= 0) { - Content = ; - } else if (contentError) { - Content = ; - } else if (items.length <= 0) { - Content = ( - - ); - } else { - Content = ( - this.handleListItemSelect(id)} - > - {items.map(renderItem)} - - ); - } - - const ToolbarPagination = ( - + - ); - - return ( - - - {Content} - {items.length ? ( - - ) : null} - - ); - } + ) : null} + + ); } const Item = PropTypes.shape({