import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { STATUS } from 'foremanReact/constants';
import {
Button,
Card,
CardHeader,
CardTitle,
CardBody,
DescriptionList,
DescriptionListGroup,
DescriptionListDescription,
DescriptionListTerm,
Flex,
FlexItem,
GridItem,
Label,
List,
ListItem,
Tooltip,
Skeleton,
} from '@patternfly/react-core';
import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
import { translate as __ } from 'foremanReact/common/I18n';
import { propsToCamelCase } from 'foremanReact/common/helpers';
import './SystemPurposeCard.scss';
import SystemPurposeEditModal from './SystemPurposeEditModal';
import { selectHostDetailsStatus } from '../../HostDetailsSelectors';
import { hasRequiredPermissions } from '../../hostDetailsHelpers';
const SystemPurposeCard = ({ hostDetails }) => {
const showEditButton = hasRequiredPermissions(['edit_hosts'], hostDetails?.permissions);
const orgId = hostDetails.organization_id;
const subscriptionFacetAttributes = hostDetails?.subscription_facet_attributes;
const {
purposeRole, purposeUsage, purposeAddons, releaseVersion, serviceLevel,
} = propsToCamelCase(subscriptionFacetAttributes ?? {});
const sysPurposeProps = {
purposeRole,
purposeUsage,
purposeAddons,
releaseVersion,
serviceLevel,
};
const hostDetailsStatus = useSelector(selectHostDetailsStatus);
const dataIsLoading = hostDetailsStatus === STATUS.PENDING;
const [editing, setEditing] = useState(false);
if (!hostDetails?.id) {
return (
);
}
return (
{__('System purpose')}
{showEditButton && (
)
}
{__('Role')}
{dataIsLoading ? : purposeRole}
{__('SLA')}
{serviceLevel && (dataIsLoading ? : (
))}
{__('Usage type')}
{purposeUsage && (dataIsLoading ? : (
))}
{__('Release version')}
{dataIsLoading ? : releaseVersion}
{!!purposeAddons?.length && (
<>
{__('Add-ons')}
{dataIsLoading ? : (
{purposeAddons.map(addon => (
{addon}
))}
)}
>
)
}
{showEditButton && (
setEditing(false)}
hostName={hostDetails.name}
hostId={hostDetails.id}
{...sysPurposeProps}
/>
)}
);
};
SystemPurposeCard.propTypes = {
hostDetails: PropTypes.shape({
name: PropTypes.string,
organization_id: PropTypes.number,
id: PropTypes.number,
subscription_facet_attributes: PropTypes.shape({
installed_products: PropTypes.arrayOf(PropTypes.shape({
productId: PropTypes.string,
productName: PropTypes.string,
})),
}),
permissions: PropTypes.shape({
edit_hosts: PropTypes.bool,
}),
}),
};
SystemPurposeCard.defaultProps = {
hostDetails: {},
};
export default SystemPurposeCard;