import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { head } from 'lodash';
import {
Split,
SplitItem,
Grid,
GridItem,
TextContent,
Text,
TextVariants,
Label,
Dropdown,
DropdownItem,
KebabToggle,
DropdownPosition,
Flex,
FlexItem,
} from '@patternfly/react-core';
import { translate as __ } from 'foremanReact/common/I18n';
import { useDispatch } from 'react-redux';
import {
getCVFilterDetails,
editCVFilter,
deleteContentViewFilter,
} from '../ContentViewDetailActions';
import AffectedRepositorySelection from './AffectedRepositories/AffectedRepositorySelection';
import RepoIcon from '../Repositories/RepoIcon';
import { repoType } from '../../../../utils/helpers';
import { hasPermission } from '../../helpers';
import { typeName } from './ContentType';
import ActionableDetail from '../../../../components/ActionableDetail';
import { ArtifactsWithNoErrataRenderer } from './ArtifactsWithNoErrata';
const ContentViewFilterDetailsHeader = ({
cvId, filterId, filterDetails, setShowAffectedRepos, details,
}) => {
const dispatch = useDispatch();
const { push } = useHistory();
const [currentAttribute, setCurrentAttribute] = useState('');
const [loading, setLoading] = useState(false);
const [dropDownOpen, setDropdownOpen] = useState(false);
const {
type, name, inclusion, description, rules,
} = filterDetails;
const { permissions } = details;
const errataByDate = !!(type === 'erratum' && head(rules)?.types);
const repositoryType = repoType(type);
const displayedType = () => typeName(type, errataByDate);
const onEdit = async (val, attribute) => {
if (val === filterDetails[attribute]) return;
setLoading(true);
await dispatch(editCVFilter(
filterId,
{ [attribute]: val },
() => {
setLoading(false);
dispatch(getCVFilterDetails(cvId, filterId));
},
() => setLoading(false),
));
};
const dropDownItems = [
{
dispatch(deleteContentViewFilter(filterId, () => {
push(`/content_views/${cvId}#/filters/`);
}));
}}
>
{__('Delete')}
,
];
const showArtifactsWithNoErrata = (type === 'rpm' || type === 'modulemd');
return (
{showArtifactsWithNoErrata &&
}
}
isOpen={dropDownOpen}
isPlain
dropdownItems={dropDownItems}
/>
{displayedType()}
);
};
ContentViewFilterDetailsHeader.propTypes = {
cvId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
filterId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
filterDetails: PropTypes.shape({
name: PropTypes.string,
type: PropTypes.string,
inclusion: PropTypes.bool,
description: PropTypes.string,
rules: PropTypes.arrayOf(PropTypes.shape({ types: PropTypes.arrayOf(PropTypes.string) })),
}).isRequired,
setShowAffectedRepos: PropTypes.func.isRequired,
details: PropTypes.shape({
permissions: PropTypes.shape({}),
}).isRequired,
};
ContentViewFilterDetailsHeader.defaultProps = {
cvId: '',
filterId: '',
};
export default ContentViewFilterDetailsHeader;