2016-08-22 20:05:42 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
|
|
|
|
import KeyValueEditor from '../base/KeyValueEditor';
|
|
|
|
import Editor from '../base/Editor';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2016-08-22 20:05:42 +00:00
|
|
|
|
|
|
|
class RequestHeadersEditor extends Component {
|
|
|
|
_handleBulkUpdate (headersString) {
|
|
|
|
this.props.onChange(this._getHeadersFromString(headersString));
|
|
|
|
}
|
|
|
|
|
|
|
|
_getHeadersFromString (headersString) {
|
|
|
|
const headers = [];
|
|
|
|
const rows = headersString.split(/[\n,]+/);
|
|
|
|
|
|
|
|
for (const row of rows) {
|
|
|
|
const items = row.split(':');
|
|
|
|
|
|
|
|
if (items.length != 2) {
|
|
|
|
// Need a colon to be valid
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = items[0].trim();
|
|
|
|
const value = items[1].trim();
|
|
|
|
|
|
|
|
if (!name || !value) {
|
|
|
|
// Need name and value to be valid
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 a valid header (key + value)
|
|
|
|
if (!header.name || !header.value) {
|
2016-08-22 20:05:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
headersString += `${header.name}: ${header.value}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return headersString;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {bulk, headers, onChange} = this.props;
|
|
|
|
|
|
|
|
return bulk ? (
|
2016-09-22 20:04:23 +00:00
|
|
|
<div className="tall">
|
2016-08-22 20:05:42 +00:00
|
|
|
<Editor
|
|
|
|
onChange={v => this._handleBulkUpdate(v)}
|
|
|
|
value={this._getHeadersString()}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2016-09-22 20:02:29 +00:00
|
|
|
<div className="pad-bottom scrollable-container">
|
|
|
|
<div className="scrollable">
|
|
|
|
<KeyValueEditor
|
|
|
|
namePlaceholder="My-Header"
|
|
|
|
valuePlaceholder="Value"
|
|
|
|
pairs={headers}
|
2016-11-23 19:33:24 +00:00
|
|
|
onToggleDisable={pair => trackEvent('Headers Editor', 'Toggle', pair.disabled ? 'Disable' : 'Enable')}
|
|
|
|
onCreate={() => trackEvent('Headers Editor', 'Create')}
|
|
|
|
onDelete={() => trackEvent('Headers Editor', 'Delete')}
|
2016-09-22 20:02:29 +00:00
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-08-22 20:05:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestHeadersEditor.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
bulk: PropTypes.bool.isRequired,
|
|
|
|
headers: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
value: PropTypes.string.isRequired
|
|
|
|
})).isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RequestHeadersEditor;
|