2017-05-11 20:16:52 +03:00
|
|
|
import React, { Component } from 'react';
|
2017-05-19 00:04:29 +03:00
|
|
|
import { compose, graphql } from 'react-apollo';
|
2017-06-01 12:28:59 +03:00
|
|
|
import { connect } from 'react-redux';
|
2017-05-19 00:04:29 +03:00
|
|
|
import styled from 'styled-components';
|
2017-05-11 20:16:52 +03:00
|
|
|
import ServicesQuery from '@graphql/Services.gql';
|
2017-06-19 15:10:57 +03:00
|
|
|
import ServicesRestartMutation from '@graphql/ServicesRestartMutation.gql';
|
|
|
|
import ServicesStopMutation from '@graphql/ServicesStopMutation.gql';
|
|
|
|
import ServicesStartMutation from '@graphql/ServicesStartMutation.gql';
|
2017-02-28 20:34:48 +02:00
|
|
|
|
2017-05-19 00:04:29 +03:00
|
|
|
import { processServices } from '@root/state/selectors';
|
2017-06-01 12:28:59 +03:00
|
|
|
import { toggleServicesQuickActions } from '@root/state/actions';
|
2017-05-19 00:04:29 +03:00
|
|
|
|
|
|
|
import { LayoutContainer } from '@components/layout';
|
2017-05-22 20:13:24 +03:00
|
|
|
import { Loader, ErrorMessage } from '@components/messaging';
|
2017-05-19 00:04:29 +03:00
|
|
|
import { ServiceListItem } from '@components/services';
|
|
|
|
|
2017-06-16 17:12:28 +03:00
|
|
|
import { ServicesQuickActions } from '@components/services';
|
|
|
|
|
2017-05-19 00:04:29 +03:00
|
|
|
const StyledContainer = styled.div`
|
|
|
|
position: relative;
|
|
|
|
`;
|
|
|
|
|
2017-05-11 20:16:52 +03:00
|
|
|
class ServiceList extends Component {
|
2017-06-16 17:12:28 +03:00
|
|
|
ref(name) {
|
|
|
|
this._refs = this._refs || {};
|
|
|
|
|
|
|
|
return el => {
|
|
|
|
this._refs[name] = el;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:17:47 +02:00
|
|
|
render() {
|
2017-06-01 12:28:59 +03:00
|
|
|
const {
|
|
|
|
deploymentGroup,
|
|
|
|
services,
|
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
servicesQuickActions,
|
2017-06-16 17:12:28 +03:00
|
|
|
toggleServicesQuickActions,
|
2017-06-19 15:10:57 +03:00
|
|
|
url,
|
|
|
|
restartServices,
|
|
|
|
stopServices,
|
|
|
|
startServices
|
2017-06-01 12:28:59 +03:00
|
|
|
} = this.props;
|
2017-05-11 20:16:52 +03:00
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
if (loading) {
|
2017-05-22 20:13:24 +03:00
|
|
|
return (
|
|
|
|
<LayoutContainer>
|
|
|
|
<Loader />
|
|
|
|
</LayoutContainer>
|
2017-05-18 21:21:33 +03:00
|
|
|
);
|
|
|
|
} else if (error) {
|
2017-05-19 00:04:29 +03:00
|
|
|
return (
|
2017-05-22 20:13:24 +03:00
|
|
|
<LayoutContainer>
|
2017-05-18 21:21:33 +03:00
|
|
|
<ErrorMessage message="Oops, and error occured while loading your services." />
|
2017-05-22 20:13:24 +03:00
|
|
|
</LayoutContainer>
|
2017-05-18 21:21:33 +03:00
|
|
|
);
|
2017-05-19 00:04:29 +03:00
|
|
|
}
|
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const handleQuickActionsClick = (evt, service) => {
|
2017-06-16 17:12:28 +03:00
|
|
|
const list = this._refs.container;
|
|
|
|
const listRect = list.getBoundingClientRect();
|
|
|
|
const button = evt.currentTarget;
|
|
|
|
const buttonRect = button.getBoundingClientRect();
|
|
|
|
|
|
|
|
const position = {
|
2017-06-17 01:03:35 +03:00
|
|
|
left:
|
|
|
|
buttonRect.left -
|
|
|
|
listRect.left +
|
|
|
|
(buttonRect.right - buttonRect.left) / 2,
|
2017-06-16 17:12:28 +03:00
|
|
|
top: buttonRect.bottom - listRect.top
|
|
|
|
};
|
|
|
|
|
|
|
|
toggleServicesQuickActions({
|
|
|
|
service,
|
|
|
|
position
|
|
|
|
});
|
2017-06-01 12:28:59 +03:00
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const handleRestartClick = (evt, service) => {
|
|
|
|
restartServices(service.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleStopClick = (evt, service) => {
|
|
|
|
stopServices(service.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleStartClick = (evt, service) => {
|
|
|
|
startServices(service.id);
|
|
|
|
};
|
|
|
|
|
2017-06-01 12:28:59 +03:00
|
|
|
const handleQuickActionsBlur = o => {
|
2017-06-16 17:12:28 +03:00
|
|
|
toggleServicesQuickActions({ show: false });
|
2017-06-01 12:28:59 +03:00
|
|
|
};
|
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const serviceList = services.map(service => {
|
|
|
|
return (
|
|
|
|
<ServiceListItem
|
|
|
|
key={service.id}
|
|
|
|
deploymentGroup={deploymentGroup.slug}
|
|
|
|
service={service}
|
|
|
|
onQuickActionsClick={handleQuickActionsClick}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2017-03-22 18:50:21 +02:00
|
|
|
|
2017-03-09 17:17:47 +02:00
|
|
|
return (
|
2017-05-19 00:04:29 +03:00
|
|
|
<LayoutContainer>
|
|
|
|
<StyledContainer>
|
2017-06-16 17:12:28 +03:00
|
|
|
<div ref={this.ref('container')}>
|
2017-05-19 00:04:29 +03:00
|
|
|
{serviceList}
|
2017-06-16 17:12:28 +03:00
|
|
|
<ServicesQuickActions
|
|
|
|
position={servicesQuickActions.position}
|
|
|
|
service={servicesQuickActions.service}
|
|
|
|
show={servicesQuickActions.show}
|
|
|
|
url={url}
|
|
|
|
onBlur={handleQuickActionsBlur}
|
2017-06-19 15:10:57 +03:00
|
|
|
onRestartClick={handleRestartClick}
|
|
|
|
onStopClick={handleStopClick}
|
|
|
|
onStartClick={handleStartClick}
|
2017-06-16 17:12:28 +03:00
|
|
|
/>
|
2017-05-19 00:04:29 +03:00
|
|
|
</div>
|
|
|
|
</StyledContainer>
|
|
|
|
</LayoutContainer>
|
2017-03-09 17:17:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 20:34:48 +02:00
|
|
|
|
2017-06-01 12:28:59 +03:00
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
2017-06-16 17:12:28 +03:00
|
|
|
servicesQuickActions: state.ui.services.quickActions,
|
|
|
|
url: ownProps.match.url
|
2017-06-01 12:28:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
toggleServicesQuickActions: data => dispatch(toggleServicesQuickActions(data))
|
|
|
|
});
|
|
|
|
|
|
|
|
const UiConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
2017-05-19 00:04:29 +03:00
|
|
|
const ServicesGql = graphql(ServicesQuery, {
|
2017-05-11 20:16:52 +03:00
|
|
|
options(props) {
|
|
|
|
return {
|
2017-06-22 20:09:13 +03:00
|
|
|
pollInterval: 1000,
|
2017-05-11 20:16:52 +03:00
|
|
|
variables: {
|
2017-05-17 21:02:35 +03:00
|
|
|
deploymentGroupSlug: props.match.params.deploymentGroup
|
2017-05-11 20:16:52 +03:00
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
};
|
2017-02-28 20:34:48 +02:00
|
|
|
},
|
2017-05-18 21:21:33 +03:00
|
|
|
props: ({ data: { deploymentGroup, loading, error } }) => ({
|
2017-05-19 00:04:29 +03:00
|
|
|
deploymentGroup,
|
2017-05-18 21:21:33 +03:00
|
|
|
services: deploymentGroup
|
|
|
|
? processServices(deploymentGroup.services, null)
|
|
|
|
: null,
|
2017-05-11 20:16:52 +03:00
|
|
|
loading,
|
|
|
|
error
|
|
|
|
})
|
2017-05-19 00:04:29 +03:00
|
|
|
});
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const ServicesRestartGql = graphql(ServicesRestartMutation, {
|
|
|
|
props: ({ mutate }) => ({
|
|
|
|
restartServices: serviceId => mutate({ variables: { ids: [serviceId] } })
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const ServicesStopGql = graphql(ServicesStopMutation, {
|
|
|
|
props: ({ mutate }) => ({
|
|
|
|
stopServices: serviceId => mutate({ variables: { ids: [serviceId] } })
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const ServicesStartGql = graphql(ServicesStartMutation, {
|
|
|
|
props: ({ mutate }) => ({
|
|
|
|
startServices: serviceId => mutate({ variables: { ids: [serviceId] } })
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const ServiceListWithData = compose(
|
|
|
|
ServicesGql,
|
2017-06-23 23:22:06 +03:00
|
|
|
ServicesRestartGql,
|
2017-06-19 15:10:57 +03:00
|
|
|
ServicesStopGql,
|
|
|
|
ServicesStartGql,
|
|
|
|
ServicesGql,
|
|
|
|
UiConnect
|
|
|
|
)(ServiceList);
|
2017-05-11 20:16:52 +03:00
|
|
|
|
|
|
|
export default ServiceListWithData;
|