insomnia/app/ui/components/editors/request-headers-editor.js

152 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import KeyValueEditor from '../key-value-editor/editor';
import CodeEditor from '../codemirror/code-editor';
import {trackEvent} from '../../../analytics/index';
2017-03-24 05:38:42 +00:00
import allHeaderNames from '../../../datasets/header-names';
import allCharsets from '../../../datasets/charsets';
import allMimeTypes from '../../../datasets/content-types';
import allEncodings from '../../../datasets/encodings';
@autobind
class RequestHeadersEditor extends PureComponent {
_handleBulkUpdate (headersString) {
2016-08-22 20:05:42 +00:00
this.props.onChange(this._getHeadersFromString(headersString));
}
_handleTrackToggle (pair) {
trackEvent('Headers Editor', 'Toggle', pair.disabled ? 'Disable' : 'Enable');
}
_handleTrackCreate () {
trackEvent('Headers Editor', 'Create');
}
_handleTrackDelete () {
trackEvent('Headers Editor', 'Delete');
}
2016-08-22 20:05:42 +00:00
_getHeadersFromString (headersString) {
const headers = [];
const rows = headersString.split(/\n+/);
2016-08-22 20:05:42 +00:00
for (const row of rows) {
const [rawName, rawValue] = row.split(/:(.*)$/);
2016-08-22 20:05:42 +00:00
const name = (rawName || '').trim();
const value = (rawValue || '').trim();
if (!name && !value) {
continue;
}
2016-08-22 20:05:42 +00:00
headers.push({name, value});
}
return headers;
}
_getHeadersString () {
const {headers} = this.props;
let headersString = '';
for (const header of headers) {
2016-11-22 19:43:36 +00:00
// Make sure it's not disabled
if (header.disabled) {
continue;
}
// Make sure it's not blank
if (!header.name && !header.value) {
2016-08-22 20:05:42 +00:00
continue;
}
headersString += `${header.name}: ${header.value}\n`;
}
return headersString;
}
_getCommonHeaderValues (pair) {
switch (pair.name.toLowerCase()) {
case 'content-type':
case 'accept':
return allMimeTypes;
case 'accept-charset':
return allCharsets;
case 'accept-encoding':
return allEncodings;
default:
return [];
}
}
_getCommonHeaderNames (pair) {
return allHeaderNames;
}
2016-08-22 20:05:42 +00:00
render () {
const {
bulk,
headers,
editorFontSize,
editorIndentSize,
editorLineWrapping,
onChange,
handleRender,
handleGetRenderContext
} = this.props;
2016-08-22 20:05:42 +00:00
return bulk ? (
<div className="tall">
<CodeEditor
getRenderContext={handleGetRenderContext}
render={handleRender}
fontSize={editorFontSize}
indentSize={editorIndentSize}
lineWrapping={editorLineWrapping}
onChange={this._handleBulkUpdate}
defaultValue={this._getHeadersString()}
2016-09-22 20:02:29 +00:00
/>
</div>
) : (
<div className="pad-bottom scrollable-container">
<div className="scrollable">
<KeyValueEditor
sortable
namePlaceholder="My-Header"
valuePlaceholder="Value"
pairs={headers}
handleRender={handleRender}
handleGetRenderContext={handleGetRenderContext}
handleGetAutocompleteNameConstants={this._getCommonHeaderNames}
handleGetAutocompleteValueConstants={this._getCommonHeaderValues}
onToggleDisable={this._handleTrackToggle}
onCreate={this._handleTrackCreate}
onDelete={this._handleTrackDelete}
onChange={onChange}
/>
</div>
</div>
);
2016-08-22 20:05:42 +00:00
}
}
RequestHeadersEditor.propTypes = {
onChange: PropTypes.func.isRequired,
bulk: PropTypes.bool.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorIndentSize: PropTypes.number.isRequired,
editorLineWrapping: PropTypes.bool.isRequired,
handleRender: PropTypes.func.isRequired,
handleGetRenderContext: PropTypes.func.isRequired,
2016-08-22 20:05:42 +00:00
headers: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string.isRequired,
value: PropTypes.string.isRequired
})).isRequired
};
export default RequestHeadersEditor;