aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/src/app/first-time/notice-screen.js
blob: 8cb6f10573a59b5ba245b4356c73f54fe61fdd10 (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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Markdown from 'react-markdown'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { compose } from 'recompose'
import debounce from 'lodash.debounce'
import { markNoticeRead } from '../../../../ui/app/actions'
import Identicon from '../../../../ui/app/components/identicon'
import Breadcrumbs from './breadcrumbs'
import { INITIALIZE_BACKUP_PHRASE_ROUTE } from '../../../../ui/app/routes'
import LoadingScreen from './loading-screen'

class NoticeScreen extends Component {
  static propTypes = {
    address: PropTypes.string.isRequired,
    nextUnreadNotice: PropTypes.shape({
      title: PropTypes.string,
      date: PropTypes.string,
      body: PropTypes.string,
    }),
    location: PropTypes.shape({
      state: PropTypes.shape({
        next: PropTypes.func.isRequired,
      }),
    }),
    markNoticeRead: PropTypes.func,
    history: PropTypes.object,
    isLoading: PropTypes.bool,
    noActiveNotices: PropTypes.bool,
  };

  static defaultProps = {
    nextUnreadNotice: {},
  };

  state = {
    atBottom: false,
  }

  componentDidMount () {
    if (this.props.noActiveNotices) {
      this.props.history.push(INITIALIZE_BACKUP_PHRASE_ROUTE)
    }

    this.onScroll()
  }

  acceptTerms = () => {
    const { markNoticeRead, nextUnreadNotice, history } = this.props
    markNoticeRead(nextUnreadNotice)
      .then(hasActiveNotices => {
        if (!hasActiveNotices) {
          history.push(INITIALIZE_BACKUP_PHRASE_ROUTE)
        } else {
          this.setState({ atBottom: false })
          this.onScroll()
        }
      })
  }

  onScroll = debounce(() => {
    if (this.state.atBottom) return

    const target = document.querySelector('.tou__body')
    const {scrollTop, offsetHeight, scrollHeight} = target
    const atBottom = scrollTop + offsetHeight >= scrollHeight

    this.setState({atBottom: atBottom})
  }, 25)

  render () {
    const {
      address,
      nextUnreadNotice: { title, body },
      isLoading,
    } = this.props
    const { atBottom } = this.state

    return (
      isLoading
        ? <LoadingScreen />
        : (
          <div className="first-time-flow">
            <div className="first-view-main-wrapper">
              <div className="first-view-main">
                <div
                  className="tou"
                  onScroll={this.onScroll}
                >
                  <Identicon address={address} diameter={70} />
                  <div className="tou__title">{title}</div>
                  <Markdown
                    className="tou__body markdown"
                    source={body}
                    skipHtml
                  />
                  <button
                    className="first-time-flow__button"
                    onClick={atBottom && this.acceptTerms}
                    disabled={!atBottom}
                  >
                    Accept
                  </button>
                  <Breadcrumbs total={3} currentIndex={2} />
                </div>
              </div>
            </div>
          </div>
        )
    )
  }
}

const mapStateToProps = ({ metamask, appState }) => {
  const { selectedAddress, nextUnreadNotice, noActiveNotices } = metamask
  const { isLoading } = appState

  return {
    address: selectedAddress,
    nextUnreadNotice,
    noActiveNotices,
    isLoading,
  }
}

export default compose(
  withRouter,
  connect(
    mapStateToProps,
    dispatch => ({
      markNoticeRead: notice => dispatch(markNoticeRead(notice)),
    })
  )
)(NoticeScreen)