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 { useUrlParams } from 'foremanReact/components/PF4/TableIndexPage/Table/TableHooks';
import PropTypes from 'prop-types';
import ContentViewIcon from '../../../../../scenes/ContentViews/components/ContentViewIcon';
import { hasRequiredPermissions, hostIsRegistered } from '../../hostDetailsHelpers';
import ChangeHostCVModal from './ChangeHostCVModal';
import { truncate } from '../../../../../utils/helpers';
const requiredPermissions = [
'view_lifecycle_environments', 'view_content_views',
'promote_or_remove_content_views_to_environments',
];
export const ContentViewEnvironmentDisplay = ({
contentView, lifecycleEnvironment,
}) => {
const {
contentViewDefault,
contentViewVersionId,
contentViewVersion,
contentViewVersionLatest,
} = propsToCamelCase(contentView);
let versionLabel = 'Version {version}';
if (contentViewVersionLatest) {
versionLabel += ' (latest)';
}
return (
}
>
{contentViewDefault ? {contentView.name} :
{truncate(contentView.name)}
}
{!contentViewDefault &&
}
);
};
ContentViewEnvironmentDisplay.propTypes = {
contentView: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
composite: PropTypes.bool,
}).isRequired,
lifecycleEnvironment: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
}).isRequired,
};
const HostContentViewDetails = ({
contentViewEnvironments, hostId, hostName, orgId, hostEnvId,
hostPermissions, permissions, contentSourceId,
}) => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const toggleHamburger = () => setIsDropdownOpen(prev => !prev);
const { content_view_assignment: initialCVModalOpen } = useUrlParams();
const [isModalOpen, setIsModalOpen] = useState(!!initialCVModalOpen);
const closeModal = () => setIsModalOpen(false);
const openModal = () => {
setIsDropdownOpen(false);
setIsModalOpen(true);
};
const userPermissions = { ...hostPermissions, ...permissions };
const showKebab = hasRequiredPermissions(requiredPermissions, userPermissions);
const dropdownItems = [
{__('Edit content view environments')}
,
];
return (
{showKebab && (
}
isOpen={isDropdownOpen}
isPlain
ouiaId="change-host-content-view-kebab"
position="right"
dropdownItems={dropdownItems}
/>
)}
{contentViewEnvironments.map(env => (
))}
{hostId &&
1}
key={`cv-change-modal-${hostId}`}
/>
}
);
};
HostContentViewDetails.propTypes = {
contentViewEnvironments: PropTypes.arrayOf(PropTypes.shape({
content_view: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
composite: PropTypes.bool,
}),
lifecycle_environment: PropTypes.shape({
name: PropTypes.string,
id: PropTypes.number,
}),
})),
hostId: PropTypes.number,
hostName: PropTypes.string,
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,
}),
contentSourceId: PropTypes.number,
};
HostContentViewDetails.defaultProps = {
contentViewEnvironments: [],
hostId: null,
hostName: '',
orgId: null,
hostEnvId: null,
hostPermissions: {},
permissions: {},
contentSourceId: null,
};
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,
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 = {
hostEnvId: null,
hostId: null,
hostName: '',
orgId: null,
contentSourceId: null,
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;