import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { Flex, Modal, ModalVariant, Select, SelectVariant, SelectOption, 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 getContentViewDetails, { addComponent } from '../ContentViewDetailActions'; import { truncate } from '../../../../utils/helpers'; const ComponentContentViewBulkAddModal = ({ cvId, rowsToAdd, onClose }) => { const dispatch = useDispatch(); const versionSelect = {}; const versionSelectedOption = {}; const componentLatest = {}; rowsToAdd.forEach((row) => { const { componentCvVersions: versions, componentCvName: name } = row; versionSelect[name] = versions; versionSelectedOption[name] = versions[0]?.id; componentLatest[name] = versions && versions?.length === 0; }); const [selectedVersion, setSelectedVersion] = useState(versionSelectedOption); const [selectedComponentLatest, setSelectedComponentLatest] = useState(componentLatest); const [cvVersionSelectOpen, setCvVersionSelectOpen] = useState(''); 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(), }, () => dispatch(getContentViewDetails(cvId)))); onClose(); }; return (
{ e.preventDefault(); onSubmit(); }} > {Object.keys(versionSelect).sort().map(componentCvName => ( {truncate(componentCvName)} { setSelectedComponentLatest({ ...selectedComponentLatest, ...{ [componentCvName]: checked }, }); setSelectedVersion({ ...selectedVersion, ...{ [componentCvName]: versionSelect[componentCvName][0]?.id }, }); } } /> ))}
); }; 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;