import React, { useState } from 'react'; import useDeepCompareEffect from 'use-deep-compare-effect'; import { useDispatch, useSelector } from 'react-redux'; import { STATUS } from 'foremanReact/constants'; import PropTypes from 'prop-types'; import { translate as __ } from 'foremanReact/common/I18n'; import { Modal, ModalVariant, Form, FormGroup, TextInput, ActionGroup, Button, FormSelect, FormSelectOption, } from '@patternfly/react-core'; import { addCVFilterRule, editCVFilterRule, getCVFilterRules } from '../../../ContentViewDetailActions'; import { selectCreateFilterRuleStatus, } from '../../../ContentViewDetailSelectors'; import { orgId } from '../../../../../../services/api'; import SearchText from '../../../../../../components/Search/SearchText'; const AddEditPackageRuleModal = ({ filterId, onClose, selectedFilterRuleData, repositoryIds, }) => { const { id: editingId, name: editingName, arch: editingArchitecture, version: editingVersion, min_version: editingMinVersion, max_version: editingMaxVersion, } = selectedFilterRuleData || {}; const architectureAutoCompleteEndpoint = '/katello/api/v2/packages/auto_complete_arch'; const nameAutoCompleteEndpoint = '/katello/api/v2/packages/auto_complete_name'; const isEditing = !!selectedFilterRuleData; const VersionModifiers = { 'All versions': __('All versions'), 'Equal to': __('Equal to'), 'Greater than': __('Greater than'), 'Less than': __('Less than'), /* eslint-disable quote-props */ 'Range': __('Range'), }; const versionText = () => { switch (true) { case !!editingVersion: return VersionModifiers['Equal to']; case !!editingMinVersion && !editingMaxVersion: return VersionModifiers['Greater than']; case !editingMinVersion && !!editingMaxVersion: return VersionModifiers['Less than']; case !!editingMinVersion && !!editingMaxVersion: return VersionModifiers.Range; default: return VersionModifiers['All versions']; } }; const [name, setName] = useState(editingName || ''); const [architecture, setArchitecture] = useState(editingArchitecture || ''); const [version, setVersion] = useState(editingVersion || ''); const [minVersion, setMinVersion] = useState(editingMinVersion || ''); const [maxVersion, setMaxVersion] = useState(editingMaxVersion || ''); const [versionComparator, setVersionComparator] = useState(versionText(selectedFilterRuleData)); const [saving, setSaving] = useState(false); const dispatch = useDispatch(); const status = useSelector(state => selectCreateFilterRuleStatus(state)); const submitDisabled = !name || name.length === 0; const showVersion = versionComparator === VersionModifiers['Equal to']; const showMinVersion = versionComparator === VersionModifiers['Greater than'] || versionComparator === VersionModifiers.Range; const showMaxVersion = versionComparator === VersionModifiers['Less than'] || versionComparator === VersionModifiers.Range; const formVersionParams = () => { switch (versionComparator) { case 'All versions': return {}; case 'Equal to': return { version }; case 'Greater than': return { min_version: minVersion }; case 'Less than': return { max_version: maxVersion }; case 'Range': return { min_version: minVersion, max_version: maxVersion }; default: return {}; } }; const onSubmit = () => { setSaving(true); dispatch(isEditing ? editCVFilterRule( filterId, { id: editingId, name, architecture, ...formVersionParams(), }, () => { dispatch(getCVFilterRules(filterId)); onClose(); }, ) : addCVFilterRule( filterId, { name, architecture, ...formVersionParams() }, () => { dispatch(getCVFilterRules(filterId)); onClose(); }, )); }; useDeepCompareEffect(() => { if (status === STATUS.ERROR) { setSaving(false); } }, [status, setSaving]); const searchDataProp = term => ({ organization_id: orgId(), term, repoids: repositoryIds, non_modular: true, }); return (
{ e.preventDefault(); onSubmit(); }} > searchDataProp(input), }, }} onSearchChange={setName} /> searchDataProp(arch), }, }} onSearchChange={setArchitecture} /> { Object.values(VersionModifiers).map(item => ( )) } {showVersion && } {showMinVersion && } {showMaxVersion && }
); }; AddEditPackageRuleModal.propTypes = { filterId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, onClose: PropTypes.func, selectedFilterRuleData: PropTypes.shape({ id: PropTypes.number, name: PropTypes.string, arch: PropTypes.string, version: PropTypes.string, min_version: PropTypes.string, max_version: PropTypes.string, }), repositoryIds: PropTypes.arrayOf(PropTypes.number), }; AddEditPackageRuleModal.defaultProps = { onClose: null, selectedFilterRuleData: undefined, repositoryIds: [], }; export default AddEditPackageRuleModal;