insomnia/app/containers/WorkspaceDropdown.js

173 lines
5.3 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
2016-04-23 06:53:22 +00:00
import electron from 'electron'
2016-04-04 07:15:30 +00:00
import React, {Component, PropTypes} from 'react'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
2016-04-23 06:53:22 +00:00
2016-04-23 06:16:23 +00:00
import Dropdown from '../components/base/Dropdown'
2016-04-23 06:53:22 +00:00
import DropdownDivider from '../components/base/DropdownDivider'
2016-04-23 06:16:23 +00:00
import * as RequestGroupActions from '../redux/modules/requestGroups'
import * as WorkspaceActions from '../redux/modules/workspaces'
2016-04-23 06:16:23 +00:00
import * as db from '../database'
import importData from '../lib/import'
2016-04-04 07:15:30 +00:00
class WorkspaceDropdown extends Component {
_importDialog () {
const options = {
properties: ['openFile'],
filters: [{
name: 'Insomnia Imports', extensions: ['json']
}]
};
// TODO: Factor this out into a selector
const {entities, workspaces} = this.props;
let workspace = entities.workspaces[workspaces.activeId];
if (!workspace) {
workspace = entities.workspaces[Object.keys(entities.workspaces)[0]];
}
2016-04-23 06:53:22 +00:00
electron.remote.dialog.showOpenDialog(options, paths => {
paths.map(path => {
fs.readFile(path, 'utf8', (err, data) => {
err || importData(workspace, data);
})
})
});
}
_workspaceCreate () {
db.workspaceCreate({name: 'New Workspace'}).then(workspace => {
this.props.actions.workspaces.activate(workspace);
});
}
2016-04-04 07:15:30 +00:00
render () {
const {actions, loading, workspaces, entities, ...other} = this.props;
const allWorkspaces = Object.keys(entities.workspaces).map(id => entities.workspaces[id]);
// TODO: Factor this out into a selector
let workspace = entities.workspaces[workspaces.activeId];
if (!workspace) {
workspace = entities.workspaces[Object.keys(entities.workspaces)[0]];
}
2016-04-04 07:15:30 +00:00
return (
<Dropdown right={true} {...other} className="block">
2016-04-06 07:19:36 +00:00
<button className="btn header__content">
2016-04-05 20:49:28 +00:00
<div className="grid grid--center">
2016-04-15 02:13:49 +00:00
<div className="grid__cell">
<h1 className="no-pad">{workspace.name}</h1>
2016-04-15 02:13:49 +00:00
</div>
2016-04-04 07:15:30 +00:00
<div className="no-wrap">
{loading ? <i className="fa fa-refresh fa-spin txt-lg"></i> : ''}&nbsp;
<i className="fa fa-caret-down txt-lg"></i>
</div>
</div>
</button>
<ul>
<DropdownDivider name="Current Workspace"/>
<li>
<button onClick={e => db.requestCreate({parentId: workspace._id})}>
2016-04-23 06:53:22 +00:00
<i className="fa fa-plus-circle"></i> New Request
</button>
</li>
<li>
<button onClick={e => db.requestGroupCreate({parentId: workspace._id})}>
2016-04-23 06:53:22 +00:00
<i className="fa fa-folder"></i> New Request Group
</button>
</li>
{/*<li>
<button onClick={e => actions.requestGroups.showEnvironmentEditModal()}>
2016-04-23 06:53:22 +00:00
<i className="fa fa-code"></i> Manage Environments
</button>
</li>*/}
<li>
<button onClick={e => this._importDialog()}>
<i className="fa fa-share-square-o"></i> Import/Export
</button>
</li>
2016-04-23 06:53:22 +00:00
<li>
<button onClick={e => actions.workspaces.showUpdateNamePrompt(workspace)}>
<i className="fa fa-empty"></i> Rename <strong>{workspace.name}</strong>
2016-04-23 06:53:22 +00:00
</button>
</li>
<li>
<button onClick={e => db.remove(workspace)}>
<i className="fa fa-empty"></i> Delete <strong>{workspace.name}</strong>
2016-04-23 06:53:22 +00:00
</button>
</li>
<DropdownDivider name="Workspaces"/>
{allWorkspaces.map(w => {
return w._id === workspace._id ? null : (
<li key={w._id}>
<button onClick={() => actions.workspaces.activate(w)}>
<i className="fa fa-random"></i> Switch to <strong>{w.name}</strong>
</button>
</li>
)
})}
2016-04-23 06:53:22 +00:00
<li>
<button onClick={e => this._workspaceCreate()}>
2016-04-23 06:53:22 +00:00
<i className="fa fa-blank"></i> Create Workspace
</button>
</li>
<DropdownDivider name="Insomnia"/>
2016-04-23 06:53:22 +00:00
<li><button><i className="fa fa-cog"></i> Settings</button></li>
<li><button><i className="fa fa-blank"></i> Open New Window</button></li>
2016-04-04 07:15:30 +00:00
</ul>
</Dropdown>
)
}
}
WorkspaceDropdown.propTypes = {
loading: PropTypes.bool.isRequired,
workspaces: PropTypes.shape({
activeId: PropTypes.string
}),
entities: PropTypes.shape({
workspaces: PropTypes.object.isRequired
}).isRequired,
2016-04-04 07:15:30 +00:00
actions: PropTypes.shape({
requestGroups: PropTypes.shape({
showEnvironmentEditModal: PropTypes.func.isRequired
}),
workspaces: PropTypes.shape({
activate: PropTypes.func.isRequired,
showUpdateNamePrompt: PropTypes.func.isRequired
})
2016-04-04 07:15:30 +00:00
})
};
function mapStateToProps (state) {
return {
workspaces: state.workspaces,
entities: state.entities,
2016-04-04 07:15:30 +00:00
actions: state.actions,
2016-04-23 06:08:52 +00:00
loading: state.global.loading
2016-04-04 07:15:30 +00:00
};
}
function mapDispatchToProps (dispatch) {
return {
actions: {
requestGroups: bindActionCreators(RequestGroupActions, dispatch),
workspaces: bindActionCreators(WorkspaceActions, dispatch)
}
2016-04-04 07:15:30 +00:00
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(WorkspaceDropdown);