insomnia/app/ui/containers/WorkspaceDropdown.js
Gregory Schier 46d3719b99 Sync Proof of Concept (#33)
* Maybe working POC

* Change to use remote url

* Other URL too

* Some logic

* Got the push part working

* Made some updates

* Fix

* Update

* Add status code check

* Stuff

* Implemented new sync api

* A bit more robust

* Debounce changes

* Change timeout

* Some fixes

* Remove .less

* Better error handling

* Fix base url

* Support for created vs updated docs

* Try silent

* Silence removal too

* Small fix after merge

* Fix test

* Stuff

* Implement key generation algorithm

* Tidy

* stuff

* A bunch of stuff for the new API

* Integrated the session stuff

* Stuff

* Just started on encryption

* Lots of updates to encryption

* Finished createResourceGroup function

* Full encryption/decryption working (I think)

* Encrypt localstorage with sessionID

* Some more

* Some extra checks

* Now uses separate DB. Still needs to be simplified a LOT

* Fix deletion bug

* Fixed unicode bug with encryption

* Simplified and working

* A bunch of polish

* Some stuff

* Removed some workspace meta properties

* Migrated a few more meta properties

* Small changes

* Fix body scrolling and url cursor jumping

* Removed duplication of webpack port

* Remove workspaces reduces

* Some small fixes

* Added sync modal and opt-in setting

* Good start to sync flow

* Refactored modal footer css

* Update sync status

* Sync logger

* A bit better logging

* Fixed a bunch of sync-related bugs

* Fixed signup form button

* Gravatar component

* Split sync modal into tabs

* Tidying

* Some more error handling

* start sending 'user agent

* Login/signup error handling

* Use real UUIDs

* Fixed tests

* Remove unused function

* Some extra checks

* Moved cloud sync setting to about page

* Some small changes

* Some things
2016-10-21 10:20:36 -07:00

185 lines
5.5 KiB
JavaScript

import React, {Component, PropTypes} from 'react';
import {ipcRenderer} from 'electron';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux'
import {shell} from 'electron';
import PromptButton from '../components/base/PromptButton';
import Dropdown from '../components/base/Dropdown';
import DropdownDivider from '../components/base/DropdownDivider';
import DropdownHint from '../components/base/DropdownHint';
import PromptModal from '../components/modals/PromptModal';
import AlertModal from '../components/modals/AlertModal';
import SettingsModal from '../components/modals/SettingsModal';
import ChangelogModal from '../components/modals/ChangelogModal';
import * as GlobalActions from '../redux/modules/global';
import * as db from '../../backend/database';
import {getAppVersion} from '../../backend/appInfo';
import {getModal} from '../components/modals/index';
class WorkspaceDropdown extends Component {
async _promptUpdateName () {
const workspace = this._getActiveWorkspace(this.props);
const name = await getModal(PromptModal).show({
headerName: 'Rename Workspace',
defaultValue: workspace.name
});
db.workspace.update(workspace, {name});
}
async _workspaceCreate () {
const name = await getModal(PromptModal).show({
headerName: 'Create New Workspace',
defaultValue: 'My Workspace',
submitName: 'Create',
selectText: true
});
const workspace = await db.workspace.create({name});
this.props.actions.global.activateWorkspace(workspace);
}
async _workspaceRemove () {
const count = await db.workspace.count();
if (count <= 1) {
getModal(AlertModal).show({
message: 'You cannot delete your last workspace'
});
} else {
const workspace = this._getActiveWorkspace(this.props);
db.workspace.remove(workspace);
}
}
_getActiveWorkspace (props) {
// TODO: Factor this out into a selector
const {entities, global} = props || this.props;
let workspace = entities.workspaces[global.activeWorkspaceId];
if (!workspace) {
workspace = entities.workspaces[Object.keys(entities.workspaces)[0]];
}
return workspace;
}
render () {
const {className, actions, global, entities, ...other} = this.props;
const allWorkspaces = Object.keys(entities.workspaces).map(id => entities.workspaces[id]);
const workspace = this._getActiveWorkspace(this.props);
return (
<Dropdown {...other} className={className + ' wide workspace-dropdown'}>
<button className="btn wide">
<h1 className="no-pad text-left">
<div className="pull-right">
{global.loading ?
<i className="fa fa-refresh fa-spin txt-lg"></i> : ''}&nbsp;
<i className="fa fa-caret-down"></i>
</div>
{workspace.name}
</h1>
</button>
<ul>
<DropdownDivider name="Current Workspace"/>
<li>
<button onClick={e => this._promptUpdateName()}>
<i className="fa fa-pencil-square-o"></i> Rename
{" "}
<strong>{workspace.name}</strong>
</button>
</li>
<li>
<PromptButton onClick={e => this._workspaceRemove()} addIcon={true}>
<i className="fa fa-trash-o"></i> Delete
{" "}
<strong>{workspace.name}</strong>
</PromptButton>
</li>
<DropdownDivider name="Workspaces"/>
{allWorkspaces.map(w => {
return w._id === workspace._id ? null : (
<li key={w._id}>
<button onClick={() => actions.global.activateWorkspace(w)}>
<i className="fa fa-random"></i> Switch to
{" "}
<strong>{w.name}</strong>
</button>
</li>
)
})}
<li>
<button onClick={e => this._workspaceCreate()}>
<i className="fa fa-blank"></i> New Workspace
</button>
</li>
<DropdownDivider name={`Insomnia Version ${getAppVersion()}`}/>
<li>
<button onClick={e => getModal(SettingsModal).show(2)}>
<i className="fa fa-share"></i> Import/Export
</button>
</li>
<li>
<button onClick={e => getModal(SettingsModal).show()}>
<i className="fa fa-cog"></i> Settings
<DropdownHint char=","></DropdownHint>
</button>
</li>
<li>
<button onClick={e => getModal(ChangelogModal).show()}>
<i className="fa fa-blank"></i> Changelog
</button>
</li>
</ul>
</Dropdown>
)
}
}
WorkspaceDropdown.propTypes = {
global: PropTypes.shape({
loading: PropTypes.bool.isRequired,
activeWorkspaceId: PropTypes.string.isRequired
}),
entities: PropTypes.shape({
workspaces: PropTypes.object.isRequired
}).isRequired,
actions: PropTypes.shape({
global: PropTypes.shape({
importFile: PropTypes.func.isRequired,
exportFile: PropTypes.func.isRequired,
activateWorkspace: PropTypes.func.isRequired,
})
})
};
function mapStateToProps (state) {
return {
global: state.global,
entities: state.entities,
actions: state.actions
};
}
function mapDispatchToProps (dispatch) {
return {
actions: {
global: bindActionCreators(GlobalActions, dispatch)
}
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(WorkspaceDropdown);