aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/pages/notice.js
blob: 2329a9147b732acb17a1a5bbcdcf3a9960ce75c0 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { Component } = require('react')
const h = require('react-hyperscript')
const { connect } = require('react-redux')
const PropTypes = require('prop-types')
const ReactMarkdown = require('react-markdown')
const linker = require('extension-link-enabler')
const generateLostAccountsNotice = require('../../../lib/lost-accounts-notice')
const findDOMNode = require('react-dom').findDOMNode
const actions = require('../../actions')
const { DEFAULT_ROUTE } = require('../../routes')

class Notice extends Component {
  constructor (props) {
    super(props)

    this.state = {
      disclaimerDisabled: true,
    }
  }

  componentWillMount () {
    if (!this.props.notice) {
      this.props.history.push(DEFAULT_ROUTE)
    }
  }

  componentDidMount () {
    // eslint-disable-next-line react/no-find-dom-node
    var node = findDOMNode(this)
    linker.setupListener(node)
    if (document.getElementsByClassName('notice-box')[0].clientHeight < 310) {
      this.setState({ disclaimerDisabled: false })
    }
  }

  componentWillReceiveProps (nextProps) {
    if (!nextProps.notice) {
      this.props.history.push(DEFAULT_ROUTE)
    }
  }

  componentWillUnmount () {
    // eslint-disable-next-line react/no-find-dom-node
    var node = findDOMNode(this)
    linker.teardownListener(node)
  }

  handleAccept () {
    this.setState({ disclaimerDisabled: true })
    this.props.onConfirm()
  }

  render () {
    const { notice = {} } = this.props
    const { title, date, body } = notice
    const { disclaimerDisabled } = this.state

    return (
      h('.flex-column.flex-center.flex-grow', {
        style: {
          width: '100%',
        },
      }, [
        h('h3.flex-center.text-transform-uppercase.terms-header', {
          style: {
            background: '#EBEBEB',
            color: '#AEAEAE',
            width: '100%',
            fontSize: '20px',
            textAlign: 'center',
            padding: 6,
          },
        }, [
          title,
        ]),

        h('h5.flex-center.text-transform-uppercase.terms-header', {
          style: {
            background: '#EBEBEB',
            color: '#AEAEAE',
            marginBottom: 24,
            width: '100%',
            fontSize: '20px',
            textAlign: 'center',
            padding: 6,
          },
        }, [
          date,
        ]),

        h('style', `

          .markdown {
            overflow-x: hidden;
          }

          .markdown h1, .markdown h2, .markdown h3 {
            margin: 10px 0;
            font-weight: bold;
          }

          .markdown strong {
            font-weight: bold;
          }
          .markdown em {
            font-style: italic;
          }

          .markdown p {
            margin: 10px 0;
          }

          .markdown a {
            color: #df6b0e;
          }

        `),

        h('div.markdown', {
          onScroll: (e) => {
            var object = e.currentTarget
            if (object.offsetHeight + object.scrollTop + 100 >= object.scrollHeight) {
              this.setState({ disclaimerDisabled: false })
            }
          },
          style: {
            background: 'rgb(235, 235, 235)',
            height: '310px',
            padding: '6px',
            width: '90%',
            overflowY: 'scroll',
            scroll: 'auto',
          },
        }, [
          h(ReactMarkdown, {
            className: 'notice-box',
            source: body,
            skipHtml: true,
          }),
        ]),

        h('button.primary', {
          disabled: disclaimerDisabled,
          onClick: () => this.handleAccept(),
          style: {
            marginTop: '18px',
          },
        }, 'Accept'),
      ])
    )
  }

}

const mapStateToProps = state => {
  const { metamask } = state
  const { noActiveNotices, lastUnreadNotice, lostAccounts } = metamask

  return {
    noActiveNotices,
    lastUnreadNotice,
    lostAccounts,
  }
}

Notice.propTypes = {
  notice: PropTypes.object,
  onConfirm: PropTypes.func,
  history: PropTypes.object,
}

const mapDispatchToProps = dispatch => {
  return {
    markNoticeRead: lastUnreadNotice => dispatch(actions.markNoticeRead(lastUnreadNotice)),
    markAccountsFound: () => dispatch(actions.markAccountsFound()),
  }
}

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const { noActiveNotices, lastUnreadNotice, lostAccounts } = stateProps
  const { markNoticeRead, markAccountsFound } = dispatchProps

  let notice
  let onConfirm

  if (!noActiveNotices) {
    notice = lastUnreadNotice
    onConfirm = () => markNoticeRead(lastUnreadNotice)
  } else if (lostAccounts && lostAccounts.length > 0) {
    notice = generateLostAccountsNotice(lostAccounts)
    onConfirm = () => markAccountsFound()
  }

  return {
    ...stateProps,
    ...dispatchProps,
    ...ownProps,
    notice,
    onConfirm,
  }
}

module.exports = connect(mapStateToProps, mapDispatchToProps, mergeProps)(Notice)