insomnia/app/containers/App.js

238 lines
9.1 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-09 21:41:27 +00:00
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'
2016-03-20 04:47:43 +00:00
2016-03-23 05:58:16 +00:00
import Editor from '../components/base/Editor'
2016-04-15 05:23:54 +00:00
import Prompts from '../components/modals/Prompts'
2016-04-09 01:14:25 +00:00
import KeyValueEditor from '../components/base/KeyValueEditor'
2016-03-22 05:01:58 +00:00
import RequestBodyEditor from '../components/RequestBodyEditor'
2016-04-09 21:08:55 +00:00
import RequestAuthEditor from '../components/RequestAuthEditor'
2016-03-22 18:20:05 +00:00
import RequestUrlBar from '../components/RequestUrlBar'
2016-03-20 04:47:43 +00:00
import Sidebar from '../components/Sidebar'
2016-04-15 05:23:54 +00:00
import RequestGroupEnvironmentEditModal from '../components/modals/RequestGroupEnvironmentEditModal'
2016-03-20 04:47:43 +00:00
import * as GlobalActions from '../actions/global'
2016-04-09 21:08:55 +00:00
import * as RequestGroupActions from '../actions/requestGroups'
import * as RequestActions from '../actions/requests'
2016-04-16 23:24:57 +00:00
import * as db from '../database'
2016-03-20 04:47:43 +00:00
// Don't inject component styles (use our own)
Tabs.setUseDefaultStyles(false);
2016-03-20 04:47:43 +00:00
class App extends Component {
2016-04-09 21:08:55 +00:00
_renderPageBody (actions, activeRequest, activeResponse, tabs) {
if (!activeRequest) {
return <div></div>;
}
2016-03-20 20:42:27 +00:00
return (
2016-04-09 19:10:34 +00:00
<div className="grid__cell grid grid--collapse">
<section className="grid__cell section">
<div className="grid--v wide">
<div className="grid__cell grid__cell--no-flex section__header">
<RequestUrlBar
2016-04-16 23:24:57 +00:00
sendRequest={actions.requests.send}
onUrlChange={url => {db.update(activeRequest, {url})}}
onMethodChange={method => {db.update(activeRequest, {method})}}
2016-04-06 04:21:42 +00:00
request={activeRequest}/>
2016-04-09 19:10:34 +00:00
</div>
<Tabs className="grid__cell grid--v section__body"
2016-04-16 23:24:57 +00:00
onSelect={i => actions.global.selectTab('request', i)}
2016-04-09 19:10:34 +00:00
selectedIndex={tabs.request || 0}>
<TabList className="grid grid--start">
<Tab><button className="btn btn--compact">Body</button></Tab>
<Tab>
<button className="btn btn--compact no-wrap">
Params {activeRequest.params.length ? `(${activeRequest.params.length})` : ''}
</button>
</Tab>
<Tab><button className="btn btn--compact">Auth</button></Tab>
<Tab>
<button className="btn btn--compact no-wrap">
Headers {activeRequest.headers.length ? `(${activeRequest.headers.length})` : ''}
</button>
</Tab>
</TabList>
2016-04-12 06:03:52 +00:00
<TabPanel className="grid__cell">
2016-04-09 19:10:34 +00:00
<RequestBodyEditor
2016-04-16 23:24:57 +00:00
onChange={body => {db.update(activeRequest, {body})}}
2016-04-09 19:10:34 +00:00
request={activeRequest}/>
</TabPanel>
2016-04-12 06:03:52 +00:00
<TabPanel className="grid__cell grid__cell--scroll--v">
2016-04-15 02:13:49 +00:00
<div className="wide pad">
2016-04-12 06:03:52 +00:00
<KeyValueEditor
pairs={activeRequest.params}
2016-04-16 23:24:57 +00:00
onChange={params => {db.update(activeRequest, {params})}}
2016-04-12 06:03:52 +00:00
/>
</div>
2016-04-09 19:10:34 +00:00
</TabPanel>
2016-04-12 06:03:52 +00:00
<TabPanel className="grid__cell grid__cell--scroll--v">
2016-04-15 02:13:49 +00:00
<div className="wide pad">
2016-04-12 06:03:52 +00:00
<RequestAuthEditor
request={activeRequest}
2016-04-16 23:24:57 +00:00
onChange={authentication => {db.update(activeRequest, {authentication})}}
2016-04-12 06:03:52 +00:00
/>
</div>
2016-04-09 21:08:55 +00:00
</TabPanel>
2016-04-12 06:03:52 +00:00
<TabPanel className="grid__cell grid__cell--scroll--v">
2016-04-15 02:13:49 +00:00
<div className="wide pad">
2016-04-12 06:03:52 +00:00
<KeyValueEditor
pairs={activeRequest.headers}
2016-04-16 23:24:57 +00:00
onChange={headers => {db.update(activeRequest, {headers})}}
2016-04-12 06:03:52 +00:00
/>
</div>
2016-04-09 19:10:34 +00:00
</TabPanel>
</Tabs>
</div>
</section>
2016-04-09 19:10:34 +00:00
<section className="grid__cell section">
<div className="grid--v wide">
<header
2016-04-12 06:03:52 +00:00
className="grid grid--center header text-center bg-light txt-sm section__header">
<div className="tag success">
<strong>{activeResponse && activeResponse.statusCode}</strong>&nbsp;SUCCESS
</div>
2016-04-09 19:10:34 +00:00
<div className="tag">TIME&nbsp;<strong>143ms</strong></div>
</header>
2016-04-09 21:08:55 +00:00
<Tabs className="grid__cell grid--v section__body"
onSelect={i => actions.selectTab('response', i)}
selectedIndex={tabs.response || 0}>
2016-04-09 19:10:34 +00:00
<TabList className="grid grid--start">
<Tab><button className="btn btn--compact">Preview</button></Tab>
<Tab><button className="btn btn--compact">Raw</button></Tab>
<Tab><button className="btn btn--compact">Headers</button></Tab>
</TabList>
<TabPanel className="grid__cell">
<Editor
2016-04-10 02:58:48 +00:00
value={activeResponse && activeResponse.body || ''}
prettify={true}
2016-04-09 19:10:34 +00:00
options={{
2016-04-10 02:58:48 +00:00
mode: activeResponse && activeResponse.contentType || 'text/plain',
2016-04-09 21:08:55 +00:00
readOnly: true,
placeholder: 'nothing yet...'
}}
2016-04-09 19:10:34 +00:00
/>
</TabPanel>
<TabPanel className="grid__cell">
<Editor
2016-04-09 21:08:55 +00:00
value={activeResponse && activeResponse.body || ''}
2016-04-09 19:10:34 +00:00
options={{
2016-04-12 00:39:49 +00:00
lineWrapping: true,
2016-04-09 21:08:55 +00:00
mode: 'text/plain',
readOnly: true,
placeholder: 'nothing yet...'
}}
2016-04-09 19:10:34 +00:00
/>
</TabPanel>
2016-04-12 06:03:52 +00:00
<TabPanel className="grid__cell grid__cell--scroll--v">
<div className="wide">
<div className="grid--v grid--start pad">
{!activeResponse ? null : activeResponse.headers.map((h, i) => (
<div className="grid grid__cell grid__cell--no-flex selectable" key={i}>
<div className="grid__cell">{h.name}</div>
<div className="grid__cell">{h.value}</div>
</div>
))}
</div>
2016-04-10 06:37:22 +00:00
</div>
</TabPanel>
2016-04-09 19:10:34 +00:00
</Tabs>
</div>
</section>
</div>
2016-03-20 04:47:43 +00:00
)
}
2016-03-22 05:01:58 +00:00
2016-03-20 04:47:43 +00:00
render () {
2016-04-15 05:23:54 +00:00
const {actions, requests, responses, requestGroups, tabs, modals} = this.props;
2016-04-16 23:24:57 +00:00
const activeRequest = requests.all.find(r => r._id === requests.active);
const activeResponse = responses[activeRequest && activeRequest._id];
2016-03-20 04:47:43 +00:00
return (
2016-04-11 00:40:14 +00:00
<div className="grid bg-super-dark tall">
2016-04-15 05:23:54 +00:00
<Prompts />
{!modals.find(m => m.id === RequestGroupEnvironmentEditModal.defaultProps.id) ? null : (
<RequestGroupEnvironmentEditModal
onClose={() => actions.hideModal(RequestGroupEnvironmentEditModal.defaultProps.id)}
onChange={v => console.log(v)}
/>
)}
2016-03-20 04:47:43 +00:00
<Sidebar
2016-04-16 23:24:57 +00:00
activateRequest={actions.requests.activate}
changeFilter={actions.requests.changeFilter}
addRequestToRequestGroup={requestGroup => db.requestCreate({parent: requestGroup._id})}
toggleRequestGroup={requestGroup => db.requestGroupToggle(requestGroup)}
2016-03-20 23:20:00 +00:00
activeRequest={activeRequest}
2016-03-23 05:26:27 +00:00
activeFilter={requests.filter}
2016-03-24 10:20:11 +00:00
requestGroups={requestGroups.all}
2016-03-22 05:01:58 +00:00
requests={requests.all}/>
2016-04-05 20:49:28 +00:00
<div className="grid__cell grid--v">
2016-04-06 07:19:36 +00:00
{/*<header className="header bg-light">
2016-04-07 03:09:14 +00:00
<div className="header__content"><h1>Hi World</h1></div>
</header>*/}
2016-04-09 21:08:55 +00:00
{this._renderPageBody(actions, activeRequest, activeResponse, tabs)}
2016-03-21 05:47:49 +00:00
</div>
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({
activate: PropTypes.func.isRequired,
update: PropTypes.func.isRequired,
remove: PropTypes.func.isRequired,
send: PropTypes.func.isRequired,
changeFilter: PropTypes.func.isRequired
}),
requestGroups: PropTypes.shape({
remove: PropTypes.func.isRequired,
update: PropTypes.func.isRequired,
toggle: PropTypes.func.isRequired
}),
global: PropTypes.shape({
selectTab: PropTypes.func.isRequired
})
2016-03-22 05:01:58 +00:00
}).isRequired,
2016-04-09 21:08:55 +00:00
requestGroups: PropTypes.shape({
all: PropTypes.array.isRequired
}).isRequired,
2016-03-22 05:01:58 +00:00
requests: PropTypes.shape({
all: PropTypes.array.isRequired,
active: PropTypes.string // "required" but can be null
}).isRequired,
2016-04-09 21:08:55 +00:00
responses: PropTypes.object.isRequired,
2016-04-15 05:23:54 +00:00
tabs: PropTypes.object.isRequired,
modals: PropTypes.array.isRequired
2016-03-20 04:47:43 +00:00
};
function mapStateToProps (state) {
return {
actions: state.actions,
2016-03-22 05:01:58 +00:00
requests: state.requests,
2016-03-24 10:20:11 +00:00
requestGroups: state.requestGroups,
2016-04-09 21:08:55 +00:00
responses: state.responses,
2016-04-15 05:23:54 +00:00
tabs: state.tabs,
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),
requestGroups: bindActionCreators(RequestGroupActions, dispatch),
requests: bindActionCreators(RequestActions, dispatch)
}
2016-03-20 04:47:43 +00:00
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);