insomnia/app/ui/components/editors/body/BodyEditor.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

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