2016-11-26 08:29:16 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-08 05:52:17 +00:00
|
|
|
import RawEditor from './raw-editor';
|
|
|
|
import UrlEncodedEditor from './url-encoded-editor';
|
|
|
|
import FormEditor from './form-editor';
|
|
|
|
import FileEditor from './file-editor';
|
2016-11-25 19:01:41 +00:00
|
|
|
import {getContentTypeFromHeaders, CONTENT_TYPE_FORM_URLENCODED, CONTENT_TYPE_FORM_DATA, CONTENT_TYPE_FILE} from '../../../../common/constants';
|
|
|
|
import {newBodyRaw, newBodyFormUrlEncoded, newBodyForm, newBodyFile} from '../../../../models/request';
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2016-11-26 08:29:16 +00:00
|
|
|
class BodyEditor extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleRawChange (rawValue) {
|
2016-11-22 19:42:10 +00:00
|
|
|
const {onChange, request} = this.props;
|
|
|
|
|
|
|
|
const contentType = getContentTypeFromHeaders(request.headers);
|
2016-11-25 19:01:41 +00:00
|
|
|
const newBody = newBodyRaw(rawValue, contentType || '');
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
onChange(newBody);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleFormUrlEncodedChange (parameters) {
|
2016-11-22 19:42:10 +00:00
|
|
|
const {onChange} = this.props;
|
|
|
|
const newBody = newBodyFormUrlEncoded(parameters);
|
|
|
|
onChange(newBody);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleFormChange (parameters) {
|
2016-11-22 19:42:10 +00:00
|
|
|
const {onChange} = this.props;
|
|
|
|
const newBody = newBodyForm(parameters);
|
|
|
|
onChange(newBody);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleFileChange (path) {
|
2016-11-22 22:26:52 +00:00
|
|
|
const {onChange} = this.props;
|
|
|
|
const newBody = newBodyFile(path);
|
|
|
|
onChange(newBody);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-22 22:26:52 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
render () {
|
2017-03-12 00:31:23 +00:00
|
|
|
const {
|
|
|
|
keyMap,
|
|
|
|
fontSize,
|
|
|
|
lineWrapping,
|
|
|
|
request,
|
|
|
|
handleRender,
|
|
|
|
handleGetRenderContext
|
|
|
|
} = this.props;
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
const fileName = request.body.fileName;
|
2016-11-25 19:01:41 +00:00
|
|
|
const mimeType = request.body.mimeType;
|
2016-11-26 00:37:59 +00:00
|
|
|
const isBodyEmpty = typeof mimeType !== 'string' && !request.body.text;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2016-11-25 19:01:41 +00:00
|
|
|
if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
|
2016-11-22 19:42:10 +00:00
|
|
|
return (
|
|
|
|
<UrlEncodedEditor
|
|
|
|
key={request._id}
|
2016-11-25 20:55:15 +00:00
|
|
|
onChange={this._handleFormUrlEncodedChange}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2017-03-12 00:31:23 +00:00
|
|
|
handleGetRenderContext={handleGetRenderContext}
|
2016-11-22 19:42:10 +00:00
|
|
|
parameters={request.body.params || []}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-25 19:01:41 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
|
2016-11-22 19:42:10 +00:00
|
|
|
return (
|
|
|
|
<FormEditor
|
|
|
|
key={request._id}
|
2016-11-25 20:55:15 +00:00
|
|
|
onChange={this._handleFormChange}
|
2017-02-27 21:00:13 +00:00
|
|
|
handleRender={handleRender}
|
2017-03-12 00:31:23 +00:00
|
|
|
handleGetRenderContext={handleGetRenderContext}
|
2016-11-22 19:42:10 +00:00
|
|
|
parameters={request.body.params || []}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-25 19:01:41 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FILE) {
|
2016-11-22 22:26:52 +00:00
|
|
|
return (
|
|
|
|
<FileEditor
|
|
|
|
key={request._id}
|
2016-11-25 20:55:15 +00:00
|
|
|
onChange={this._handleFileChange}
|
2016-11-22 22:26:52 +00:00
|
|
|
path={fileName || ''}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-26 00:37:59 +00:00
|
|
|
} else if (!isBodyEmpty) {
|
2016-11-25 19:01:41 +00:00
|
|
|
const contentType = getContentTypeFromHeaders(request.headers) || mimeType;
|
2016-11-22 19:42:10 +00:00
|
|
|
return (
|
|
|
|
<RawEditor
|
|
|
|
key={`${request._id}::${contentType}`}
|
|
|
|
fontSize={fontSize}
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap={keyMap}
|
2016-11-22 19:42:10 +00:00
|
|
|
lineWrapping={lineWrapping}
|
|
|
|
contentType={contentType || 'text/plain'}
|
|
|
|
content={request.body.text || ''}
|
2017-02-13 08:12:02 +00:00
|
|
|
render={handleRender}
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext={handleGetRenderContext}
|
2016-11-25 20:55:15 +00:00
|
|
|
onChange={this._handleRawChange}
|
2016-11-22 19:42:10 +00:00
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-25 19:01:41 +00:00
|
|
|
} else {
|
|
|
|
return (
|
2017-02-26 21:12:51 +00:00
|
|
|
<div className="editor valign-center text-center">
|
2016-11-25 19:01:41 +00:00
|
|
|
<p className="pad super-faint text-sm text-center">
|
2017-02-13 08:12:02 +00:00
|
|
|
<i className="fa fa-hand-peace-o" style={{fontSize: '8rem', opacity: 0.3}}/>
|
2016-11-28 07:12:17 +00:00
|
|
|
<br/><br/>
|
2017-01-20 21:54:03 +00:00
|
|
|
Select a body type from above
|
2016-11-25 19:01:41 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BodyEditor.propTypes = {
|
|
|
|
// Required
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2016-11-25 19:01:41 +00:00
|
|
|
handleUpdateRequestMimeType: PropTypes.func.isRequired,
|
2017-02-13 08:12:02 +00:00
|
|
|
handleRender: PropTypes.func.isRequired,
|
2017-03-12 00:31:23 +00:00
|
|
|
handleGetRenderContext: PropTypes.func.isRequired,
|
2016-11-22 19:42:10 +00:00
|
|
|
request: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
fontSize: PropTypes.number,
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap: PropTypes.string,
|
2016-11-22 19:42:10 +00:00
|
|
|
lineWrapping: PropTypes.bool
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BodyEditor;
|