1 import React from 'react'
2 import componentClassNames from 'utility/componentClassNames'
3 import {PageContent, PageTitle} from 'features/shared/components'
4 import styles from './Backup.scss'
5 import {connect} from 'react-redux'
6 import {chainClient} from 'utility/environment'
7 import actions from 'actions'
9 class Backup extends React.Component {
19 value:event.target.value
24 chainClient().backUp.backup()
26 const date = new Date()
27 const dateStr = date.toLocaleDateString().split(' ')[0]
28 const timestamp = date.getTime()
29 const fileName = ['bytom-wallet-backup-', dateStr, timestamp].join('-')
31 let element = document.createElement('a')
32 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(resp.data)))
33 element.setAttribute('download', fileName)
34 element.style.display = 'none'
35 document.body.appendChild(element)
38 document.body.removeChild(element)
40 .catch(err => { throw {_error: err} })
43 handleFileChange(event) {
44 const files = event.target.files
45 if (files.length <= 0) {
46 this.setState({key: null})
50 const fileReader = new FileReader()
51 fileReader.onload = fileLoadedEvent => {
52 const backupData = JSON.parse(fileLoadedEvent.target.result)
53 this.props.restoreFile(backupData)
55 fileReader.readAsText(files[0], 'UTF-8')
57 const fileElement = document.getElementById('bytom-restore-file-upload')
58 fileElement.value = ''
62 const element = document.getElementById('bytom-restore-file-upload')
67 const lang = this.props.core.lang
69 const newButton = <button className={`btn btn-primary btn-lg ${styles.submit}`} onClick={this.backup.bind(this)}>
70 {lang === 'zh' ? '下载备份' : 'Download Backup'}
72 const restoreButton = <button className={`btn btn-primary btn-lg ${styles.submit}`} onClick={this.restore.bind(this)}>
73 {lang === 'zh' ? '选择备份文件' : 'Select Restore File'}
75 const rescanButton = <button className={`btn btn-primary btn-lg ${styles.submit}`} onClick={() => this.props.rescan()}>
76 {lang === 'zh' ? '重新扫描' : 'Rescan'}
80 <div className='flex-container'>
81 <PageTitle title={lang === 'zh' ? '备份与恢复' : 'Backup and Restore'}/>
84 <div onChange={e => this.setValue(e)}>
85 <div className={styles.choices}>
86 <div className={styles.choice_wrapper}>
88 <input className={styles.choice_radio_button}
92 <div className={`${styles.choice} ${styles.new} `}>
93 <span className={styles.choice_title}>{lang === 'zh' ?'备份':'Back Up'}</span>
95 This option will back up all data stored in this core,
96 including blockchain data, accounts, assets
103 <div className={styles.choice_wrapper}>
105 <input className={styles.choice_radio_button}
109 <div className={`${styles.choice} ${styles.join}`}>
110 <span className={styles.choice_title}>{lang === 'zh' ?'恢复':'Restore'}</span>
112 This option will restore the wallet data form files.
113 You might need to rescan your wallet, if you balance is not up to date
119 <div className={styles.choice_wrapper}>
121 <input className={styles.choice_radio_button}
125 <div className={`${styles.choice} ${styles.join}`}>
126 <span className={styles.choice_title}>{lang === 'zh' ?'重新扫描':'Rescan'}</span>
128 This option will rescan your wallet and update your balance.
137 <div className={styles.choices}>
140 this.state.value === 'backup'
141 &&<span className={styles.submitWrapper}>{newButton}</span>
147 this.state.value === 'restore'
149 <span className={styles.submitWrapper}>{restoreButton}</span>
151 <input id='bytom-restore-file-upload' type='file'
152 style={{'display': 'none', 'alignItems': 'center', 'fontSize': '12px'}}
153 onChange={this.handleFileChange.bind(this)}/>
157 this.state.value === 'rescan'
159 <span className={styles.submitWrapper}>{rescanButton}</span>
171 const mapStateToProps = (state) => ({
173 navAdvancedState: state.app.navAdvancedState,
176 const mapDispatchToProps = (dispatch) => ({
177 backup: () => dispatch(actions.backUp.backup()),
178 rescan: () => dispatch(actions.backUp.rescan()),
179 restoreFile: (backUpFile) => dispatch(actions.backUp.restore(backUpFile)),
182 export default connect(