aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/src/app/first-time/confirm-seed-screen.js
blob: 7c043143129c0c59eca1bfcac5dffed6f314d57a (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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import classnames from 'classnames'
import shuffle from 'lodash.shuffle'
import { compose } from 'recompose'
import Identicon from '../../../../ui/app/components/identicon'
import { confirmSeedWords, showModal } from '../../../../ui/app/actions'
import Breadcrumbs from './breadcrumbs'
import LoadingScreen from './loading-screen'
import { DEFAULT_ROUTE, INITIALIZE_BACKUP_PHRASE_ROUTE } from '../../../../ui/app/routes'

class ConfirmSeedScreen extends Component {
  static propTypes = {
    isLoading: PropTypes.bool,
    address: PropTypes.string,
    seedWords: PropTypes.string,
    confirmSeedWords: PropTypes.func,
    history: PropTypes.object,
    openBuyEtherModal: PropTypes.func,
  };

  static defaultProps = {
    seedWords: '',
  }

  constructor (props) {
    super(props)
    const { seedWords } = props
    this.state = {
      selectedSeeds: [],
      shuffledSeeds: seedWords && shuffle(seedWords.split(' ')) || [],
    }
  }

  componentWillMount () {
    const { seedWords, history } = this.props

    if (!seedWords) {
      history.push(DEFAULT_ROUTE)
    }
  }

  handleClick () {
    const { confirmSeedWords, history, openBuyEtherModal } = this.props

    confirmSeedWords()
      .then(() => {
        history.push(DEFAULT_ROUTE)
        openBuyEtherModal()
      })
  }

  render () {
    const { seedWords, history } = this.props
    const { selectedSeeds, shuffledSeeds } = this.state
    const isValid = seedWords === selectedSeeds.map(([_, seed]) => seed).join(' ')

    return (
      <div className="first-time-flow">
      {
        this.props.isLoading
          ? <LoadingScreen loadingMessage="Creating your new account" />
          : (
            <div className="first-view-main-wrapper">
              <div className="first-view-main">
                <div className="backup-phrase">
                  <a
                    className="backup-phrase__back-button"
                    onClick={e => {
                      e.preventDefault()
                      history.push(INITIALIZE_BACKUP_PHRASE_ROUTE)
                    }}
                    href="#"
                  >
                    {`< Back`}
                  </a>
                  <Identicon address={this.props.address} diameter={70} />
                  <div className="backup-phrase__content-wrapper">
                    <div>
                      <div className="backup-phrase__title">
                        Confirm your Secret Backup Phrase
                      </div>
                      <div className="backup-phrase__body-text">
                        Please select each phrase in order to make sure it is correct.
                      </div>
                      <div className="backup-phrase__confirm-secret">
                        {selectedSeeds.map(([_, word], i) => (
                          <button
                            key={i}
                            className="backup-phrase__confirm-seed-option"
                          >
                            {word}
                          </button>
                        ))}
                      </div>
                      <div className="backup-phrase__confirm-seed-options">
                        {shuffledSeeds.map((word, i) => {
                          const isSelected = selectedSeeds
                            .filter(([index, seed]) => seed === word && index === i)
                            .length

                          return (
                            <button
                              key={i}
                              className={classnames('backup-phrase__confirm-seed-option', {
                                'backup-phrase__confirm-seed-option--selected': isSelected,
                              })}
                              onClick={() => {
                                if (!isSelected) {
                                  this.setState({
                                    selectedSeeds: [...selectedSeeds, [i, word]],
                                  })
                                } else {
                                  this.setState({
                                    selectedSeeds: selectedSeeds
                                      .filter(([index, seed]) => !(seed === word && index === i)),
                                  })
                                }
                              }}
                            >
                              {word}
                            </button>
                          )
                        })}
                      </div>
                      <button
                        className="first-time-flow__button"
                        onClick={() => isValid && this.handleClick()}
                        disabled={!isValid}
                      >
                        Confirm
                      </button>
                    </div>
                  </div>
                  <Breadcrumbs total={3} currentIndex={1} />
                </div>
              </div>
            </div>
          )
      }
      </div>
    )
  }
}

export default compose(
  withRouter,
  connect(
    ({ metamask: { selectedAddress, seedWords }, appState: { isLoading } }) => ({
      seedWords,
      isLoading,
      address: selectedAddress,
    }),
    dispatch => ({
      confirmSeedWords: () => dispatch(confirmSeedWords()),
      openBuyEtherModal: () => dispatch(showModal({ name: 'DEPOSIT_ETHER'})),
    })
  )
)(ConfirmSeedScreen)