2018-06-25 17:42:50 +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';
|
2018-06-25 17:42:50 +00:00
|
|
|
import {
|
|
|
|
getContentTypeName,
|
|
|
|
METHOD_GET,
|
|
|
|
METHOD_HEAD,
|
|
|
|
METHOD_OPTIONS,
|
2018-12-12 17:36:11 +00:00
|
|
|
METHOD_DELETE,
|
2018-06-25 17:42:50 +00:00
|
|
|
} from '../../../common/constants';
|
2016-11-27 21:42:38 +00:00
|
|
|
import * as models from '../../../models/index';
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class RequestCreateModal extends PureComponent {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-03-03 01:44:07 +00:00
|
|
|
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,
|
2018-12-12 17:36:11 +00:00
|
|
|
parentId: null,
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_setModalRef(n) {
|
2017-03-03 01:44:07 +00:00
|
|
|
this.modal = n;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_setInputRef(n) {
|
2017-03-03 01:44:07 +00:00
|
|
|
this._input = n;
|
2017-08-12 16:19:43 +00:00
|
|
|
if (this._input) {
|
|
|
|
this._input.value = 'My Request';
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async _handleSubmit(e) {
|
2016-11-27 21:42:38 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const { parentId, selectedContentType, selectedMethod } = this.state;
|
2017-09-13 06:11:49 +00:00
|
|
|
const request = await models.initModel(models.request.type, {
|
2016-11-27 21:42:38 +00:00
|
|
|
parentId,
|
|
|
|
name: this._input.value,
|
2018-12-12 17:36:11 +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,
|
2018-12-12 17:36:11 +00:00
|
|
|
true,
|
2016-11-27 21:42:38 +00:00
|
|
|
);
|
|
|
|
|
2017-06-09 21:42:19 +00:00
|
|
|
this._onComplete(finalRequest);
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
this.hide();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleChangeSelectedContentType(selectedContentType) {
|
|
|
|
this.setState({ selectedContentType });
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleChangeSelectedMethod(selectedMethod) {
|
|
|
|
this.setState({ selectedMethod });
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_shouldNotHaveBody() {
|
|
|
|
const { selectedMethod } = this.state;
|
2016-11-28 07:12:17 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
hide() {
|
2016-11-27 21:42:38 +00:00
|
|
|
this.modal.hide();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
show({ parentId, onComplete }) {
|
2016-11-29 20:55:31 +00:00
|
|
|
this.setState({
|
|
|
|
parentId,
|
|
|
|
selectedContentType: null,
|
2018-12-12 17:36:11 +00:00
|
|
|
selectedMethod: METHOD_GET,
|
2016-11-29 20:55:31 +00:00
|
|
|
});
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-08-12 21:56:24 +00:00
|
|
|
this.modal.show();
|
|
|
|
|
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-06-09 21:42:19 +00:00
|
|
|
this._onComplete = onComplete;
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { selectedContentType, selectedMethod } = this.state;
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
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">
|
2018-06-25 17:42:50 +00:00
|
|
|
<label>
|
|
|
|
Name
|
|
|
|
<input ref={this._setInputRef} type="text" />
|
2016-11-29 20:55:31 +00:00
|
|
|
</label>
|
2016-11-27 21:42:38 +00:00
|
|
|
</div>
|
2018-10-17 16:42:33 +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() && (
|
2018-10-17 16:42:33 +00:00
|
|
|
<div className="form-control form-control--no-label" style={{ width: 'auto' }}>
|
2018-06-25 17:42:50 +00:00
|
|
|
<ContentTypeDropdown
|
|
|
|
className="btn btn--clicky no-wrap"
|
|
|
|
right
|
|
|
|
contentType={selectedContentType}
|
|
|
|
request={null}
|
|
|
|
onChange={this._handleChangeSelectedContentType}>
|
|
|
|
{getContentTypeName(selectedContentType) || 'No Body'}{' '}
|
|
|
|
<i className="fa fa-caret-down" />
|
2017-03-28 22:45:23 +00:00
|
|
|
</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>
|
2017-06-29 18:27:29 +00:00
|
|
|
<div className="margin-left italic txt-sm tall">
|
|
|
|
* Tip: paste Curl command into URL afterwards to import it
|
|
|
|
</div>
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RequestCreateModal;
|