2016-10-21 17:20:36 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
import SyncModal from './modals/SyncModal';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {showModal} from './modals/index';
|
2016-11-09 03:18:25 +00:00
|
|
|
import * as syncStorage from '../../sync/storage';
|
|
|
|
import * as session from '../../sync/session';
|
2016-10-21 17:20:36 +00:00
|
|
|
import SignupModal from './modals/SignupModal';
|
|
|
|
|
|
|
|
const STATE_OK = 'synced';
|
2016-11-07 20:24:38 +00:00
|
|
|
const STATE_AHEAD = 'dirty';
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
class SyncButton extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
state: STATE_OK,
|
|
|
|
loggedIn: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateState () {
|
2016-10-24 23:30:37 +00:00
|
|
|
const {workspaceId} = this.props;
|
2016-11-07 20:24:38 +00:00
|
|
|
const resource = await syncStorage.getResourceByDocId(workspaceId);
|
2016-10-24 23:30:37 +00:00
|
|
|
const resourceGroupId = resource ? resource.resourceGroupId : null;
|
2016-11-07 20:24:38 +00:00
|
|
|
const isDirty = await syncStorage.hasDirtyResourcesForResourceGroup(resourceGroupId);
|
2016-10-24 23:30:37 +00:00
|
|
|
let state;
|
2016-11-07 20:24:38 +00:00
|
|
|
if (isDirty) {
|
2016-10-24 23:30:37 +00:00
|
|
|
state = STATE_AHEAD;
|
|
|
|
} else {
|
|
|
|
state = STATE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loggedIn = session.isLoggedIn();
|
|
|
|
const newState = Object.assign({}, this.state, {state, loggedIn});
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// Only reset the state if something has changed
|
|
|
|
for (const k of Object.keys(newState)) {
|
|
|
|
if (newState[k] !== this.state[k]) {
|
|
|
|
this.setState(newState);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this._interval = setInterval(() => this._updateState(), 2000);
|
|
|
|
this._updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
if (session.isLoggedIn()) {
|
|
|
|
return (
|
2016-10-24 23:52:05 +00:00
|
|
|
<button className="btn btn--super-duper-compact btn--outlined wide ellipsis"
|
2016-11-07 20:24:38 +00:00
|
|
|
onClick={e => showModal(SyncModal)}>
|
2016-10-24 23:30:37 +00:00
|
|
|
Sync
|
2016-10-21 17:20:36 +00:00
|
|
|
{this.state.state ? <span> ({this.state.state})</span> : null}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
2016-10-25 01:23:49 +00:00
|
|
|
<button className="btn btn--super-duper-compact btn--outlined wide"
|
2016-11-07 20:24:38 +00:00
|
|
|
onClick={e => showModal(SignupModal)}>
|
2016-10-21 17:20:36 +00:00
|
|
|
Login to Cloud Sync
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
SyncButton.propTypes = {
|
|
|
|
workspaceId: PropTypes.string.isRequired
|
|
|
|
};
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
export default SyncButton;
|