import React, {PropTypes, PureComponent} from 'react';
import autobind from 'autobind-decorator';
import fs from 'fs';
import mime from 'mime-types';
import {remote} from 'electron';
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
import SizeTag from './tags/SizeTag';
import StatusTag from './tags/StatusTag';
import TimeTag from './tags/TimeTag';
import Button from './base/Button';
import PreviewModeDropdown from './dropdowns/PreviewModeDropdown';
import ResponseViewer from './viewers/ResponseViewer';
import ResponseHistoryDropdown from './dropdowns/ResponseHistoryDropdown';
import ResponseTimer from './ResponseTimer';
import ResponseHeadersViewer from './viewers/ResponseHeadersViewer';
import ResponseCookiesViewer from './viewers/ResponseCookiesViewer';
import * as models from '../../models';
import {MOD_SYM, PREVIEW_MODE_SOURCE, getPreviewModeName} from '../../common/constants';
import {getSetCookieHeaders, nullFn} from '../../common/misc';
import {cancelCurrentRequest} from '../../common/network';
import {trackEvent} from '../../analytics';
@autobind
class ResponsePane extends PureComponent {
constructor (props) {
super(props);
this.state = {
response: null
};
}
_trackTab (name) {
trackEvent('Response Pane', 'View', name);
}
async _getResponse (requestId, responseId) {
let response = await models.response.getById(responseId);
if (!response) {
response = await models.response.getLatestByParentId(requestId);
}
this.setState({response});
}
async _handleDownloadResponseBody () {
if (!this.state.response) {
// Should never happen
console.warn('No response to download');
return;
}
const {body, encoding, contentType} = this.state.response;
const bodyBuffer = new Buffer(body, encoding);
const extension = mime.extension(contentType) || '';
const options = {
title: 'Save Response',
buttonLabel: 'Save',
filters: [{
name: 'Download', extensions: [extension],
}]
};
remote.dialog.showSaveDialog(options, filename => {
if (!filename) {
trackEvent('Response', 'Save Cancel');
return;
}
fs.writeFile(filename, bodyBuffer, {}, err => {
if (err) {
console.warn('Failed to save response body', err);
trackEvent('Response', 'Save Failure');
} else {
trackEvent('Response', 'Save Success');
}
});
});
}
componentWillReceiveProps (nextProps) {
const activeRequestId = nextProps.request ? nextProps.request._id : null;
const activeResponseId = nextProps.activeResponseId;
this._getResponse(activeRequestId, activeResponseId);
}
componentDidMount () {
const activeRequestId = this.props.request ? this.props.request._id : null;
const activeResponseId = this.props.activeResponseId;
this._getResponse(activeRequestId, activeResponseId);
}
render () {
const {
request,
previewMode,
handleSetPreviewMode,
handleSetActiveResponse,
handleDeleteResponses,
handleSetFilter,
loadStartTime,
editorLineWrapping,
editorFontSize,
editorKeyMap,
filter,
activeResponseId,
showCookiesModal
} = this.props;
const {response} = this.state;
if (!request) {
return (
)
}
if (!response) {
return (
Send Request |
{MOD_SYM}Enter
|
Focus Url Bar |
{MOD_SYM}L
|
Manage Cookies |
{MOD_SYM}K
|
Edit Environments |
{MOD_SYM}E
|
)
}
const cookieHeaders = getSetCookieHeaders(response.headers);
return (
{!response ? null : (
)}
)
}
}
ResponsePane.propTypes = {
// Functions
handleSetFilter: PropTypes.func.isRequired,
showCookiesModal: PropTypes.func.isRequired,
handleSetPreviewMode: PropTypes.func.isRequired,
handleSetActiveResponse: PropTypes.func.isRequired,
handleDeleteResponses: PropTypes.func.isRequired,
// Required
previewMode: PropTypes.string.isRequired,
filter: PropTypes.string.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorKeyMap: PropTypes.string.isRequired,
editorLineWrapping: PropTypes.bool.isRequired,
loadStartTime: PropTypes.number.isRequired,
activeResponseId: PropTypes.string.isRequired,
// Other
request: PropTypes.object,
};
export default ResponsePane;