import React, {PureComponent, PropTypes} from 'react'; import autobind from 'autobind-decorator'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import KeyValueEditor from './key-value-editor/editor'; import RequestHeadersEditor from './editors/request-headers-editor'; import ContentTypeDropdown from './dropdowns/content-type-dropdown'; import RenderedQueryString from './rendered-query-string'; import BodyEditor from './editors/body/body-editor'; import AuthEditor from './editors/auth-editor'; import RequestUrlBar from './request-url-bar.js'; import {MOD_SYM, getContentTypeName} from '../../common/constants'; import {debounce} from '../../common/misc'; import {trackEvent} from '../../analytics/index'; import * as querystring from '../../common/querystring'; @autobind class RequestPane extends PureComponent { constructor (props) { super(props); this._handleUpdateRequestUrl = debounce(this._handleUpdateRequestUrl); } _handleHidePasswords () { this.props.updateSettingsShowPasswords(false); } _handleShowPasswords () { this.props.updateSettingsShowPasswords(true); } _handleUpdateSettingsUseBulkHeaderEditor () { const {useBulkHeaderEditor, updateSettingsUseBulkHeaderEditor} = this.props; updateSettingsUseBulkHeaderEditor(!useBulkHeaderEditor); trackEvent('Headers', 'Toggle Bulk', !useBulkHeaderEditor ? 'On' : 'Off'); } _handleImportFile () { this.props.handleImportFile(); trackEvent('Request Pane', 'CTA', 'Import'); } _handleCreateRequest () { this.props.handleCreateRequest(this.props.request); trackEvent('Request Pane', 'CTA', 'New Request'); } _handleUpdateRequestUrl (url) { this.props.updateRequestUrl(url); } _handleImportQueryFromUrl () { const {request} = this.props; let query; try { query = querystring.extractFromUrl(request.url); } catch (e) { console.warn('Failed to parse url to import querystring'); return; } // Remove the search string (?foo=bar&...) from the Url const url = request.url.replace(query, ''); const parameters = [ ...request.parameters, ...querystring.deconstructToParams(query) ]; // Only update if url changed if (url !== request.url) { this.props.forceUpdateRequest({url, parameters}); } } _trackQueryToggle (pair) { trackEvent('Query', 'Toggle', pair.disabled ? 'Disable' : 'Enable'); } _trackQueryCreate () { trackEvent('Query', 'Create'); } _trackQueryDelete () { trackEvent('Query', 'Delete'); } _trackTabBody () { trackEvent('Request Pane', 'View', 'Body'); } _trackTabHeaders () { trackEvent('Request Pane', 'View', 'Headers'); } _trackTabAuthentication () { trackEvent('Request Pane', 'View', 'Authentication'); } _trackTabQuery () { trackEvent('Request Pane', 'View', 'Query'); } render () { const { request, showPasswords, editorFontSize, editorKeyMap, editorLineWrapping, handleSend, handleSendAndDownload, handleRender, handleGetRenderContext, forceRefreshCounter, useBulkHeaderEditor, handleGenerateCode, handleImport, updateRequestMethod, updateRequestBody, updateRequestParameters, updateRequestAuthentication, updateRequestHeaders, updateRequestMimeType, updateSettingsShowPasswords } = this.props; if (!request) { return (
New Request {MOD_SYM}N
Switch Requests {MOD_SYM}P
Edit Environments {MOD_SYM}E
); } let numBodyParams = 0; if (request.body && request.body.params) { numBodyParams = request.body.params.filter(p => !p.disabled).length; } const numParameters = request.parameters.filter(p => !p.disabled).length; const numHeaders = request.headers.filter(h => !h.disabled).length; const hasAuth = !request.authentication.disabled && request.authentication.username; const urlHasQueryParameters = request.url.indexOf('?') >= 0; const uniqueKey = `${forceRefreshCounter}::${request._id}`; return (
{showPasswords ? ( ) : ( )}
); } } RequestPane.propTypes = { // Functions forceUpdateRequest: PropTypes.func.isRequired, handleSend: PropTypes.func.isRequired, handleSendAndDownload: PropTypes.func.isRequired, handleCreateRequest: PropTypes.func.isRequired, handleGenerateCode: PropTypes.func.isRequired, handleRender: PropTypes.func.isRequired, handleGetRenderContext: PropTypes.func.isRequired, updateRequestUrl: PropTypes.func.isRequired, updateRequestMethod: PropTypes.func.isRequired, updateRequestBody: PropTypes.func.isRequired, updateRequestParameters: PropTypes.func.isRequired, updateRequestAuthentication: PropTypes.func.isRequired, updateRequestHeaders: PropTypes.func.isRequired, updateRequestMimeType: PropTypes.func.isRequired, updateSettingsShowPasswords: PropTypes.func.isRequired, updateSettingsUseBulkHeaderEditor: PropTypes.func.isRequired, handleImport: PropTypes.func.isRequired, handleImportFile: PropTypes.func.isRequired, // Other useBulkHeaderEditor: PropTypes.bool.isRequired, showPasswords: PropTypes.bool.isRequired, editorFontSize: PropTypes.number.isRequired, editorKeyMap: PropTypes.string.isRequired, editorLineWrapping: PropTypes.bool.isRequired, workspace: PropTypes.object.isRequired, forceRefreshCounter: PropTypes.number.isRequired, // Optional request: PropTypes.object }; export default RequestPane;