2016-11-26 00:37:59 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2016-11-28 07:12:17 +00:00
|
|
|
import {parse as urlParse} from 'url';
|
2016-10-21 17:20:36 +00:00
|
|
|
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
|
2017-02-28 06:26:24 +00:00
|
|
|
import KeyValueEditor from './keyvalueeditor/Editor';
|
2016-09-17 05:46:44 +00:00
|
|
|
import RequestHeadersEditor from './editors/RequestHeadersEditor';
|
|
|
|
import ContentTypeDropdown from './dropdowns/ContentTypeDropdown';
|
|
|
|
import RenderedQueryString from './RenderedQueryString';
|
2016-11-22 19:42:10 +00:00
|
|
|
import BodyEditor from './editors/body/BodyEditor';
|
2016-09-17 05:46:44 +00:00
|
|
|
import AuthEditor from './editors/AuthEditor';
|
2016-11-07 20:24:38 +00:00
|
|
|
import RequestUrlBar from './RequestUrlBar.js';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {MOD_SYM, getContentTypeName} from '../../common/constants';
|
2016-11-10 02:40:53 +00:00
|
|
|
import {debounce} from '../../common/misc';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../analytics/index';
|
2016-11-28 07:12:17 +00:00
|
|
|
import * as querystring from '../../common/querystring';
|
2016-09-17 05:46:44 +00:00
|
|
|
|
2016-11-26 00:37:59 +00:00
|
|
|
class RequestPane extends PureComponent {
|
|
|
|
_handleHidePasswords = () => this.props.updateSettingsShowPasswords(false);
|
|
|
|
_handleShowPasswords = () => this.props.updateSettingsShowPasswords(true);
|
|
|
|
|
2016-11-26 08:29:16 +00:00
|
|
|
_handleUpdateSettingsUseBulkHeaderEditor = () => {
|
2016-11-26 00:49:38 +00:00
|
|
|
const {useBulkHeaderEditor, updateSettingsUseBulkHeaderEditor} = this.props;
|
|
|
|
updateSettingsUseBulkHeaderEditor(!useBulkHeaderEditor);
|
|
|
|
trackEvent('Headers', 'Toggle Bulk', !useBulkHeaderEditor ? 'On' : 'Off');
|
|
|
|
};
|
|
|
|
|
2016-11-26 08:29:16 +00:00
|
|
|
_handleImportFile = () => {
|
|
|
|
this.props.handleImportFile();
|
|
|
|
trackEvent('Request Pane', 'CTA', 'Import');
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleCreateRequest = () => {
|
|
|
|
this.props.handleCreateRequest(this.props.request);
|
|
|
|
trackEvent('Request Pane', 'CTA', 'New Request');
|
|
|
|
};
|
|
|
|
|
2017-02-28 21:32:23 +00:00
|
|
|
_handleUpdateRequestUrl = debounce(url => this.props.updateRequestUrl(url));
|
|
|
|
|
|
|
|
_handleImportQueryFromUrl = () => {
|
2016-11-28 07:12:17 +00:00
|
|
|
const {request} = this.props;
|
|
|
|
|
|
|
|
let parsed;
|
|
|
|
try {
|
|
|
|
parsed = urlParse(request.url);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Failed to parse url to import querystring');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the search string (?foo=bar&...) from the Url
|
|
|
|
const url = request.url.replace(parsed.search, '');
|
|
|
|
const parameters = [
|
|
|
|
...request.parameters,
|
|
|
|
...querystring.deconstructToParams(parsed.query),
|
|
|
|
];
|
|
|
|
|
2017-02-02 18:30:13 +00:00
|
|
|
this.props.forceUpdateRequest({url, parameters});
|
2016-11-28 07:12:17 +00:00
|
|
|
};
|
|
|
|
|
2016-11-26 08:29:16 +00:00
|
|
|
_trackQueryToggle = pair => trackEvent('Query', 'Toggle', pair.disabled ? 'Disable' : 'Enable');
|
|
|
|
_trackQueryCreate = () => trackEvent('Query', 'Create');
|
|
|
|
_trackQueryDelete = () => trackEvent('Query', 'Delete');
|
|
|
|
_trackTabBody = () => trackEvent('Request Pane', 'View', 'Body');
|
|
|
|
_trackTabHeaders = () => trackEvent('Request Pane', 'View', 'Headers');
|
|
|
|
_trackTabAuthentication = () => trackEvent('Request Pane', 'View', 'Authentication');
|
|
|
|
_trackTabQuery = () => trackEvent('Request Pane', 'View', 'Query');
|
|
|
|
|
2016-09-17 05:46:44 +00:00
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
request,
|
2016-11-17 18:45:54 +00:00
|
|
|
environmentId,
|
2016-09-17 05:46:44 +00:00
|
|
|
showPasswords,
|
|
|
|
editorFontSize,
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorLineWrapping,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSend,
|
2017-02-01 20:21:14 +00:00
|
|
|
handleSendAndDownload,
|
2017-02-13 08:12:02 +00:00
|
|
|
handleRender,
|
2016-11-28 07:12:17 +00:00
|
|
|
forceRefreshCounter,
|
2016-09-17 05:46:44 +00:00
|
|
|
useBulkHeaderEditor,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode,
|
2017-02-02 18:30:13 +00:00
|
|
|
handleImport,
|
2016-09-17 05:46:44 +00:00
|
|
|
updateRequestMethod,
|
|
|
|
updateRequestBody,
|
|
|
|
updateRequestParameters,
|
|
|
|
updateRequestAuthentication,
|
|
|
|
updateRequestHeaders,
|
2016-11-22 19:42:10 +00:00
|
|
|
updateRequestMimeType,
|
2016-11-26 00:37:59 +00:00
|
|
|
updateSettingsShowPasswords,
|
2016-09-17 05:46:44 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="request-pane pane">
|
|
|
|
<header className="pane__header"></header>
|
|
|
|
<div className="pane__body pane__body--placeholder">
|
|
|
|
<div>
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>New Request</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}N</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Switch Requests</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}P</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Edit Environments</td>
|
|
|
|
<td className="text-right">
|
|
|
|
<code>{MOD_SYM}E</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2016-09-17 05:46:44 +00:00
|
|
|
|
2016-09-20 21:17:01 +00:00
|
|
|
<div className="text-center pane__body--placeholder__cta">
|
2016-11-26 08:29:16 +00:00
|
|
|
<button className="btn inline-block btn--clicky" onClick={this._handleImportFile}>
|
2016-09-20 21:17:01 +00:00
|
|
|
Import from File
|
|
|
|
</button>
|
2016-11-26 00:49:38 +00:00
|
|
|
<button className="btn inline-block btn--clicky"
|
2016-11-26 08:29:16 +00:00
|
|
|
onClick={this._handleCreateRequest}>
|
2016-09-20 21:17:01 +00:00
|
|
|
New Request
|
|
|
|
</button>
|
2016-09-17 05:46:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
|
|
|
</section>
|
2016-09-17 05:46:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:24:59 +00:00
|
|
|
let numBodyParams = 0;
|
|
|
|
if (request.body && request.body.params) {
|
|
|
|
numBodyParams = request.body.params.filter(p => !p.disabled).length;
|
|
|
|
}
|
2016-11-26 08:29:16 +00:00
|
|
|
|
2016-11-23 03:07:35 +00:00
|
|
|
const numParameters = request.parameters.filter(p => !p.disabled).length;
|
|
|
|
const numHeaders = request.headers.filter(h => !h.disabled).length;
|
2016-11-24 01:12:13 +00:00
|
|
|
const hasAuth = !request.authentication.disabled && request.authentication.username;
|
2016-11-28 07:12:17 +00:00
|
|
|
const urlHasQueryParameters = request.url.indexOf('?') >= 0;
|
|
|
|
|
|
|
|
const uniqueKey = `${forceRefreshCounter}::${request._id}`;
|
2016-11-23 03:07:35 +00:00
|
|
|
|
2016-09-17 05:46:44 +00:00
|
|
|
return (
|
2016-09-20 21:17:01 +00:00
|
|
|
<section className="pane request-pane">
|
2016-11-07 20:24:38 +00:00
|
|
|
<header className="pane__header">
|
|
|
|
<RequestUrlBar
|
2016-11-28 07:12:17 +00:00
|
|
|
key={uniqueKey}
|
2016-11-07 20:24:38 +00:00
|
|
|
method={request.method}
|
|
|
|
onMethodChange={updateRequestMethod}
|
2017-02-28 21:32:23 +00:00
|
|
|
onUrlChange={this._handleUpdateRequestUrl}
|
2017-02-02 18:30:13 +00:00
|
|
|
handleImport={handleImport}
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode={handleGenerateCode}
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSend={handleSend}
|
2017-02-01 20:21:14 +00:00
|
|
|
handleSendAndDownload={handleSendAndDownload}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2016-11-07 20:24:38 +00:00
|
|
|
url={request.url}
|
|
|
|
/>
|
|
|
|
</header>
|
2016-09-20 21:17:01 +00:00
|
|
|
<Tabs className="pane__body">
|
|
|
|
<TabList>
|
2016-11-26 08:29:16 +00:00
|
|
|
<Tab onClick={this._trackTabBody}>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-11-25 19:01:41 +00:00
|
|
|
{getContentTypeName(request.body.mimeType)}
|
2016-11-24 01:24:59 +00:00
|
|
|
{" "}
|
|
|
|
{numBodyParams ? <span className="txt-sm">({numBodyParams})</span> : null}
|
2016-09-20 21:17:01 +00:00
|
|
|
</button>
|
2016-11-27 21:42:38 +00:00
|
|
|
<ContentTypeDropdown onChange={updateRequestMimeType}
|
|
|
|
contentType={request.body.mimeType}
|
|
|
|
className="tall">
|
2016-11-25 19:01:41 +00:00
|
|
|
<i className="fa fa-caret-down"></i>
|
|
|
|
</ContentTypeDropdown>
|
2016-09-20 21:17:01 +00:00
|
|
|
</Tab>
|
2016-11-26 08:29:16 +00:00
|
|
|
<Tab onClick={this._trackTabAuthentication}>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-11-23 03:07:35 +00:00
|
|
|
Auth {hasAuth ? <i className="fa fa-lock txt-sm"></i> : null}
|
2016-09-20 21:17:01 +00:00
|
|
|
</button>
|
|
|
|
</Tab>
|
2016-11-26 08:29:16 +00:00
|
|
|
<Tab onClick={this._trackTabQuery}>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-11-23 03:07:35 +00:00
|
|
|
Query {numParameters ? <span className="txt-sm">({numParameters})</span> : null}
|
2016-09-20 21:17:01 +00:00
|
|
|
</button>
|
|
|
|
</Tab>
|
2016-11-26 08:29:16 +00:00
|
|
|
<Tab onClick={this._trackTabHeaders}>
|
2016-10-27 03:41:30 +00:00
|
|
|
<button>
|
2016-11-23 03:07:35 +00:00
|
|
|
Headers {numHeaders ? <span className="txt-sm">({numHeaders})</span> : null}
|
2016-09-20 21:17:01 +00:00
|
|
|
</button>
|
|
|
|
</Tab>
|
|
|
|
</TabList>
|
|
|
|
<TabPanel className="editor-wrapper">
|
|
|
|
<BodyEditor
|
2016-11-25 19:01:41 +00:00
|
|
|
handleUpdateRequestMimeType={updateRequestMimeType}
|
2017-02-13 08:12:02 +00:00
|
|
|
handleRender={handleRender}
|
2016-11-28 07:12:17 +00:00
|
|
|
key={uniqueKey}
|
2016-09-20 21:17:01 +00:00
|
|
|
request={request}
|
|
|
|
onChange={updateRequestBody}
|
|
|
|
fontSize={editorFontSize}
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap={editorKeyMap}
|
2016-09-20 21:17:01 +00:00
|
|
|
lineWrapping={editorLineWrapping}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
2016-09-21 00:03:26 +00:00
|
|
|
<TabPanel className="scrollable-container">
|
|
|
|
<div className="scrollable">
|
|
|
|
<AuthEditor
|
2016-11-28 07:12:17 +00:00
|
|
|
key={uniqueKey}
|
2016-09-21 00:03:26 +00:00
|
|
|
showPasswords={showPasswords}
|
2016-11-26 00:37:59 +00:00
|
|
|
authentication={request.authentication}
|
|
|
|
handleUpdateSettingsShowPasswords={updateSettingsShowPasswords}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2016-09-21 00:03:26 +00:00
|
|
|
onChange={updateRequestAuthentication}
|
|
|
|
/>
|
2016-11-26 00:37:59 +00:00
|
|
|
<div className="pad pull-right">
|
|
|
|
{showPasswords ? (
|
2017-01-28 06:09:01 +00:00
|
|
|
<button className="btn btn--clicky" onClick={this._handleHidePasswords}>
|
|
|
|
Hide Password
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button className="btn btn--clicky" onClick={this._handleShowPasswords}>
|
|
|
|
Show Password
|
|
|
|
</button>
|
|
|
|
)}
|
2016-11-26 00:37:59 +00:00
|
|
|
</div>
|
2016-09-21 00:03:26 +00:00
|
|
|
</div>
|
2016-09-20 21:17:01 +00:00
|
|
|
</TabPanel>
|
2016-11-28 07:12:17 +00:00
|
|
|
<TabPanel className="query-editor">
|
|
|
|
<div className="pad no-pad-bottom">
|
|
|
|
<label className="label--small">
|
|
|
|
Url Preview
|
|
|
|
</label>
|
|
|
|
<code className="txt-sm block">
|
|
|
|
<RenderedQueryString
|
|
|
|
request={request}
|
|
|
|
environmentId={environmentId}
|
|
|
|
placeholder="http://awesome-api.com?name=Gregory"
|
|
|
|
/>
|
|
|
|
</code>
|
|
|
|
</div>
|
|
|
|
<div className="scrollable-container">
|
|
|
|
<div className="scrollable">
|
|
|
|
<KeyValueEditor
|
|
|
|
key={uniqueKey}
|
|
|
|
namePlaceholder="name"
|
|
|
|
valuePlaceholder="value"
|
2017-02-28 06:26:24 +00:00
|
|
|
sortable={true}
|
2016-11-28 07:12:17 +00:00
|
|
|
onToggleDisable={this._trackQueryToggle}
|
|
|
|
onCreate={this._trackQueryCreate}
|
|
|
|
onDelete={this._trackQueryDelete}
|
|
|
|
pairs={request.parameters}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2016-11-28 07:12:17 +00:00
|
|
|
onChange={updateRequestParameters}
|
|
|
|
/>
|
2016-09-21 00:03:26 +00:00
|
|
|
</div>
|
2016-11-28 07:12:17 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="pad-right text-right">
|
|
|
|
<button className="margin-top-sm btn btn--clicky"
|
|
|
|
title={urlHasQueryParameters ? 'Import querystring' : 'No query params to import'}
|
|
|
|
onClick={this._handleImportQueryFromUrl}>
|
|
|
|
Import from Url
|
|
|
|
</button>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
|
|
|
</TabPanel>
|
2016-09-22 20:02:29 +00:00
|
|
|
<TabPanel className="header-editor">
|
|
|
|
<RequestHeadersEditor
|
2016-11-28 07:12:17 +00:00
|
|
|
key={uniqueKey}
|
2016-09-22 20:02:29 +00:00
|
|
|
headers={request.headers}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2016-09-22 20:02:29 +00:00
|
|
|
onChange={updateRequestHeaders}
|
|
|
|
bulk={useBulkHeaderEditor}
|
|
|
|
/>
|
|
|
|
|
2016-09-22 20:04:23 +00:00
|
|
|
<div className="pad-right text-right">
|
2016-11-26 00:49:38 +00:00
|
|
|
<button className="margin-top-sm btn btn--clicky"
|
2016-11-26 08:29:16 +00:00
|
|
|
onClick={this._handleUpdateSettingsUseBulkHeaderEditor}>
|
2016-09-22 20:02:29 +00:00
|
|
|
{useBulkHeaderEditor ? 'Regular Edit' : 'Bulk Edit'}
|
|
|
|
</button>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
</section>
|
2016-09-17 05:46:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestPane.propTypes = {
|
|
|
|
// Functions
|
2017-02-02 18:30:13 +00:00
|
|
|
forceUpdateRequest: PropTypes.func.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleSend: PropTypes.func.isRequired,
|
2017-02-01 20:21:14 +00:00
|
|
|
handleSendAndDownload: PropTypes.func.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleCreateRequest: PropTypes.func.isRequired,
|
2016-11-28 07:12:17 +00:00
|
|
|
handleGenerateCode: PropTypes.func.isRequired,
|
2017-02-13 08:12:02 +00:00
|
|
|
handleRender: PropTypes.func.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
updateRequestUrl: PropTypes.func.isRequired,
|
|
|
|
updateRequestMethod: PropTypes.func.isRequired,
|
|
|
|
updateRequestBody: PropTypes.func.isRequired,
|
|
|
|
updateRequestParameters: PropTypes.func.isRequired,
|
|
|
|
updateRequestAuthentication: PropTypes.func.isRequired,
|
|
|
|
updateRequestHeaders: PropTypes.func.isRequired,
|
2016-11-22 19:42:10 +00:00
|
|
|
updateRequestMimeType: PropTypes.func.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
updateSettingsShowPasswords: PropTypes.func.isRequired,
|
|
|
|
updateSettingsUseBulkHeaderEditor: PropTypes.func.isRequired,
|
2017-02-02 18:30:13 +00:00
|
|
|
handleImport: PropTypes.func.isRequired,
|
2016-11-16 17:18:39 +00:00
|
|
|
handleImportFile: PropTypes.func.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Other
|
|
|
|
useBulkHeaderEditor: PropTypes.bool.isRequired,
|
|
|
|
showPasswords: PropTypes.bool.isRequired,
|
|
|
|
editorFontSize: PropTypes.number.isRequired,
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap: PropTypes.string.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
editorLineWrapping: PropTypes.bool.isRequired,
|
2016-11-29 20:55:31 +00:00
|
|
|
workspace: PropTypes.object.isRequired,
|
2016-11-17 18:45:54 +00:00
|
|
|
environmentId: PropTypes.string.isRequired,
|
2016-11-28 07:12:17 +00:00
|
|
|
forceRefreshCounter: PropTypes.number.isRequired,
|
2016-09-17 05:46:44 +00:00
|
|
|
|
|
|
|
// Optional
|
|
|
|
request: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RequestPane;
|