2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-12-21 23:37:48 +00:00
|
|
|
import {Dropdown, DropdownButton, DropdownItem, DropdownDivider} from '../base/dropdown';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Link from '../base/link';
|
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
2016-12-21 23:37:48 +00:00
|
|
|
import * as session from '../../../sync/session';
|
2017-01-11 03:18:15 +00:00
|
|
|
import * as sync from '../../../sync/index';
|
2016-12-21 23:37:48 +00:00
|
|
|
import {showModal} from './index';
|
2017-03-08 05:52:17 +00:00
|
|
|
import PromptModal from './prompt-modal';
|
|
|
|
import PromptButton from '../base/prompt-button';
|
2017-01-11 18:35:44 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class WorkspaceShareSettingsModal extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {};
|
|
|
|
}
|
2017-01-11 03:18:15 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleSubmit (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
_handleClose () {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
2017-01-11 03:18:15 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
async _handleUnshare () {
|
2017-01-13 00:45:15 +00:00
|
|
|
if (!session.isLoggedIn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
const {resourceGroup} = this.state;
|
|
|
|
|
|
|
|
this._resetState({loading: true});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await session.unshareWithAllTeams(resourceGroup.id);
|
|
|
|
await this._load();
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Failed to unshare workspace', err);
|
|
|
|
this._resetState({error: err.message, loading: false});
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async _handleShareWithTeam (team) {
|
2017-01-11 03:18:15 +00:00
|
|
|
const passphrase = await showModal(PromptModal, {
|
|
|
|
headerName: 'Share Workspace',
|
|
|
|
label: 'Confirm password to share workspace',
|
|
|
|
placeholder: '•••••••••••••••••',
|
|
|
|
submitName: 'Share with Team',
|
2017-03-03 20:09:08 +00:00
|
|
|
inputType: 'password'
|
2017-01-11 03:18:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const {resourceGroup} = this.state;
|
|
|
|
this._resetState({loading: true});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await session.shareWithTeam(resourceGroup.id, team.id, passphrase);
|
|
|
|
await this._load();
|
|
|
|
} catch (err) {
|
|
|
|
this._resetState({error: err.message, loading: false});
|
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
|
|
|
async _load () {
|
|
|
|
if (!session.isLoggedIn()) {
|
2017-01-13 00:45:15 +00:00
|
|
|
this._resetState({});
|
|
|
|
return;
|
2016-12-21 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
const {workspace} = this.props;
|
|
|
|
const resource = await sync.getOrCreateResourceForDoc(workspace);
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-01-11 18:35:44 +00:00
|
|
|
const teams = await session.listTeams();
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-01-11 18:35:44 +00:00
|
|
|
try {
|
2017-01-11 20:58:37 +00:00
|
|
|
const resourceGroup = await sync.fetchResourceGroup(resource.resourceGroupId, true);
|
2017-01-11 18:35:44 +00:00
|
|
|
this.setState({teams, resourceGroup, loading: false, error: ''});
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Failed to fetch ResourceGroup', err);
|
|
|
|
this.setState({error: 'No sync info found. Please try again.', loading: false});
|
|
|
|
trackEvent('Sync', 'Error', 'Share Fetch Fail');
|
|
|
|
}
|
2017-01-11 03:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_resetState (patch = {}) {
|
|
|
|
this.setState(Object.assign({
|
|
|
|
teams: [],
|
|
|
|
resourceGroup: null,
|
|
|
|
error: '',
|
2017-03-03 20:09:08 +00:00
|
|
|
loading: false
|
2017-01-11 03:18:15 +00:00
|
|
|
}, patch));
|
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
async show () {
|
2016-12-21 23:37:48 +00:00
|
|
|
this.modal.show();
|
2017-01-11 03:18:15 +00:00
|
|
|
this._resetState();
|
|
|
|
await this._load();
|
2016-12-21 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-01-11 03:18:15 +00:00
|
|
|
componentWillMount () {
|
|
|
|
this._resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {teams, resourceGroup, error, loading} = this.state;
|
|
|
|
const {workspace} = this.props;
|
2016-12-21 23:37:48 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={this._handleSubmit}>
|
|
|
|
<Modal ref={this._setModalRef}>
|
|
|
|
<ModalHeader key="header">Share Workspace</ModalHeader>
|
2017-03-01 21:15:56 +00:00
|
|
|
<ModalBody key="body" className="pad text-center" noScroll>
|
2016-12-21 23:37:48 +00:00
|
|
|
<p>
|
2017-01-11 03:18:15 +00:00
|
|
|
Share <strong>{workspace.name}</strong> to automatically sync
|
2017-01-13 00:45:15 +00:00
|
|
|
your API workspace with your team members.
|
2016-12-21 23:37:48 +00:00
|
|
|
</p>
|
2017-01-11 03:18:15 +00:00
|
|
|
<div className="form-control pad">
|
|
|
|
{error ? <div className="danger">Oops: {error}</div> : null}
|
2017-03-01 21:15:56 +00:00
|
|
|
<Dropdown outline>
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownDivider>Teams</DropdownDivider>
|
2017-01-13 00:45:15 +00:00
|
|
|
{!loading ? (
|
|
|
|
resourceGroup && resourceGroup.teamId ? (
|
2017-01-11 03:18:15 +00:00
|
|
|
<DropdownButton className="btn btn--clicky">
|
|
|
|
<i className="fa fa-users"/> Shared with
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2017-01-11 03:18:15 +00:00
|
|
|
<strong>{resourceGroup.teamName}</strong>
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2017-01-11 03:18:15 +00:00
|
|
|
<i className="fa fa-caret-down"/>
|
|
|
|
</DropdownButton>
|
|
|
|
) : (
|
|
|
|
<DropdownButton className="btn btn--clicky">
|
|
|
|
<i className="fa fa-lock"/> Private <i className="fa fa-caret-down"/>
|
|
|
|
</DropdownButton>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<DropdownButton className="btn btn--clicky">
|
|
|
|
<i className="fa fa-spin fa-refresh"/> Loading...
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2017-01-11 03:18:15 +00:00
|
|
|
<i className="fa fa-caret-down"/>
|
|
|
|
</DropdownButton>
|
|
|
|
)}
|
2016-12-21 23:37:48 +00:00
|
|
|
{teams.map(team => (
|
2017-01-11 03:18:15 +00:00
|
|
|
<DropdownItem key={team.id} value={team} onClick={this._handleShareWithTeam}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<i className="fa fa-users"/> Share with <strong>{team.name}</strong>
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
2017-01-13 00:45:15 +00:00
|
|
|
{teams.length === 0 ? (
|
2017-03-01 21:15:56 +00:00
|
|
|
<DropdownItem disabled onClick={this._handleShareWithTeam}>
|
2017-01-13 00:45:15 +00:00
|
|
|
<i className="fa fa-warning"/> You have no teams
|
|
|
|
</DropdownItem>
|
|
|
|
) : null}
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownDivider>Other</DropdownDivider>
|
2017-03-01 21:15:56 +00:00
|
|
|
<DropdownItem addIcon buttonClass={PromptButton}
|
2017-01-11 03:18:15 +00:00
|
|
|
confirmMessage="Really make private?"
|
|
|
|
onClick={this._handleUnshare}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<i className="fa fa-lock"/> Private
|
|
|
|
</DropdownItem>
|
|
|
|
</Dropdown>
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
<Link button className="btn btn--super-compact inline-block"
|
2016-12-21 23:37:48 +00:00
|
|
|
href="https://insomnia.rest/app/teams/">
|
2017-01-13 00:45:15 +00:00
|
|
|
Manage Teams
|
2016-12-21 23:37:48 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter key="footer">
|
|
|
|
<button type="button" className="btn" onClick={this._handleClose}>
|
|
|
|
Done
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkspaceShareSettingsModal.propTypes = {
|
2017-03-03 20:09:08 +00:00
|
|
|
workspace: PropTypes.object.isRequired
|
2016-12-21 23:37:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default WorkspaceShareSettingsModal;
|