2017-06-16 17:12:28 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { compose, graphql } from 'react-apollo';
|
2017-06-19 15:10:57 +03:00
|
|
|
import { reduxForm } from 'redux-form';
|
2017-06-16 17:12:28 +03:00
|
|
|
import ServiceScaleMutation from '@graphql/ServiceScale.gql';
|
|
|
|
import { Loader, ErrorMessage } from '@components/messaging';
|
|
|
|
import { ServiceScale as ServiceScaleComponent } from '@components/service';
|
|
|
|
import { Modal } from 'joyent-ui-toolkit';
|
|
|
|
import ServiceGql from './service-gql';
|
|
|
|
|
|
|
|
class ServiceScale extends Component {
|
|
|
|
render() {
|
|
|
|
if (this.props.loading) {
|
|
|
|
return <Loader />;
|
|
|
|
}
|
|
|
|
if (this.props.error) {
|
|
|
|
return (
|
|
|
|
<ErrorMessage message="Oops, an error occured while loading your service." />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { service, scale, history, match } = this.props;
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const validateReplicas = ({ replicas }) => {
|
|
|
|
if (replicas === '') {
|
|
|
|
return {
|
|
|
|
replicas:
|
|
|
|
'Please enter the number of instances you would like to scale to.'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ServiceScaleForm = reduxForm({
|
|
|
|
form: 'scale-service',
|
|
|
|
destroyOnUnmount: true,
|
|
|
|
forceUnregisterOnUnmount: true,
|
|
|
|
validate: validateReplicas,
|
|
|
|
initialValues: {
|
|
|
|
replicas: service.instances.length
|
|
|
|
}
|
|
|
|
})(ServiceScaleComponent);
|
|
|
|
|
2017-06-16 17:12:28 +03:00
|
|
|
const handleCloseClick = evt => {
|
|
|
|
const closeUrl = match.url.split('/').slice(0, -2).join('/');
|
|
|
|
history.replace(closeUrl);
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const handleSubmitClick = values => {
|
|
|
|
scale(service.id, values.replicas);
|
2017-06-16 17:12:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal width={460} onCloseClick={handleCloseClick}>
|
2017-06-19 15:10:57 +03:00
|
|
|
<ServiceScaleForm
|
2017-06-16 17:12:28 +03:00
|
|
|
service={service}
|
2017-06-19 15:10:57 +03:00
|
|
|
onSubmit={handleSubmitClick.bind(this)}
|
|
|
|
onCancel={handleCloseClick}
|
2017-06-16 17:12:28 +03:00
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceScale.propTypes = {
|
|
|
|
service: PropTypes.object,
|
|
|
|
history: PropTypes.object,
|
|
|
|
scale: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
const ServiceScaleGql = graphql(ServiceScaleMutation, {
|
|
|
|
props: ({ mutate }) => ({
|
|
|
|
scale: (serviceId, replicas) =>
|
|
|
|
mutate({
|
|
|
|
variables: {
|
|
|
|
serviceId,
|
|
|
|
replicas
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const ServiceScaleWithData = compose(ServiceScaleGql, ServiceGql)(ServiceScale);
|
|
|
|
|
|
|
|
export default ServiceScaleWithData;
|