import React, {Component, PropTypes} from 'react' import {connect} from 'react-redux' import {bindActionCreators} from 'redux' import {Tab, Tabs, TabList, TabPanel} from 'react-tabs' import Editor from '../components/base/Editor' import Prompts from './Prompts' import KeyValueEditor from '../components/base/KeyValueEditor' import RequestBodyEditor from '../components/RequestBodyEditor' import RequestAuthEditor from '../components/RequestAuthEditor' import RequestUrlBar from '../components/RequestUrlBar' import StatusTag from '../components/StatusTag' import SizeTag from '../components/SizeTag' import TimeTag from '../components/TimeTag' import Sidebar from '../components/Sidebar' import EnvironmentEditModal from '../components/EnvironmentEditModal' 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' import * as TabActions from '../redux/modules/tabs' import * as db from '../database' // Don't inject component styles (use our own) Tabs.setUseDefaultStyles(false); class App extends Component { constructor (props) { super(props); this.state = { activeResponse: null, activeRequest: null } } _renderRequestPanel (actions, activeRequest, tabs) { if (!activeRequest) { return (
) } return (
{db.update(activeRequest, {url})}} onMethodChange={method => {db.update(activeRequest, {method})}} request={activeRequest} />
actions.tabs.select('request', i)} selectedIndex={tabs.request || 0}> {db.update(activeRequest, {body})}} request={activeRequest}/>
{db.update(activeRequest, {params})}} />
{db.update(activeRequest, {authentication})}} />
{db.update(activeRequest, {headers})}} />
) } _renderResponsePanel (actions, activeResponse, tabs) { if (!activeResponse) { return (
) } return (
{!activeResponse ? null : (
)}
actions.tabs.select('response', i)} selectedIndex={tabs.response || 0}>
{!activeResponse ? null : activeResponse.headers.map((h, i) => (
{h.name}
{h.value}
))}
) } render () { const {actions, requests, responses, requestGroups, tabs, modals} = this.props; const activeRequest = requests.all.find(r => r._id === requests.active); const activeResponse = activeRequest ? responses[activeRequest._id] : undefined; return (
db.requestCreate({parent: requestGroup._id})} toggleRequestGroup={requestGroup => db.update(requestGroup, {collapsed: !requestGroup.collapsed})} activeRequest={activeRequest} activeFilter={requests.filter} requestGroups={requestGroups.all} requests={requests.all}/>
{this._renderRequestPanel(actions, activeRequest, tabs)} {this._renderResponsePanel(actions, activeResponse, tabs)}
{modals.map(m => { if (m.id === EnvironmentEditModal.defaultProps.id) { return ( actions.modals.hide(m.id)} onChange={rg => db.update(m.data.requestGroup, {environment: rg.environment})} /> ) } else { return null; } })}
) } } App.propTypes = { actions: PropTypes.shape({ 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 }), modals: PropTypes.shape({ hide: PropTypes.func.isRequired }), tabs: PropTypes.shape({ select: PropTypes.func.isRequired }) }).isRequired, requestGroups: PropTypes.shape({ all: PropTypes.array.isRequired }).isRequired, requests: PropTypes.shape({ all: PropTypes.array.isRequired, active: PropTypes.string // "required" but can be null }).isRequired, responses: PropTypes.object.isRequired, tabs: PropTypes.object.isRequired, modals: PropTypes.array.isRequired }; function mapStateToProps (state) { return { actions: state.actions, requests: state.requests, requestGroups: state.requestGroups, responses: state.responses, tabs: state.tabs, modals: state.modals }; } function mapDispatchToProps (dispatch) { return { actions: { global: bindActionCreators(GlobalActions, dispatch), tabs: bindActionCreators(TabActions, dispatch), modals: bindActionCreators(ModalActions, dispatch), requestGroups: bindActionCreators(RequestGroupActions, dispatch), requests: bindActionCreators(RequestActions, dispatch) } } } export default connect( mapStateToProps, mapDispatchToProps )(App);