insomnia/app/ui/components/RequestPane.js

260 lines
9.4 KiB
JavaScript
Raw Normal View History

2016-11-26 00:37:59 +00:00
import React, {PureComponent, PropTypes} from 'react';
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
2016-09-17 05:46:44 +00:00
import KeyValueEditor from './base/KeyValueEditor';
import RequestHeadersEditor from './editors/RequestHeadersEditor';
import ContentTypeDropdown from './dropdowns/ContentTypeDropdown';
import RenderedQueryString from './RenderedQueryString';
import BodyEditor from './editors/body/BodyEditor';
2016-09-17 05:46:44 +00:00
import AuthEditor from './editors/AuthEditor';
import RequestUrlBar from './RequestUrlBar.js';
import {MOD_SYM, getContentTypeName} from '../../common/constants';
2016-11-10 02:40:53 +00:00
import {debounce} from '../../common/misc';
import {trackEvent} from '../../analytics/index';
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');
};
_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,
editorLineWrapping,
handleSend,
2016-09-17 05:46:44 +00:00
useBulkHeaderEditor,
updateRequestUrl,
updateRequestMethod,
updateRequestBody,
updateRequestParameters,
updateRequestAuthentication,
updateRequestHeaders,
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;
const hasAuth = !request.authentication.disabled && request.authentication.username;
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">
<header className="pane__header">
<RequestUrlBar
key={request._id}
method={request.method}
onMethodChange={updateRequestMethod}
onUrlChange={debounce(updateRequestUrl)}
handleSend={handleSend}
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>
{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>
<ContentTypeDropdown updateRequestMimeType={updateRequestMimeType} className="tall">
<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
handleUpdateRequestMimeType={updateRequestMimeType}
key={request._id}
2016-09-20 21:17:01 +00:00
request={request}
onChange={updateRequestBody}
fontSize={editorFontSize}
lineWrapping={editorLineWrapping}
/>
</TabPanel>
2016-09-21 00:03:26 +00:00
<TabPanel className="scrollable-container">
<div className="scrollable">
<AuthEditor
key={request._id}
2016-09-21 00:03:26 +00:00
showPasswords={showPasswords}
2016-11-26 00:37:59 +00:00
authentication={request.authentication}
handleUpdateSettingsShowPasswords={updateSettingsShowPasswords}
2016-09-21 00:03:26 +00:00
onChange={updateRequestAuthentication}
/>
2016-11-26 00:37:59 +00:00
<div className="pad pull-right">
{showPasswords ? (
2016-11-26 00:49:38 +00:00
<button className="btn btn--clicky" onClick={this._handleHidePasswords}>
2016-11-26 00:37:59 +00:00
Hide Password
</button>
) : (
2016-11-26 00:49:38 +00:00
<button className="btn btn--clicky" onClick={this._handleShowPasswords}>
2016-11-26 00:37:59 +00:00
Show Password
</button>
)}
</div>
2016-09-21 00:03:26 +00:00
</div>
2016-09-20 21:17:01 +00:00
</TabPanel>
2016-09-21 00:03:26 +00:00
<TabPanel className="scrollable-container">
<div className="scrollable">
<div className="pad no-pad-bottom">
2016-11-26 09:02:45 +00:00
<label className="label--small">
Url Preview
</label>
2016-09-21 00:03:26 +00:00
<code className="txt-sm block">
<RenderedQueryString
request={request}
2016-11-17 18:45:54 +00:00
environmentId={environmentId}
2016-11-23 03:07:35 +00:00
placeholder="http://awesome-api.com?name=Gregory"
2016-09-21 00:03:26 +00:00
/>
</code>
</div>
<KeyValueEditor
key={request._id}
2016-09-21 00:03:26 +00:00
namePlaceholder="name"
valuePlaceholder="value"
2016-11-26 08:29:16 +00:00
onToggleDisable={this._trackQueryToggle}
onCreate={this._trackQueryCreate}
onDelete={this._trackQueryDelete}
2016-09-21 00:03:26 +00:00
pairs={request.parameters}
onChange={updateRequestParameters}
/>
2016-09-20 21:17:01 +00:00
</div>
</TabPanel>
2016-09-22 20:02:29 +00:00
<TabPanel className="header-editor">
<RequestHeadersEditor
key={request._id}
2016-09-22 20:02:29 +00:00
headers={request.headers}
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
handleSend: PropTypes.func.isRequired,
handleCreateRequest: 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,
updateRequestMimeType: PropTypes.func.isRequired,
2016-09-17 05:46:44 +00:00
updateSettingsShowPasswords: PropTypes.func.isRequired,
updateSettingsUseBulkHeaderEditor: PropTypes.func.isRequired,
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,
editorLineWrapping: PropTypes.bool.isRequired,
2016-11-17 18:45:54 +00:00
environmentId: PropTypes.string.isRequired,
2016-09-17 05:46:44 +00:00
// Optional
request: PropTypes.object,
};
export default RequestPane;