joyent-portal/frontend/src/containers/services/list.js

139 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-02-28 20:34:48 +02:00
import React from 'react';
import styled from 'styled-components';
2017-02-28 20:34:48 +02:00
import { connect } from 'react-redux';
import PropTypes from '@root/prop-types';
import { LayoutContainer } from '@components/layout';
2017-02-28 20:34:48 +02:00
import ServiceItem from '@components/service/item';
import UnmanagedInstances from '@components/services/unmanaged-instances';
import { toggleTooltip } from '@state/actions';
import ServicesTooltip from '@components/services/tooltip';
2017-03-17 21:27:15 +02:00
import { subscribeMetric } from '@state/thunks';
2017-02-28 20:34:48 +02:00
import {
orgByIdSelector,
projectByIdSelector,
servicesByProjectIdSelector,
serviceUiTooltipSelector
2017-02-28 20:34:48 +02:00
} from '@state/selectors';
const StyledContainer = styled.div`
position: relative;
`;
2017-03-17 21:27:15 +02:00
// TMP - single source of truth
const duration = '1 hour';
const interval = '2 minutes';
class Services extends React.Component {
2017-03-17 21:27:15 +02:00
// we DON'T want to unsubscribe once we started going
componentWillMount() {
this.props.subscribeMetric(interval);
}
ref(name) {
this._refs = this._refs || {};
return (el) => {
this._refs[name] = el;
};
}
render() {
const {
org = {},
project = {},
services = [],
2017-03-17 21:27:15 +02:00
toggleTooltip = () => ({}),
uiTooltip = {}
} = this.props;
const onQuickActions = (evt, service) => {
const list = this._refs.container;
const listRect = list.getBoundingClientRect();
const button = evt.currentTarget;
const buttonRect = button.getBoundingClientRect();
const position = {
left: buttonRect.left - listRect.left
+ (buttonRect.right - buttonRect.left)/2,
top: buttonRect.bottom - listRect.top
};
toggleTooltip({
service: service,
position: position,
data: {
serviceId: service.id,
orgId: org.id,
projectId: project.id
}
});
};
2017-03-22 18:50:21 +02:00
const handleTooltipBlur = (evt) => onQuickActions(evt, uiTooltip.service);
const serviceList = services.map((service) => (
<ServiceItem
key={service.uuid}
onQuickActions={onQuickActions}
org={org.id}
project={project.id}
service={service}
uiTooltip={uiTooltip}
/>
));
2017-03-22 18:50:21 +02:00
// TODO replace `false` with a check for existence unmanaged instances
// eslint-disable-next-line no-constant-condition
const unmanagedInstances = false ? (
<UnmanagedInstances instances={0} />
) : null;
return (
<LayoutContainer>
2017-03-22 18:50:21 +02:00
{unmanagedInstances}
<StyledContainer>
<div ref={this.ref('container')}>
{serviceList}
2017-03-22 18:50:21 +02:00
<ServicesTooltip {...uiTooltip} onBlur={handleTooltipBlur} />
</div>
</StyledContainer>
</LayoutContainer>
);
}
}
2017-02-28 20:34:48 +02:00
Services.propTypes = {
org: PropTypes.org,
project: PropTypes.project,
services: React.PropTypes.arrayOf(PropTypes.service),
toggleTooltip: React.PropTypes.func,
2017-03-17 21:27:15 +02:00
uiTooltip: React.PropTypes.object,
subscribeMetric: React.PropTypes.func
2017-02-28 20:34:48 +02:00
};
const mapStateToProps = (state, {
match = {
params: {}
},
push
}) => ({
org: orgByIdSelector(match.params.org)(state),
project: projectByIdSelector(match.params.project)(state),
services: servicesByProjectIdSelector(match.params.project, {
2017-03-17 21:27:15 +02:00
duration,
interval
})(state),
uiTooltip: serviceUiTooltipSelector(state)
});
const mapDispatchToProps = (dispatch) => ({
2017-03-17 21:27:15 +02:00
toggleTooltip: (data) => dispatch(toggleTooltip(data)),
subscribeMetric: (payload) => dispatch(subscribeMetric(payload))
2017-02-28 20:34:48 +02:00
});
export default connect(
mapStateToProps,
mapDispatchToProps
2017-02-28 20:34:48 +02:00
)(Services);