2016-11-27 21:42:38 +00:00
|
|
|
import React, {Component} from 'react';
|
|
|
|
import ContentTypeDropdown from '../dropdowns/ContentTypeDropdown';
|
|
|
|
import MethodDropdown from '../dropdowns/MethodDropdown';
|
|
|
|
import Modal from '../base/Modal';
|
|
|
|
import ModalBody from '../base/ModalBody';
|
|
|
|
import ModalHeader from '../base/ModalHeader';
|
|
|
|
import ModalFooter from '../base/ModalFooter';
|
2016-12-01 03:54:26 +00:00
|
|
|
import {getContentTypeName, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_DELETE} from '../../../common/constants';
|
2016-11-27 21:42:38 +00:00
|
|
|
import * as models from '../../../models/index';
|
|
|
|
import {trackEvent} from '../../../analytics/index';
|
|
|
|
|
|
|
|
class RequestCreateModal extends Component {
|
2016-11-29 20:55:31 +00:00
|
|
|
state = {
|
|
|
|
selectedContentType: null,
|
|
|
|
selectedMethod: METHOD_GET,
|
|
|
|
parentId: null,
|
|
|
|
};
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
_handleSubmit = async e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const {parentId, selectedContentType, selectedMethod} = this.state;
|
|
|
|
const request = models.initModel(models.request.type, {
|
|
|
|
parentId,
|
|
|
|
name: this._input.value,
|
|
|
|
method: selectedMethod,
|
|
|
|
});
|
|
|
|
|
|
|
|
const finalRequest = await models.request.updateMimeType(
|
|
|
|
request,
|
2016-11-28 07:12:17 +00:00
|
|
|
this._shouldNotHaveBody() ? null : selectedContentType,
|
2016-11-27 21:42:38 +00:00
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
this._onSubmitCallback(finalRequest);
|
|
|
|
|
|
|
|
this.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleChangeSelectedContentType = selectedContentType => {
|
|
|
|
this.setState({selectedContentType});
|
|
|
|
trackEvent('Request Create', 'Content Type Change', selectedContentType);
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleChangeSelectedMethod = selectedMethod => {
|
|
|
|
this.setState({selectedMethod});
|
|
|
|
trackEvent('Request Create', 'Method Change', selectedMethod);
|
|
|
|
};
|
|
|
|
|
2016-11-28 07:12:17 +00:00
|
|
|
_shouldNotHaveBody () {
|
|
|
|
const {selectedMethod} = this.state;
|
|
|
|
return (
|
|
|
|
selectedMethod === METHOD_GET ||
|
|
|
|
selectedMethod === METHOD_HEAD ||
|
2016-12-01 03:54:26 +00:00
|
|
|
selectedMethod === METHOD_DELETE ||
|
2016-11-28 07:12:17 +00:00
|
|
|
selectedMethod === METHOD_OPTIONS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
show ({parentId}) {
|
|
|
|
this.modal.show();
|
|
|
|
|
|
|
|
this._input.value = 'My Request';
|
2016-11-29 20:55:31 +00:00
|
|
|
this.setState({
|
|
|
|
parentId,
|
|
|
|
selectedContentType: null,
|
|
|
|
selectedMethod: METHOD_GET
|
|
|
|
});
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
// Need to do this after render because modal focuses itself too
|
|
|
|
setTimeout(() => {
|
|
|
|
this._input.focus();
|
|
|
|
this._input.select();
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
return new Promise(resolve => this._onSubmitCallback = resolve);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {selectedContentType, selectedMethod} = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal ref={m => this.modal = m}>
|
2016-11-29 20:55:31 +00:00
|
|
|
<ModalHeader>New Request</ModalHeader>
|
2016-11-27 21:42:38 +00:00
|
|
|
<ModalBody noScroll={true}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<form onSubmit={this._handleSubmit} className="pad">
|
2016-11-29 20:55:31 +00:00
|
|
|
<div className="row-fill">
|
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<label>Name
|
|
|
|
<input ref={n => this._input = n} type="text"/>
|
|
|
|
</label>
|
2016-11-27 21:42:38 +00:00
|
|
|
</div>
|
2016-11-29 20:55:31 +00:00
|
|
|
<div className="form-control" style={{width: 'auto'}}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<label> </label>
|
|
|
|
<MethodDropdown
|
|
|
|
className="btn btn--clicky no-wrap"
|
|
|
|
right={true}
|
|
|
|
method={selectedMethod}
|
|
|
|
onChange={this._handleChangeSelectedMethod}
|
|
|
|
/>
|
2016-11-29 20:55:31 +00:00
|
|
|
</div>
|
|
|
|
{!this._shouldNotHaveBody() ? (
|
|
|
|
<div className="form-control" style={{width: 'auto'}}>
|
|
|
|
<label htmlFor="nothing">
|
|
|
|
<ContentTypeDropdown className="btn btn--clicky no-wrap"
|
|
|
|
right={true}
|
|
|
|
contentType={selectedContentType}
|
|
|
|
onChange={this._handleChangeSelectedContentType}>
|
|
|
|
{getContentTypeName(selectedContentType)}
|
|
|
|
{" "}
|
|
|
|
<i className="fa fa-caret-down"></i>
|
|
|
|
</ContentTypeDropdown>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2016-11-27 21:42:38 +00:00
|
|
|
</form>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2016-11-29 20:55:31 +00:00
|
|
|
<button className="btn" onClick={this._handleSubmit}>
|
|
|
|
Create
|
|
|
|
</button>
|
2016-11-27 21:42:38 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestCreateModal.propTypes = {};
|
|
|
|
|
|
|
|
export default RequestCreateModal;
|