aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/pages/settings/connections-tab/connections-tab.component.js
blob: 3309fcc0d9843affb43f2254ba7d160a3411479b (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import ConnectedSiteEntry from './connected-site-row'
import TextField from '../../../components/ui/text-field'
import Button from '../../../components/ui/button'

export default class ConnectionsTab extends PureComponent {
  static contextTypes = {
    t: PropTypes.func,
    metricsEvent: PropTypes.func,
  }

  static defaultProps = {
    activeTab: {},
  }

  static propTypes = {
    activeTab: PropTypes.object,
    approvedOrigins: PropTypes.object.isRequired,
    approveProviderRequestByOrigin: PropTypes.func.isRequired,
    rejectProviderRequestByOrigin: PropTypes.func.isRequired,
    showClearApprovalModal: PropTypes.func.isRequired,
  }

  state = {
    input: this.props.activeTab.origin || '',
  }

  handleAddOrigin = () => {
    const newOrigin = this.state.input
    this.setState({
      input: '',
    }, () => {
      if (newOrigin && newOrigin.trim()) {
        this.props.approveProviderRequestByOrigin(newOrigin)
      }
    })
  }

  renderNewOriginInput () {
    const { t } = this.context

    return (
      <div className="settings-page__content-row">
        <div className="settings-page__content-item">
          <span>{ t('addSite') }</span>
          <div className="settings-page__content-description">
            { t('addSiteDescription') }
          </div>
        </div>
        <div className="settings-page__content-item">
          <div className="settings-page__content-item-col">
            <TextField
              type="text"
              value={this.state.input}
              onChange={e => this.setState({ input: e.target.value })}
              fullWidth
              margin="dense"
              min={0}
            />
            <button
              className="button btn-primary settings-tab__rpc-save-button"
              onClick={this.handleAddOrigin}
            >
              { t('connect') }
            </button>
          </div>
        </div>
      </div>
    )
  }

  renderApprovedOriginsList () {
    const { t } = this.context
    const { approvedOrigins, rejectProviderRequestByOrigin, showClearApprovalModal } = this.props
    const approvedEntries = Object.entries(approvedOrigins)
    const approvalListEmpty = approvedEntries.length === 0

    return (
      <div className="settings-page__content-row">
        <div className="settings-page__content-item">
          <span>{ t('connected') }</span>
          <span className="settings-page__content-description">
            { t('connectedDescription') }
          </span>
        </div>
        <div className="settings-page__content-item">
          {
            approvalListEmpty
              ? <div><i className="fa fa-ban" /></div>
              : null
          }
          {
            approvedEntries.map(([origin, { siteTitle, siteImage }]) => (
              <ConnectedSiteEntry
                key={origin}
                origin={origin}
                siteTitle={siteTitle}
                siteImage={siteImage}
                onDelete={() => {
                  rejectProviderRequestByOrigin(origin)
                }}
              />
            ))
          }
        </div>
        <div className="settings-page__content-item-col">
          <Button
            disabled={approvalListEmpty}
            type="warning"
            large
            className="settings-tab__button--orange"
            onClick={event => {
              event.preventDefault()
              showClearApprovalModal()
            }}
          >
            { t('clearApprovalData') }
          </Button>
        </div>
      </div>
    )
  }

  render () {
    return (
      <div className="settings-page__body">
        { this.renderNewOriginInput() }
        { this.renderApprovedOriginsList() }
      </div>
    )
  }
}