insomnia/app/ui/components/Wrapper.js

368 lines
15 KiB
JavaScript
Raw Normal View History

2016-11-25 23:09:17 +00:00
import React, {PropTypes, Component} from 'react';
import classnames from 'classnames';
import {showModal, registerModal} from './modals/index';
import AlertModal from '../components/modals/AlertModal';
import ChangelogModal from '../components/modals/ChangelogModal';
import CookiesModal from '../components/modals/CookiesModal';
import EnvironmentEditModal from '../components/modals/EnvironmentEditModal';
import GenerateCodeModal from '../components/modals/GenerateCodeModal';
import LoginModal from '../components/modals/LoginModal';
import PaymentModal from '../components/modals/PaymentModal';
2016-11-17 23:22:23 +00:00
import PaymentNotificationModal from '../components/modals/PaymentNotificationModal';
import PromptModal from '../components/modals/PromptModal';
import RequestCreateModal from '../components/modals/RequestCreateModal';
import RequestPane from './RequestPane';
import RequestSwitcherModal from '../components/modals/RequestSwitcherModal';
import ResponsePane from './ResponsePane';
import SettingsModal from '../components/modals/SettingsModal';
import Sidebar from './sidebar/Sidebar';
import SignupModal from '../components/modals/SignupModal';
import SyncLogsModal from '../components/modals/SyncLogsModal';
import WorkspaceEnvironmentsEditModal from '../components/modals/WorkspaceEnvironmentsEditModal';
import WorkspaceSettingsModal from '../components/modals/WorkspaceSettingsModal';
import * as models from '../../models/index';
import {updateMimeType} from '../../models/request';
2016-11-26 00:37:59 +00:00
import {trackEvent} from '../../analytics/index';
import * as importers from 'insomnia-importers';
2016-11-26 00:37:59 +00:00
const rUpdate = models.request.update;
const sUpdate = models.settings.update;
2016-11-25 23:09:17 +00:00
class Wrapper extends Component {
2016-11-26 00:37:59 +00:00
state = {forceRefreshRequestPaneCounter: Date.now()};
// Request updaters
2016-11-28 07:12:17 +00:00
_handleUpdateRequest = async patch => {
await rUpdate(this.props.activeRequest, patch);
this.forceRequestPaneRefresh();
};
2016-11-26 00:37:59 +00:00
_handleUpdateRequestBody = body => rUpdate(this.props.activeRequest, {body});
_handleUpdateRequestMethod = method => rUpdate(this.props.activeRequest, {method});
_handleUpdateRequestParameters = parameters => rUpdate(this.props.activeRequest, {parameters});
_handleUpdateRequestAuthentication = authentication => rUpdate(this.props.activeRequest, {authentication});
_handleUpdateRequestHeaders = headers => rUpdate(this.props.activeRequest, {headers});
// Special request updaters
_handleUpdateRequestMimeType = mimeType => updateMimeType(this.props.activeRequest, mimeType);
_handleUpdateRequestUrl = async url => {
// Allow user to paste any import file into the url. If it results in
// only one item, it will overwrite the current request.
const {activeRequest} = this.props;
try {
const {data} = importers.convert(url);
const {resources} = data;
2016-11-26 00:37:59 +00:00
const r = resources[0];
if (r && r._type === 'request') {
trackEvent('Import', 'Url Bar');
2016-11-25 23:09:17 +00:00
2016-11-26 00:37:59 +00:00
// Only pull fields that we want to update
await models.request.update(activeRequest, {
url: r.url,
method: r.method,
headers: r.headers,
body: r.body,
authentication: r.authentication,
parameters: r.parameters,
});
this.forceRequestPaneRefresh();
return;
}
} catch (e) {
// Import failed, that's alright
}
models.request.update(activeRequest, {url});
2016-11-25 23:09:17 +00:00
};
2016-11-26 00:37:59 +00:00
// Settings updaters
_handleUpdateSettingsShowPasswords = showPasswords => sUpdate(this.props.settings, {showPasswords});
_handleUpdateSettingsUseBulkHeaderEditor = useBulkHeaderEditor => sUpdate(this.props.settings, {useBulkHeaderEditor});
// Other Helpers
_handleImportFile = () => {
this.props.handleImportFileToWorkspace(this.props.activeWorkspace._id);
2016-12-07 17:57:01 +00:00
};
2016-11-26 00:37:59 +00:00
_handleExportWorkspaceToFile = () => this.props.handleExportFile(this.props.activeWorkspace._id);
_handleSetActiveResponse = responseId => this.props.handleSetActiveResponse(this.props.activeRequest._id, responseId);
2016-11-26 00:37:59 +00:00
_handleShowEnvironmentsModal = () => showModal(WorkspaceEnvironmentsEditModal, this.props.activeWorkspace);
_handleShowCookiesModal = () => showModal(CookiesModal, this.props.activeWorkspace);
_handleDeleteResponses = () => {
models.response.removeForRequest(this.props.activeRequest._id);
this._handleSetActiveResponse(null);
};
_handleRemoveActiveWorkspace = async () => {
const {workspaces, activeWorkspace} = this.props;
if (workspaces.length <= 1) {
showModal(AlertModal, {
title: 'Deleting Last Workspace',
message: 'Since you deleted your only workspace, a new one has been created for you.'
});
models.workspace.create({name: 'Insomnia'});
trackEvent('Workspace', 'Delete', 'Last');
} else {
trackEvent('Workspace', 'Delete');
}
models.workspace.remove(activeWorkspace);
};
2016-11-26 00:37:59 +00:00
_handleSendRequestWithActiveEnvironment = () => {
const {activeRequest, activeEnvironment, handleSendRequestWithEnvironment} = this.props;
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
const activeEnvironmentId = activeEnvironment ? activeEnvironment._id : 'n/a';
handleSendRequestWithEnvironment(activeRequestId, activeEnvironmentId);
2016-11-25 23:09:17 +00:00
};
_handleSetPreviewMode = previewMode => {
2016-11-25 23:09:17 +00:00
const activeRequest = this.props.activeRequest;
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
this.props.handleSetResponsePreviewMode(activeRequestId, previewMode);
};
2016-11-26 00:37:59 +00:00
_handleSetResponseFilter = filter => {
2016-11-25 23:09:17 +00:00
const activeRequest = this.props.activeRequest;
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
this.props.handleSetResponseFilter(activeRequestId, filter);
};
2016-11-26 00:37:59 +00:00
forceRequestPaneRefresh () {
this.setState({forceRefreshRequestPaneCounter: Date.now()});
}
2016-11-25 23:09:17 +00:00
render () {
const {
activeEnvironment,
activeRequest,
activeResponseId,
activeWorkspace,
2016-11-25 23:09:17 +00:00
environments,
handleActivateRequest,
2016-11-25 23:09:17 +00:00
handleCreateRequest,
handleCreateRequestForWorkspace,
handleCreateRequestGroup,
handleDuplicateRequest,
2016-11-28 07:12:17 +00:00
handleDuplicateRequestGroup,
2016-11-25 23:09:17 +00:00
handleExportFile,
handleMoveRequest,
handleMoveRequestGroup,
handleResetDragPane,
handleResetDragSidebar,
handleSetActiveEnvironment,
handleSetActiveWorkspace,
handleSetRequestGroupCollapsed,
2016-11-25 23:09:17 +00:00
handleSetRequestPaneRef,
handleSetResponsePaneRef,
handleSetSidebarRef,
handleStartDragPane,
handleStartDragSidebar,
handleSetSidebarFilter,
2016-11-28 07:12:17 +00:00
handleGenerateCode,
isLoading,
loadStartTime,
paneWidth,
responseFilter,
responsePreviewMode,
settings,
2016-11-25 23:09:17 +00:00
sidebarChildren,
sidebarFilter,
sidebarHidden,
sidebarWidth,
workspaceChildren,
workspaces,
2016-11-25 23:09:17 +00:00
} = this.props;
const realSidebarWidth = sidebarHidden ? 0 : sidebarWidth;
const gridTemplateColumns = `${realSidebarWidth}rem 0 ${paneWidth}fr 0 ${1 - paneWidth}fr`;
return (
<div id="wrapper"
className={classnames('wrapper', {'wrapper--vertical': settings.forceVerticalLayout})}
style={{gridTemplateColumns: gridTemplateColumns}}>
2016-11-26 00:37:59 +00:00
2016-11-25 23:09:17 +00:00
<Sidebar
ref={handleSetSidebarRef}
2016-11-26 00:37:59 +00:00
showEnvironmentsModal={this._handleShowEnvironmentsModal}
showCookiesModal={this._handleShowCookiesModal}
2016-11-25 23:09:17 +00:00
handleActivateRequest={handleActivateRequest}
handleChangeFilter={handleSetSidebarFilter}
2016-11-25 23:09:17 +00:00
handleImportFile={this._handleImportFile}
handleExportFile={handleExportFile}
handleSetActiveWorkspace={handleSetActiveWorkspace}
handleDuplicateRequest={handleDuplicateRequest}
2016-11-28 07:12:17 +00:00
handleGenerateCode={handleGenerateCode}
handleDuplicateRequestGroup={handleDuplicateRequestGroup}
2016-11-25 23:09:17 +00:00
handleSetActiveEnvironment={handleSetActiveEnvironment}
moveRequest={handleMoveRequest}
moveRequestGroup={handleMoveRequestGroup}
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
activeRequest={activeRequest}
activeEnvironment={activeEnvironment}
handleCreateRequest={handleCreateRequest}
handleCreateRequestGroup={handleCreateRequestGroup}
filter={sidebarFilter || ''}
hidden={sidebarHidden || false}
workspace={activeWorkspace}
children={sidebarChildren}
width={sidebarWidth}
isLoading={isLoading}
workspaces={workspaces}
environments={environments}
/>
2016-11-25 23:09:17 +00:00
<div className="drag drag--sidebar">
<div onDoubleClick={handleResetDragSidebar} onMouseDown={e => {
e.preventDefault();
handleStartDragSidebar();
}}></div>
</div>
<RequestPane
ref={handleSetRequestPaneRef}
handleImportFile={this._handleImportFile}
request={activeRequest}
showPasswords={settings.showPasswords}
useBulkHeaderEditor={settings.useBulkHeaderEditor}
editorFontSize={settings.editorFontSize}
editorLineWrapping={settings.editorLineWrapping}
environmentId={activeEnvironment ? activeEnvironment._id : 'n/a'}
workspace={activeWorkspace}
2016-11-25 23:09:17 +00:00
handleCreateRequest={handleCreateRequestForWorkspace}
2016-11-28 07:12:17 +00:00
handleGenerateCode={handleGenerateCode}
updateRequest={this._handleUpdateRequest}
2016-11-26 00:37:59 +00:00
updateRequestBody={this._handleUpdateRequestBody}
updateRequestUrl={this._handleUpdateRequestUrl}
updateRequestMethod={this._handleUpdateRequestMethod}
updateRequestParameters={this._handleUpdateRequestParameters}
updateRequestAuthentication={this._handleUpdateRequestAuthentication}
updateRequestHeaders={this._handleUpdateRequestHeaders}
updateRequestMimeType={this._handleUpdateRequestMimeType}
updateSettingsShowPasswords={this._handleUpdateSettingsShowPasswords}
updateSettingsUseBulkHeaderEditor={this._handleUpdateSettingsUseBulkHeaderEditor}
2016-11-28 07:12:17 +00:00
forceRefreshCounter={this.state.forceRefreshRequestPaneCounter}
2016-11-26 00:37:59 +00:00
handleSend={this._handleSendRequestWithActiveEnvironment}
2016-11-25 23:09:17 +00:00
/>
<div className="drag drag--pane">
<div onMouseDown={handleStartDragPane} onDoubleClick={handleResetDragPane}></div>
</div>
<ResponsePane
ref={handleSetResponsePaneRef}
request={activeRequest}
editorFontSize={settings.editorFontSize}
editorLineWrapping={settings.editorLineWrapping}
previewMode={responsePreviewMode}
activeResponseId={activeResponseId}
2016-11-25 23:09:17 +00:00
filter={responseFilter}
loadStartTime={loadStartTime}
showCookiesModal={this._handleShowCookiesModal}
handleSetActiveResponse={this._handleSetActiveResponse}
2016-11-25 23:09:17 +00:00
handleSetPreviewMode={this._handleSetPreviewMode}
handleDeleteResponses={this._handleDeleteResponses}
2016-11-25 23:09:17 +00:00
handleSetFilter={this._handleSetResponseFilter}
/>
2016-11-25 23:09:17 +00:00
<AlertModal ref={registerModal}/>
<CookiesModal ref={registerModal}/>
<ChangelogModal ref={registerModal}/>
<SyncLogsModal ref={registerModal}/>
<LoginModal ref={registerModal}/>
<PromptModal ref={registerModal}/>
<SignupModal ref={registerModal}/>
<PaymentModal ref={registerModal}/>
<RequestCreateModal ref={registerModal}/>
2016-11-25 23:09:17 +00:00
<PaymentNotificationModal ref={registerModal}/>
<WorkspaceSettingsModal
ref={registerModal}
workspace={activeWorkspace}
handleRemoveWorkspace={this._handleRemoveActiveWorkspace}/>
2016-11-25 23:09:17 +00:00
<EnvironmentEditModal
ref={registerModal}
onChange={models.requestGroup.update}
/>
<GenerateCodeModal
ref={registerModal}
environmentId={activeEnvironment ? activeEnvironment._id : 'n/a'}
/>
<SettingsModal
ref={registerModal}
handleExportWorkspaceToFile={this._handleExportWorkspaceToFile}
handleExportAllToFile={handleExportFile}
handleImportFile={this._handleImportFile}
settings={settings}
/>
<RequestSwitcherModal
ref={registerModal}
2016-11-26 07:33:55 +00:00
workspaceChildren={workspaceChildren}
2016-11-25 23:09:17 +00:00
workspaceId={activeWorkspace._id}
activeRequestParentId={activeRequest ? activeRequest.parentId : activeWorkspace._id}
activateRequest={handleActivateRequest}
handleSetActiveWorkspace={handleSetActiveWorkspace}
/>
<WorkspaceEnvironmentsEditModal
ref={registerModal}
onChange={models.workspace.update}/>
</div>
)
}
}
Wrapper.propTypes = {
// Helper Functions
2016-11-25 23:09:17 +00:00
handleActivateRequest: PropTypes.func.isRequired,
handleSetSidebarFilter: PropTypes.func.isRequired,
handleImportFileToWorkspace: PropTypes.func.isRequired,
handleExportFile: PropTypes.func.isRequired,
handleSetActiveWorkspace: PropTypes.func.isRequired,
handleSetActiveEnvironment: PropTypes.func.isRequired,
handleMoveRequest: PropTypes.func.isRequired,
handleMoveRequestGroup: PropTypes.func.isRequired,
handleCreateRequest: PropTypes.func.isRequired,
handleDuplicateRequest: PropTypes.func.isRequired,
2016-11-28 07:12:17 +00:00
handleDuplicateRequestGroup: PropTypes.func.isRequired,
handleCreateRequestGroup: PropTypes.func.isRequired,
2016-11-28 07:12:17 +00:00
handleGenerateCode: PropTypes.func.isRequired,
2016-11-25 23:09:17 +00:00
handleCreateRequestForWorkspace: PropTypes.func.isRequired,
handleSetRequestPaneRef: PropTypes.func.isRequired,
handleSetResponsePaneRef: PropTypes.func.isRequired,
handleSetResponsePreviewMode: PropTypes.func.isRequired,
handleSetResponseFilter: PropTypes.func.isRequired,
handleSetActiveResponse: PropTypes.func.isRequired,
handleSetSidebarRef: PropTypes.func.isRequired,
handleStartDragSidebar: PropTypes.func.isRequired,
handleResetDragSidebar: PropTypes.func.isRequired,
handleStartDragPane: PropTypes.func.isRequired,
handleResetDragPane: PropTypes.func.isRequired,
handleSetRequestGroupCollapsed: PropTypes.func.isRequired,
handleSendRequestWithEnvironment: PropTypes.func.isRequired,
// Properties
loadStartTime: PropTypes.number.isRequired,
isLoading: PropTypes.bool.isRequired,
paneWidth: PropTypes.number.isRequired,
responsePreviewMode: PropTypes.string.isRequired,
responseFilter: PropTypes.string.isRequired,
activeResponseId: PropTypes.string.isRequired,
sidebarWidth: PropTypes.number.isRequired,
sidebarHidden: PropTypes.bool.isRequired,
sidebarFilter: PropTypes.string.isRequired,
sidebarChildren: PropTypes.arrayOf(PropTypes.object).isRequired,
settings: PropTypes.object.isRequired,
workspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
2016-11-26 07:33:55 +00:00
workspaceChildren: PropTypes.arrayOf(PropTypes.object).isRequired,
environments: PropTypes.arrayOf(PropTypes.object).isRequired,
// Optional
activeEnvironment: PropTypes.object,
};
export default Wrapper;