2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-08 05:52:17 +00:00
|
|
|
import ContentTypeDropdown from '../dropdowns/content-type-dropdown';
|
|
|
|
import MethodDropdown from '../dropdowns/method-dropdown';
|
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
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';
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class RequestCreateModal extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
this.state = {
|
|
|
|
selectedContentType: null,
|
|
|
|
selectedMethod: METHOD_GET,
|
2017-03-03 20:09:08 +00:00
|
|
|
parentId: null
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setInputRef (n) {
|
|
|
|
this._input = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handleSubmit (e) {
|
2016-11-27 21:42:38 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const {parentId, selectedContentType, selectedMethod} = this.state;
|
|
|
|
const request = models.initModel(models.request.type, {
|
|
|
|
parentId,
|
|
|
|
name: this._input.value,
|
2017-03-03 20:09:08 +00:00
|
|
|
method: selectedMethod
|
2016-11-27 21:42:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleChangeSelectedContentType (selectedContentType) {
|
2016-11-27 21:42:38 +00:00
|
|
|
this.setState({selectedContentType});
|
|
|
|
trackEvent('Request Create', 'Content Type Change', selectedContentType);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleChangeSelectedMethod (selectedMethod) {
|
2016-11-27 21:42:38 +00:00
|
|
|
this.setState({selectedMethod});
|
|
|
|
trackEvent('Request Create', 'Method Change', selectedMethod);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
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();
|
2017-03-28 22:45:23 +00:00
|
|
|
}, 200);
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
this._onSubmitCallback = resolve;
|
|
|
|
});
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {selectedContentType, selectedMethod} = this.state;
|
|
|
|
|
|
|
|
return (
|
2017-02-28 21:32:23 +00:00
|
|
|
<Modal ref={this._setModalRef}>
|
2016-11-29 20:55:31 +00:00
|
|
|
<ModalHeader>New Request</ModalHeader>
|
2017-03-01 21:15:56 +00:00
|
|
|
<ModalBody noScroll>
|
2016-12-21 23:37:48 +00:00
|
|
|
<form onSubmit={this._handleSubmit} className="pad">
|
2017-03-28 22:45:23 +00:00
|
|
|
<div className="form-row">
|
2016-11-29 20:55:31 +00:00
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<label>Name
|
2017-02-28 21:32:23 +00:00
|
|
|
<input ref={this._setInputRef} type="text"/>
|
2016-11-29 20:55:31 +00:00
|
|
|
</label>
|
2016-11-27 21:42:38 +00:00
|
|
|
</div>
|
2017-03-28 22:45:23 +00:00
|
|
|
<div className="form-control form-control--no-label" style={{width: 'auto'}}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<MethodDropdown
|
2017-03-01 21:15:56 +00:00
|
|
|
right
|
2016-12-21 23:37:48 +00:00
|
|
|
className="btn btn--clicky no-wrap"
|
|
|
|
method={selectedMethod}
|
|
|
|
onChange={this._handleChangeSelectedMethod}
|
|
|
|
/>
|
2016-11-29 20:55:31 +00:00
|
|
|
</div>
|
2017-05-17 19:02:09 +00:00
|
|
|
{!this._shouldNotHaveBody() && (
|
2017-03-28 22:45:23 +00:00
|
|
|
<div className="form-control form-control--no-label" style={{width: 'auto'}}>
|
|
|
|
<ContentTypeDropdown className="btn btn--clicky no-wrap"
|
|
|
|
right
|
|
|
|
contentType={selectedContentType}
|
2017-04-07 18:10:15 +00:00
|
|
|
request={null}
|
2017-03-28 22:45:23 +00:00
|
|
|
onChange={this._handleChangeSelectedContentType}>
|
|
|
|
{getContentTypeName(selectedContentType) || 'No Body'}
|
|
|
|
{' '}
|
|
|
|
<i className="fa fa-caret-down"></i>
|
|
|
|
</ContentTypeDropdown>
|
|
|
|
</div>
|
2017-05-17 19:02:09 +00:00
|
|
|
)}
|
2016-11-29 20:55:31 +00:00
|
|
|
</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>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestCreateModal.propTypes = {};
|
|
|
|
|
|
|
|
export default RequestCreateModal;
|