insomnia/app/ui/components/modals/request-create-modal.js
Gregory Schier d675222bdd Merge develop for 5.1.0 (#246)
* Add Slack badge to README

* Add Twitter badge

* Appveyor badge

* Fix badges again on README

* Fix Twitter badge link

* Simplify README.md

* Migrate Travis secure items to Travis project settings (#198)

* Remove docker linux build (using Travis now) (#199)

* Fix travis build

* Update Issue and PR templates (#200)

* Add template for future pull requests

* Format issue template like pull request template

* Will not clear selected file if dialog is dismissed (#202)

* #183, Body type "Text -> Other" reverts to previous body type (#193)

* ISSUE#183

* Adding condition to check mime-type to other

* Removing older changes for fixing issue.

* Save full response to a file (#207)

* Save full response to a file

* Add a new button on the response preview pane
* Save full response to file when button clicked

* Update after PR comments

* It's a Response, not a Request

* Remove file extension requirement

* Implement lazy tag rendering and some fixes (#211)

* expanding to 3 decimals (#213)

* Update PULL_REQUEST_TEMPLATE.md (#214)

* Show build info in console (#216)

* Add waiting message in dev mode while webpack compile happens

* Switch license from GPL to AGPL (#221)

* Default remote URLs to production

* Don't use Curl's cookie handling (#220)

* Some improvements to the response tag

* Add tests for XPath response queries

* Refactor conditional element syntax

* Add option to toggle Menu Bar showing for Windows/Linux (#225)

* Add option to toggle MenuBar showing on Windows/Linux

* Extract Toggling Menu Bar functionality to App Container. Default show Menu Bar. Remove tip from Response Pane.

* Finalize {% response ... %} Tag (#224)

* Some improvements to the response tag

* Add tests for XPath response queries

* Refactor conditional element syntax

* Update nunjucks-tags.js

* Better Nunjucks Tag Editor (#234)

* Helper to tokenize Nunjucks tag

* More granular types

* Add tag definitions

* Improve editor to be more WYSIWYG

* Fixed tests

* Added raw response tag

* A few improvements to tag editor enum

* fix NTLM typo (#244)

* Tweaks and fixes for next release (#245)
2017-05-24 09:25:22 -07:00

150 lines
4.3 KiB
JavaScript

import React, {PureComponent} from 'react';
import autobind from 'autobind-decorator';
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';
import {getContentTypeName, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_DELETE} from '../../../common/constants';
import * as models from '../../../models/index';
import {trackEvent} from '../../../analytics/index';
@autobind
class RequestCreateModal extends PureComponent {
constructor (props) {
super(props);
this.state = {
selectedContentType: null,
selectedMethod: METHOD_GET,
parentId: null
};
}
_setModalRef (n) {
this.modal = n;
}
_setInputRef (n) {
this._input = n;
}
async _handleSubmit (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,
this._shouldNotHaveBody() ? null : selectedContentType,
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);
}
_shouldNotHaveBody () {
const {selectedMethod} = this.state;
return (
selectedMethod === METHOD_GET ||
selectedMethod === METHOD_HEAD ||
selectedMethod === METHOD_DELETE ||
selectedMethod === METHOD_OPTIONS
);
}
hide () {
this.modal.hide();
}
show ({parentId}) {
this.modal.show();
this._input.value = 'My Request';
this.setState({
parentId,
selectedContentType: null,
selectedMethod: METHOD_GET
});
// Need to do this after render because modal focuses itself too
setTimeout(() => {
this._input.focus();
this._input.select();
}, 200);
return new Promise(resolve => {
this._onSubmitCallback = resolve;
});
}
render () {
const {selectedContentType, selectedMethod} = this.state;
return (
<Modal ref={this._setModalRef}>
<ModalHeader>New Request</ModalHeader>
<ModalBody noScroll>
<form onSubmit={this._handleSubmit} className="pad">
<div className="form-row">
<div className="form-control form-control--outlined">
<label>Name
<input ref={this._setInputRef} type="text"/>
</label>
</div>
<div className="form-control form-control--no-label" style={{width: 'auto'}}>
<MethodDropdown
right
className="btn btn--clicky no-wrap"
method={selectedMethod}
onChange={this._handleChangeSelectedMethod}
/>
</div>
{!this._shouldNotHaveBody() && (
<div className="form-control form-control--no-label" style={{width: 'auto'}}>
<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"></i>
</ContentTypeDropdown>
</div>
)}
</div>
</form>
</ModalBody>
<ModalFooter>
<button className="btn" onClick={this._handleSubmit}>
Create
</button>
</ModalFooter>
</Modal>
);
}
}
RequestCreateModal.propTypes = {};
export default RequestCreateModal;