import React, { useState } from 'react';
import {
Card,
CardHeader,
CardTitle,
CardBody,
Dropdown,
DropdownItem,
Flex,
FlexItem,
GridItem,
KebabToggle,
Label,
Tooltip,
} from '@patternfly/react-core';
import { FormattedMessage } from 'react-intl';
import { urlBuilder } from 'foremanReact/common/urlHelpers';
import { translate as __ } from 'foremanReact/common/I18n';
import { propsToCamelCase } from 'foremanReact/common/helpers';
import PropTypes from 'prop-types';
import ContentViewIcon from '../../../../../scenes/ContentViews/components/ContentViewIcon';
import { hasRequiredPermissions, hostIsRegistered } from '../../hostDetailsHelpers';
import ChangeHostCVModal from './ChangeHostCVModal';
const requiredPermissions = [
'view_lifecycle_environments', 'view_content_views',
'promote_or_remove_content_views_to_environments',
];
const HostContentViewDetails = ({
contentView, lifecycleEnvironment, contentViewVersionId, contentViewDefault,
contentViewVersion, contentViewVersionLatest, hostId, hostName, orgId, hostEnvId,
hostPermissions, permissions, contentSourceId,
}) => {
let versionLabel = `Version ${contentViewVersion}`;
if (contentViewVersionLatest) {
versionLabel += ' (latest)';
}
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const toggleHamburger = () => setIsDropdownOpen(prev => !prev);
const [isModalOpen, setIsModalOpen] = useState(false);
const closeModal = () => setIsModalOpen(false);
const openModal = () => {
setIsDropdownOpen(false);
setIsModalOpen(true);
};
const userPermissions = { ...hostPermissions, ...permissions };
const showKebab = hasRequiredPermissions(requiredPermissions, userPermissions);
const dropdownItems = [
{__('Edit content view assignment')}
,
];
return (
{__('Content view details')}
{showKebab && (
}
isOpen={isDropdownOpen}
isPlain
ouiaId="change-host-content-view-kebab"
position="right"
dropdownItems={dropdownItems}
/>
)}
{__('Content view')}
{contentView.name}
}
>
{!contentViewDefault &&
{__('Version in use')}
{versionLabel}
}
{hostId &&
}
);
};
const ContentViewDetailsCard = ({ hostDetails }) => {
if (hostIsRegistered({ hostDetails })
&& hostDetails.content_facet_attributes && hostDetails.organization_id) {
return ();
}
return null;
};
HostContentViewDetails.propTypes = {
contentView: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
composite: PropTypes.bool,
}).isRequired,
contentViewDefault: PropTypes.bool,
lifecycleEnvironment: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
}).isRequired,
contentViewVersionId: PropTypes.number.isRequired,
contentViewVersion: PropTypes.string.isRequired,
contentViewVersionLatest: PropTypes.bool.isRequired,
id: PropTypes.number,
name: PropTypes.string,
hostId: PropTypes.number,
hostName: PropTypes.string,
contentSourceId: PropTypes.number,
orgId: PropTypes.number,
hostEnvId: PropTypes.number,
hostPermissions: PropTypes.shape({
edit_hosts: PropTypes.bool,
}),
permissions: PropTypes.shape({
view_content_views: PropTypes.bool,
view_lifecycle_environments: PropTypes.bool,
promote_or_remove_content_views_to_environments: PropTypes.bool,
}),
};
HostContentViewDetails.defaultProps = {
id: null,
name: '',
hostEnvId: null,
hostId: null,
hostName: '',
orgId: null,
contentSourceId: null,
contentViewDefault: false,
hostPermissions: {},
permissions: {},
};
ContentViewDetailsCard.propTypes = {
hostDetails: PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string,
organization_id: PropTypes.number,
content_facet_attributes: PropTypes.shape({
lifecycle_environment_id: PropTypes.number,
content_source: PropTypes.shape({
id: PropTypes.number,
}),
permissions: PropTypes.shape({
view_content_views: PropTypes.bool,
view_lifecycle_environments: PropTypes.bool,
promote_or_remove_content_views_to_environments: PropTypes.bool,
}),
}),
permissions: PropTypes.shape({
edit_hosts: PropTypes.bool,
}),
}),
};
ContentViewDetailsCard.defaultProps = {
hostDetails: {},
};
export default ContentViewDetailsCard;