import React, { useEffect, useState } from 'react'; import { Button, Form, FormGroup, InputGroup, Text, Spinner, } from '@patternfly/react-core'; import PropTypes from 'prop-types'; import { useSelector, useDispatch } from 'react-redux'; import { FilterIcon } from '@patternfly/react-icons'; import { debounce } from 'lodash'; import { get } from 'foremanReact/redux/API'; import { translate as __ } from 'foremanReact/common/I18n'; import { selectTemplateInputs, selectWithKatello, selectHostCount, selectIsLoadingHosts, } from '../../JobWizardSelectors'; import { SelectField } from '../form/SelectField'; import { SelectedChips } from './SelectedChips'; import { TemplateInputs } from './TemplateInputs'; import { HostSearch } from './HostSearch'; import { HostPreviewModal } from './HostPreviewModal'; import { WIZARD_TITLES, HOSTS, HOST_COLLECTIONS, HOST_GROUPS, hostMethods, HOSTS_API, HOSTS_TO_PREVIEW_AMOUNT, DEBOUNCE_API, } from '../../JobWizardConstants'; import { WizardTitle } from '../form/WizardTitle'; import { SelectAPI } from './SelectAPI'; import { SelectGQL } from './SelectGQL'; import { buildHostQuery } from './buildHostQuery'; const HostsAndInputs = ({ templateValues, setTemplateValues, selected, setSelected, hostsSearchQuery, setHostsSearchQuery, }) => { const [hostMethod, setHostMethod] = useState(hostMethods.hosts); const isLoading = useSelector(selectIsLoadingHosts); const templateInputs = useSelector(selectTemplateInputs); const [hostPreviewOpen, setHostPreviewOpen] = useState(false); const [wasFocus, setWasFocus] = useState(false); const [isError, setIsError] = useState(false); useEffect(() => { if (wasFocus) { if ( selected.hosts.length === 0 && selected.hostCollections.length === 0 && selected.hostGroups.length === 0 && hostsSearchQuery.length === 0 ) { setIsError(true); } else { setIsError(false); } } }, [ hostMethod, hostsSearchQuery.length, selected, selected.hostCollections.length, selected.hostGroups.length, selected.hosts.length, wasFocus, ]); useEffect(() => { debounce(() => { dispatch( get({ key: HOSTS_API, url: '/api/hosts', params: { search: buildHostQuery(selected, hostsSearchQuery), per_page: HOSTS_TO_PREVIEW_AMOUNT, }, }) ); }, DEBOUNCE_API)(); }, [ dispatch, selected, selected.hosts, selected.hostCollections, selected.hostCollections, hostsSearchQuery, ]); const withKatello = useSelector(selectWithKatello); const hostCount = useSelector(selectHostCount); const dispatch = useDispatch(); const selectedHosts = selected.hosts; const setSelectedHosts = newSelected => setSelected(prevSelected => ({ ...prevSelected, hosts: newSelected(prevSelected.hosts), })); const selectedHostCollections = selected.hostCollections; const setSelectedHostCollections = newSelected => setSelected(prevSelected => ({ ...prevSelected, hostCollections: newSelected(prevSelected.hostCollections), })); const selectedHostGroups = selected.hostGroups; const setSelectedHostGroups = newSelected => { setSelected(prevSelected => ({ ...prevSelected, hostGroups: newSelected(prevSelected.hostGroups), })); }; const clearSearch = () => { setHostsSearchQuery(''); }; const [errorText, setErrorText] = useState( __('Please select at least one host') ); return (
{hostPreviewOpen && ( )}
setWasFocus(true)}> } fieldId="host_methods" options={Object.values(hostMethods).filter(method => { if (method === hostMethods.hostCollections && !withKatello) { return false; } return true; })} setValue={val => { setHostMethod(val); if (val === hostMethods.searchQuery) { setErrorText(__('Please enter a search query')); } if (val === hostMethods.hosts) { setErrorText(__('Please select at least one host')); } if (val === hostMethods.hostCollections) { setErrorText( __('Please select at least one host collection') ); } if (val === hostMethods.hostGroups) { setErrorText(__('Please select at least one host group')); } }} value={hostMethod} /> {hostMethod === hostMethods.searchQuery && ( )} {hostMethod === hostMethods.hosts && ( )} {hostMethod === hostMethods.hostCollections && ( )} {hostMethod === hostMethods.hostGroups && ( )} {__('Apply to')}{' '} {' '} {isLoading && }
); }; HostsAndInputs.propTypes = { templateValues: PropTypes.object.isRequired, setTemplateValues: PropTypes.func.isRequired, selected: PropTypes.shape({ hosts: PropTypes.array.isRequired, hostCollections: PropTypes.array.isRequired, hostGroups: PropTypes.array.isRequired, }).isRequired, setSelected: PropTypes.func.isRequired, hostsSearchQuery: PropTypes.string.isRequired, setHostsSearchQuery: PropTypes.func.isRequired, }; export default HostsAndInputs;