insomnia/app/containers/App.js

178 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-03-20 04:47:43 +00:00
import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
2016-04-23 06:16:23 +00:00
import Prompts from './Prompts'
import EnvironmentEditModal from '../components/EnvironmentEditModal'
2016-05-07 17:29:24 +00:00
import SettingsModal from '../components/SettingsModal'
import RequestPane from '../components/RequestPane'
import ResponsePane from '../components/ResponsePane'
import Sidebar from '../components/Sidebar'
2016-03-20 04:47:43 +00:00
2016-04-23 06:16:23 +00:00
import * as GlobalActions from '../redux/modules/global'
import * as RequestGroupActions from '../redux/modules/requestGroups'
import * as RequestActions from '../redux/modules/requests'
import * as ModalActions from '../redux/modules/modals'
2016-04-16 23:24:57 +00:00
import * as db from '../database'
2016-03-20 04:47:43 +00:00
class App extends Component {
2016-04-18 04:39:15 +00:00
constructor (props) {
super(props);
this.state = {
activeResponse: null,
activeRequest: null
}
}
_generateSidebarTree (parentId, entities) {
const children = entities.filter(e => e.parentId === parentId);
2016-05-01 19:56:30 +00:00
if (children.length > 0) {
return children.map(c => ({
doc: c,
children: this._generateSidebarTree(c._id, entities)
}));
} else {
return children;
}
}
render () {
const {actions, modals, workspaces, requests, entities} = this.props;
2016-05-01 19:56:30 +00:00
// TODO: Factor this out into a selector
let workspace = entities.workspaces[workspaces.activeId];
if (!workspace) {
workspace = entities.workspaces[Object.keys(entities.workspaces)[0]];
}
const activeRequestId = workspace.activeRequestId;
const activeRequest = activeRequestId ? entities.requests[activeRequestId] : null;
2016-03-22 05:01:58 +00:00
const responses = Object.keys(entities.responses).map(id => entities.responses[id]);
const allRequests = Object.keys(entities.requests).map(id => entities.requests[id]);
const allRequestGroups = Object.keys(entities.requestGroups).map(id => entities.requestGroups[id]);
const activeResponse = responses.sort(
(a, b) => a._id > b._id ? -1 : 1
).find(r => r.parentId === activeRequestId);
const children = this._generateSidebarTree(
workspace._id,
allRequests.concat(allRequestGroups)
);
2016-03-20 04:47:43 +00:00
return (
2016-05-01 19:56:30 +00:00
<div className="wrapper">
2016-04-17 22:46:17 +00:00
<Sidebar
workspaceId={workspace._id}
activateRequest={r => db.workspaceUpdate(workspace, {activeRequestId: r._id})}
2016-04-17 22:46:17 +00:00
changeFilter={actions.requests.changeFilter}
addRequestToRequestGroup={requestGroup => db.requestCreate({parentId: requestGroup._id})}
toggleRequestGroup={requestGroup => db.requestGroupUpdate(requestGroup, {collapsed: !requestGroup.collapsed})}
activeRequestId={activeRequest ? activeRequest._id : null}
filter={requests.filter}
children={children}
/>
2016-05-07 17:29:24 +00:00
2016-05-01 19:56:30 +00:00
<RequestPane
request={activeRequest}
sendRequest={actions.requests.send}
updateRequestBody={body => db.requestUpdate(activeRequest, {body})}
updateRequestUrl={url => db.requestUpdate(activeRequest, {url})}
updateRequestMethod={method => db.requestUpdate(activeRequest, {method})}
updateRequestParams={params => db.requestUpdate(activeRequest, {params})}
updateRequestAuthentication={authentication => db.requestUpdate(activeRequest, {authentication})}
updateRequestHeaders={headers => db.requestUpdate(activeRequest, {headers})}
updateRequestContentType={contentType => db.requestUpdate(activeRequest, {contentType})}
/>
2016-05-07 17:29:24 +00:00
2016-05-01 19:56:30 +00:00
<ResponsePane
response={activeResponse}
previewMode={activeRequest ? activeRequest.previewMode : null}
updatePreviewMode={previewMode => db.requestUpdate(activeRequest, {previewMode})}
/>
2016-04-15 05:23:54 +00:00
<Prompts />
{modals.map(m => {
2016-05-07 17:29:24 +00:00
if (m.id === SettingsModal.defaultProps.id) {
return (
<SettingsModal
key={m.id}
onClose={() => actions.modals.hide(m.id)}
/>
)
} else if (m.id === EnvironmentEditModal.defaultProps.id) {
return (
<EnvironmentEditModal
key={m.id}
requestGroup={m.data.requestGroup}
onClose={() => actions.modals.hide(m.id)}
onChange={rg => db.requestGroupUpdate(m.data.requestGroup, {environment: rg.environment})}
/>
)
} else {
return null;
}
})}
2016-03-20 04:47:43 +00:00
</div>
)
}
}
App.propTypes = {
2016-03-22 05:01:58 +00:00
actions: PropTypes.shape({
2016-04-16 23:24:57 +00:00
requests: PropTypes.shape({
send: PropTypes.func.isRequired,
changeFilter: PropTypes.func.isRequired
}),
requestGroups: PropTypes.shape({
toggle: PropTypes.func.isRequired
}),
modals: PropTypes.shape({
hide: PropTypes.func.isRequired
2016-04-16 23:24:57 +00:00
})
2016-03-22 05:01:58 +00:00
}).isRequired,
entities: PropTypes.shape({
requests: PropTypes.object.isRequired,
requestGroups: PropTypes.object.isRequired,
responses: PropTypes.object.isRequired
}).isRequired,
workspaces: PropTypes.shape({
activeId: PropTypes.string
2016-04-09 21:08:55 +00:00
}).isRequired,
2016-03-22 05:01:58 +00:00
requests: PropTypes.shape({
filter: PropTypes.string.isRequired
2016-03-22 05:01:58 +00:00
}).isRequired,
2016-04-15 05:23:54 +00:00
modals: PropTypes.array.isRequired
2016-03-20 04:47:43 +00:00
};
function mapStateToProps (state) {
return {
actions: state.actions,
workspaces: state.workspaces,
2016-03-22 05:01:58 +00:00
requests: state.requests,
entities: state.entities,
2016-04-15 05:23:54 +00:00
modals: state.modals
2016-03-20 04:47:43 +00:00
};
}
function mapDispatchToProps (dispatch) {
return {
2016-04-16 23:24:57 +00:00
actions: {
global: bindActionCreators(GlobalActions, dispatch),
modals: bindActionCreators(ModalActions, dispatch),
2016-04-16 23:24:57 +00:00
requestGroups: bindActionCreators(RequestGroupActions, dispatch),
requests: bindActionCreators(RequestActions, dispatch)
}
2016-03-20 04:47:43 +00:00
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);