import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import forceArray from 'force-array';
import sortBy from 'lodash.sortby';
import { isNot } from 'styled-is';
import { InstancesIcon, HealthyIcon } from 'joyent-ui-toolkit';
import Status from './status';
import {
Card,
CardView,
CardTitle,
CardSubTitle,
CardDescription,
CardGroupView,
CardOptions,
CardHeader,
CardInfo,
Anchor
} from 'joyent-ui-toolkit';
const StyledCardHeader = styled(CardHeader)`
position: relative;
`;
const TitleInnerContainer = styled.div`
display: flex;
flex-direction: row;
justify-content: left;
align-items: center;
`;
const StyledAnchor = styled(Anchor)`
${isNot('active')`
color: ${props => props.theme.text}
`};
`;
const ServiceListItem = ({
onQuickActionsClick = () => {},
deploymentGroup = '',
service,
isChild = false
}) => {
const handleCardOptionsClick = evt => {
onQuickActionsClick(evt, service);
};
const children = sortBy(forceArray(service.children), ['slug']);
// const isServiceInactive = service.status && service.status !== 'ACTIVE';
const to = `/deployment-groups/${deploymentGroup}/services/${service.slug}`;
const instancesCount = children.length
? children.reduce((count, child) => count + child.instances.length, 0)
: service.instances.length;
const childrenItems = children.length
? children.map(service => (
))
: null;
const title = isChild ? (
{service.name}
) : (
{service.name}
);
const subtitle = (
{service.instances.length}{' '}
{service.instances.length > 1 ? 'instances' : 'instance'}
);
const header = !isChild ? (
{title}
}
iconPosition="left"
label={`${instancesCount} ${instancesCount > 1
? 'instances'
: 'instance'}`}
color={!service.instancesActive ? 'disabled' : 'light'}
/>
) : null;
let healthyInfo = null;
if (service.instancesActive) {
const { total, healthy } = service.instancesHealthy;
const iconHealthy = total === healthy ? 'HEALTHY' : 'NOT HEALTHY';
const icon = ;
const label = `${healthy} of ${total} healthy`;
healthyInfo = (
);
}
const view = children.length ? (
{childrenItems}
) : (
{isChild && title}
{isChild && subtitle}
{healthyInfo}
);
return (
1}
>
{header}
{view}
);
};
ServiceListItem.propTypes = {
onQuickActionsClick: PropTypes.func,
deploymentGroup: PropTypes.string,
service: PropTypes.object.isRequired // Define better
};
export default ServiceListItem;