diff --git a/awx/ui_next/src/screens/Host/HostList/HostList.jsx b/awx/ui_next/src/screens/Host/HostList/HostList.jsx index 0edc224d30..90f9f3e00a 100644 --- a/awx/ui_next/src/screens/Host/HostList/HostList.jsx +++ b/awx/ui_next/src/screens/Host/HostList/HostList.jsx @@ -1,5 +1,5 @@ -import React, { Component, Fragment } from 'react'; -import { withRouter } from 'react-router-dom'; +import React, { Fragment, useState, useEffect, useCallback } from 'react'; +import { useLocation, useParams } from 'react-router-dom'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { Card } from '@patternfly/react-core'; @@ -12,6 +12,7 @@ import PaginatedDataList, { ToolbarAddButton, ToolbarDeleteButton, } from '@components/PaginatedDataList'; +import useRequest, { useDeleteItems } from '@util/useRequest'; import { getQSConfig, parseQueryString } from '@util/qs'; import HostListItem from './HostListItem'; @@ -22,263 +23,158 @@ const QS_CONFIG = getQSConfig('host', { order_by: 'name', }); -class HostsList extends Component { - constructor(props) { - super(props); +function HostList({ i18n }) { + const location = useLocation(); + const match = useParams(); + const [selected, setSelected] = useState([]); - this.state = { - hasContentLoading: true, - contentError: null, - deletionError: null, + const { + result: { hosts, count, actions }, + error: contentError, + isLoading, + request: fetchHosts, + } = useRequest( + useCallback(async () => { + const params = parseQueryString(QS_CONFIG, location.search); + const results = await Promise.all([ + HostsAPI.read(params), + HostsAPI.readOptions(), + ]); + return { + hosts: results[0].data.results, + count: results[0].data.count, + actions: results[1].data.actions, + }; + }, [location]), + { hosts: [], - selected: [], - itemCount: 0, - actions: null, - toggleError: null, - toggleLoading: null, - }; - - this.handleSelectAll = this.handleSelectAll.bind(this); - this.handleSelect = this.handleSelect.bind(this); - this.handleHostDelete = this.handleHostDelete.bind(this); - this.handleDeleteErrorClose = this.handleDeleteErrorClose.bind(this); - this.loadActions = this.loadActions.bind(this); - this.loadHosts = this.loadHosts.bind(this); - this.handleHostToggle = this.handleHostToggle.bind(this); - this.handleHostToggleErrorClose = this.handleHostToggleErrorClose.bind( - this - ); - } - - componentDidMount() { - this.loadHosts(); - } - - componentDidUpdate(prevProps) { - const { location } = this.props; - if (location !== prevProps.location) { - this.loadHosts(); + count: 0, + actions: {}, } - } + ); - handleSelectAll(isSelected) { - const { hosts } = this.state; + useEffect(() => { + fetchHosts(); + }, [fetchHosts]); - const selected = isSelected ? [...hosts] : []; - this.setState({ selected }); - } + const isAllSelected = selected.length === hosts.length && selected.length > 0; + const { + isLoading: isDeleteLoading, + deleteItems: deleteHosts, + deletionError, + clearDeletionError, + } = useDeleteItems( + useCallback(async () => { + return Promise.all(selected.map(host => HostsAPI.destroy(host.id))); + }, [selected]), + { + qsConfig: QS_CONFIG, + allItemsSelected: isAllSelected, + fetchItems: fetchHosts, + } + ); - handleSelect(row) { - const { selected } = this.state; + const handleHostDelete = async () => { + await deleteHosts(); + setSelected([]); + }; - if (selected.some(s => s.id === row.id)) { - this.setState({ selected: selected.filter(s => s.id !== row.id) }); + const handleSelectAll = isSelected => { + setSelected(isSelected ? [...hosts] : []); + }; + + const handleSelect = host => { + if (selected.some(h => h.id === host.id)) { + setSelected(selected.filter(h => h.id !== host.id)); } else { - this.setState({ selected: selected.concat(row) }); + setSelected(selected.concat(host)); } - } + }; - handleDeleteErrorClose() { - this.setState({ deletionError: null }); - } + const canAdd = + actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); - handleHostToggleErrorClose() { - this.setState({ toggleError: null }); - } - - async handleHostDelete() { - const { selected } = this.state; - - this.setState({ hasContentLoading: true }); - try { - await Promise.all(selected.map(host => HostsAPI.destroy(host.id))); - } catch (err) { - this.setState({ deletionError: err }); - } finally { - await this.loadHosts(); - } - } - - async handleHostToggle(hostToToggle) { - const { hosts } = this.state; - this.setState({ toggleLoading: hostToToggle.id }); - try { - const { data: updatedHost } = await HostsAPI.update(hostToToggle.id, { - enabled: !hostToToggle.enabled, - }); - this.setState({ - hosts: hosts.map(host => - host.id === updatedHost.id ? updatedHost : host - ), - }); - } catch (err) { - this.setState({ toggleError: err }); - } finally { - this.setState({ toggleLoading: null }); - } - } - - async loadActions() { - const { actions: cachedActions } = this.state; - let optionsPromise; - if (cachedActions) { - optionsPromise = Promise.resolve({ data: { actions: cachedActions } }); - } else { - optionsPromise = HostsAPI.readOptions(); - } - - return optionsPromise; - } - - async loadHosts() { - const { location } = this.props; - const params = parseQueryString(QS_CONFIG, location.search); - - const promises = Promise.all([HostsAPI.read(params), this.loadActions()]); - - this.setState({ contentError: null, hasContentLoading: true }); - try { - const [ - { - data: { count, results }, - }, - { - data: { actions }, - }, - ] = await promises; - this.setState({ - actions, - itemCount: count, - hosts: results, - selected: [], - }); - } catch (err) { - this.setState({ contentError: err }); - } finally { - this.setState({ hasContentLoading: false }); - } - } - - render() { - const { - actions, - itemCount, - contentError, - hasContentLoading, - deletionError, - selected, - hosts, - toggleLoading, - toggleError, - } = this.state; - const { match, i18n } = this.props; - - const canAdd = - actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); - const isAllSelected = - selected.length > 0 && selected.length === hosts.length; - - return ( - - - ( - , - ...(canAdd - ? [ - , - ] - : []), - ]} - /> - )} - renderItem={o => ( - row.id === o.id)} - onSelect={() => this.handleSelect(o)} - onToggleHost={this.handleHostToggle} - toggleLoading={toggleLoading === o.id} - /> - )} - emptyStateControls={ - canAdd ? ( - - ) : null - } - /> - - {toggleError && !toggleLoading && ( - - {i18n._(t`Failed to toggle host.`)} - - - )} - {deletionError && ( - - {i18n._(t`Failed to delete one or more hosts.`)} - - - )} - - ); - } + return ( + + + ( + , + ...(canAdd ? [( + ] + ) : [], + ]} + /> + )} + renderItem={o => ( + row.id === o.id)} + onSelect={() => handleSelect(o)} + /> + )} + emptyStateControls={ + canAdd ? ( + + ) : null + } + /> + + {deletionError && ( + + {i18n._(t`Failed to delete one or more hosts.`)} + + + )} + + ); } -export { HostsList as _HostsList }; -export default withI18n()(withRouter(HostsList)); +export default withI18n()(HostList); diff --git a/awx/ui_next/src/screens/Host/HostList/HostListItem.jsx b/awx/ui_next/src/screens/Host/HostList/HostListItem.jsx index 2dda211169..3d59e29532 100644 --- a/awx/ui_next/src/screens/Host/HostList/HostListItem.jsx +++ b/awx/ui_next/src/screens/Host/HostList/HostListItem.jsx @@ -10,7 +10,6 @@ import { DataListItem, DataListItemRow, DataListItemCells, - Switch, Tooltip, } from '@patternfly/react-core'; import { Link } from 'react-router-dom'; @@ -18,6 +17,7 @@ import { PencilAltIcon } from '@patternfly/react-icons'; import Sparkline from '@components/Sparkline'; import { Host } from '@types'; +import HostToggle from './HostToggle'; import styled from 'styled-components'; const DataListAction = styled(_DataListAction)` @@ -36,15 +36,7 @@ class HostListItem extends React.Component { }; render() { - const { - host, - isSelected, - onSelect, - detailUrl, - onToggleHost, - toggleLoading, - i18n, - } = this.props; + const { host, isSelected, onSelect, detailUrl, i18n } = this.props; const recentPlaybookJobs = host.summary_fields.recent_jobs.map(job => ({ ...job, @@ -87,6 +79,22 @@ class HostListItem extends React.Component { )} , + + + , + + {host.summary_fields.user_capabilities.edit && ( + + + + )} + , ]} /> - - onToggleHost(host)} - aria-label={i18n._(t`Toggle host`)} - /> - + {host.summary_fields.user_capabilities.edit && (