import React, { useEffect } from 'react'; // Don't useContext here so this component is reusable import PropTypes from 'prop-types'; import { Alert, ToolbarItem, Text, TextContent, TextVariants, } from '@patternfly/react-core'; import TableIndexPage from 'foremanReact/components/PF4/TableIndexPage/TableIndexPage'; import { HOSTS_API_PATH } from 'foremanReact/routes/Hosts/constants'; import { translate as __ } from 'foremanReact/common/I18n'; import SelectAllCheckbox from 'foremanReact/components/PF4/TableIndexPage/Table/SelectAllCheckbox'; import { RowSelectTd } from 'foremanReact/components/HostsIndex/RowSelectTd'; import { getPageStats } from 'foremanReact/components/PF4/TableIndexPage/Table/helpers'; const HostReview = ({ initialSelectedHosts, hostsBulkSelect, setShouldValidateStep, }) => { const apiOptions = { key: 'HOST_REVIEW' }; const { hostsBulkSelect: { selectPage, selectAll, selectNone, selectOne, isSelected, selectedCount, areAllRowsSelected, areAllRowsOnPageSelected, updateSearchQuery, hasInteracted, }, hostsMetadata: { subtotal, total, page, per_page: perPage, }, hostsResponse: response, } = hostsBulkSelect; const { response: { results } = {} } = response; useEffect(() => { if (results?.length && hasInteracted) { setShouldValidateStep(true); } }, [setShouldValidateStep, results?.length, hasInteracted]); const pageStats = getPageStats({ total: subtotal, page, perPage }); const selectionToolbar = ( ); const columns = { name: { title: __('Name'), wrapper: ({ name, display_name: displayName }) => ( {displayName} ), isSorted: true, weight: 50, }, os_title: { title: __('OS'), wrapper: hostDetails => hostDetails?.operatingsystem_name, isSorted: true, weight: 100, }, }; // restrict search query to only selected hosts const restrictedSearchQuery = (newSearch) => { let newSearchQuery = initialSelectedHosts; const trimmedSearch = newSearch?.trim() ?? ''; if (!!trimmedSearch && !trimmedSearch.includes(initialSelectedHosts)) { newSearchQuery = `${initialSelectedHosts} and ${trimmedSearch}`; } return newSearchQuery; }; return ( <> {__('Review hosts')} {__('Review and optionally exclude hosts from your selection.')} {selectedCount === 0 && hasInteracted && ( )} ); }; HostReview.propTypes = { initialSelectedHosts: PropTypes.string, hostsBulkSelect: PropTypes.shape({ hostsBulkSelect: PropTypes.shape({ selectPage: PropTypes.func, selectAll: PropTypes.func, selectNone: PropTypes.func, selectOne: PropTypes.func, exclusionSet: PropTypes.object, // eslint-disable-line react/forbid-prop-types isSelected: PropTypes.func, selectedCount: PropTypes.number, areAllRowsSelected: PropTypes.func, areAllRowsOnPageSelected: PropTypes.func, updateSearchQuery: PropTypes.func, inclusionSet: PropTypes.object, // eslint-disable-line react/forbid-prop-types hasInteracted: PropTypes.bool, }), hostsMetadata: PropTypes.shape({ total: PropTypes.number, page: PropTypes.number, per_page: PropTypes.number, subtotal: PropTypes.number, }), hostsResponse: PropTypes.shape([]), }), setShouldValidateStep: PropTypes.func.isRequired, }; HostReview.defaultProps = { hostsBulkSelect: {}, initialSelectedHosts: '', }; export default HostReview;