import React from 'react'; import { Grid, Row, Col, FormControl, FormGroup, ControlLabel, HelpBlock, Button, Alert } from 'react-bootstrap'; import { AsyncButton, Card, WaitIcon } from '../components/index'; import { validateForm } from '../commons/validator'; import { server } from '../commons/server'; import SessionUtils from './session-utils'; /** * Form validation model */ const form = { password: { required: true, password: true } }; export default class Sync extends React.Component { constructor(props) { super(props); this.renderContent = this.renderContent.bind(this); this.startSync = this.startSync.bind(this); this.checkStatusUntilFinish = this.checkStatusUntilFinish.bind(this); } /** * Checks if sync is already running * @return {[type]} [description] */ componentWillMount() { this.setState({ checking: true }); server.get('/api/offline/client/sync/status') .then(res => { if (res.id !== 'NOT_RUNNING') { // update phase this.setState({ phase: res }); // schedule next status checking setTimeout(this.checkStatusUntilFinish, 800); } this.setState({ checking: false }); return res; }); } /** * Called when user clicks on the login button */ gotoHome() { SessionUtils.gotoHome(); } /** * Check sync status until it finishes * @return {[type]} [description] */ checkStatusUntilFinish() { server.get('/api/offline/client/sync/status') .then(res => { if (res.id === 'NOT_RUNNING') { // initialization has succesfully finished this.setState({ phase: undefined, success: true }); } else if (res.id === 'ERROR') { // initialization has an error this.setState({ phase: undefined, error: true }); } else { // initialization is running // update phase this.setState({ phase: res }); // schedule next status checking setTimeout(this.checkStatusUntilFinish, 800); } }); } /** * Starts sync process * @return {[type]} [description] */ startSync() { // clear previous global msgs this.setState({ globalMsgs: null }); const vals = validateForm(this, form); // there is any validation error ? if (vals.errors) { this.setState({ errors: vals.errors }); return; } this.setState({ errors: {}, fetching: true }); const req = vals.value; server.post('/api/offline/client/sync/synchronize', req) .then(res => { if (res.errors) { this.setState({ globalMsgs: res.errors, fetching: false }); return null; } setTimeout(this.checkStatusUntilFinish, 800); return res; }) .catch(err => { this.setState({ fetching: false }); return Promise.reject(err); }); } /** * Checks in whitch phase sync is and returns the render content * @return {[type]} [description] */ renderContent() { if (this.state.checking) { return this.renderChecking(); } if (this.state.success) { return this.renderSuccess(); } if (this.state.error) { return this.renderError(); } if (!this.state.phase) { return this.renderNotRunning(); } if (this.state.phase) { return this.renderInProgress(); } return null; } /** * Render content when system is checking is sync is already running * @return {[type]} [description] */ renderChecking() { return (
{__('init.offinit.success2')}
{__('init.error.msg2')}