summaryrefslogtreecommitdiff
path: root/ui/app/components/main_panel/index.js
blob: b70af7c94a63c44a267814087686239f7e0399e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// The main panel manages the current account and the list of available accounts
//
// It displays multiple sections, one for each service.
//

import React from 'react'
import App from 'app'
import Login from 'components/login'
import Account from 'models/account'
import Spinner from 'components/spinner'

import './main_panel.less'
import AccountList from './account_list'
import UserSection from './user_section'
import EmailSection from './email_section'
import VPNSection from './vpn_section'

export default class MainPanel extends React.Component {

  static get defaultProps() {return{
    initialAccount: null
  }}

  constructor(props) {
    super(props)
    this.state = {
      account: null,
      provider: null,
      accounts: []
    }
    this.activateAccount = this.activateAccount.bind(this)
    this.removeAccount = this.removeAccount.bind(this)
  }

  componentWillMount() {
    if (this.props.initialAccount) {
      this.activateAccount(this.props.initialAccount)
    }
  }

  activateAccount(account) {
    account.getProvider().then(provider => {
      this.setState({
        account: account,
        provider: provider,
        accounts: Account.list
      })
    })
  }

  removeAccount(account) {
    Account.remove(account).then(
      newActiveAccount => {
        if (newActiveAccount == null) {
          App.start()
        } else {
          this.activateAccount(newActiveAccount)
        }
      },
      error => {
        console.log(error)
      }
    )
  }

  render() {
    if (this.state.account && this.state.provider) {
      return this.renderPanel()
    } else {
      return <div className="main-panel">
      </div>
    }
  }

  renderPanel() {
    let emailSection = null
    let vpnSection = null
    let sidePanel = null

    if (this.state.account.authenticated) {
      if (this.state.provider.hasEmail) {
        emailSection = <EmailSection account={this.state.account} />
      }
    }

    if (this.state.provider.hasVPN) {
      vpnSection = <VPNSection account={this.state.account} />
    }

    sidePanel = (
      <AccountList account={this.state.account}
        accounts={this.state.accounts}
        onSelect={this.activateAccount}
        onRemove={this.removeAccount}/>
    )

    return (
      <div className="main-panel">
        {sidePanel}
        <div className="body">
          <UserSection account={this.state.account} onLogin={this.activateAccount} onLogout={this.activateAccount}/>
          {vpnSection}
          {emailSection}
        </div>
      </div>
    )
  }

}