import React, { useState, useMemo } from 'react'; import { last } from 'lodash'; import PropTypes from 'prop-types'; import { Flex, Modal, ModalVariant, FormSelect, FormSelectOption, Checkbox, Form, FormGroup, ActionGroup, Button, Card, CardTitle, CardBody, Tooltip, } from '@patternfly/react-core'; import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons'; import { useDispatch } from 'react-redux'; import { translate as __ } from 'foremanReact/common/I18n'; import { addComponent } from '../ContentViewDetailActions'; const ComponentContentViewBulkAddModal = ({ cvId, rowsToAdd, onClose }) => { const dispatch = useDispatch(); const [versionSelectOptions, setVersionSelectOptions] = useState({}); const [selectedVersion, setSelectedVersion] = useState({}); const [selectedComponentLatest, setSelectedComponentLatest] = useState({}); useMemo(() => { const versionSelect = {}; const versionSelectedOption = {}; const componentLatest = {}; rowsToAdd.forEach((row) => { const { componentCvVersions: versions, componentCvName: name } = row; const sortedVersions = [].concat(versions).sort((a, b) => (a.id > b.id ? 1 : -1)); versionSelect[name] = sortedVersions; versionSelectedOption[name] = last(sortedVersions)?.id; componentLatest[name] = sortedVersions && sortedVersions?.length === 0; }); setVersionSelectOptions(versionSelect); setSelectedVersion(versionSelectedOption); setSelectedComponentLatest(componentLatest); }, [rowsToAdd, setVersionSelectOptions, setSelectedVersion, setSelectedComponentLatest]); const bulkAddParams = () => rowsToAdd.map((row) => { const { componentCvId: id, componentCvName: name } = row; if (selectedComponentLatest[name]) { return { latest: true, content_view_id: id }; } return { content_view_version_id: selectedVersion[name] }; }); const onSubmit = () => { dispatch(addComponent({ compositeContentViewId: cvId, components: bulkAddParams(), })); onClose(); }; return (
{ e.preventDefault(); onSubmit(); }} > {Object.keys(versionSelectOptions).sort().map(componentCvName => ( {componentCvName} setSelectedVersion({ ...selectedVersion, ...{ [componentCvName]: value } })} id={`horzontal-form-title-${componentCvName}`} name="horizontal-form-title" aria-label={`version-select-${componentCvName}`} > {versionSelectOptions[componentCvName].map((version, index) => ( // eslint-disable-next-line react/no-array-index-key ))} setSelectedComponentLatest({ ...selectedComponentLatest, ...{ [componentCvName]: checked }, }) } /> ))}
); }; ComponentContentViewBulkAddModal.propTypes = { cvId: PropTypes.oneOfType([ PropTypes.number, PropTypes.string, ]).isRequired, rowsToAdd: PropTypes.arrayOf(PropTypes.shape({})), onClose: PropTypes.func, }; ComponentContentViewBulkAddModal.defaultProps = { rowsToAdd: [], onClose: null, }; export default ComponentContentViewBulkAddModal;