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'
8 class Backup extends React.Component {
11 this.connection = chainClient().connection
15 this.connection.request('/backup-wallet').then(resp => {
16 const date = new Date()
17 const dateStr = date.toLocaleDateString().split(' ')[0]
18 const timestamp = date.getTime()
19 const fileName = ['bytom-wallet-backup-', dateStr, timestamp].join('-')
21 var element = document.createElement('a')
22 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(resp.data)))
23 element.setAttribute('download', fileName)
24 element.style.display = 'none'
25 document.body.appendChild(element)
28 document.body.removeChild(element)
32 handleFileChange(event) {
33 const files = event.target.files
34 if (files.length <= 0) {
35 this.setState({key: null})
39 const fileReader = new FileReader()
40 fileReader.onload = fileLoadedEvent => {
41 const backupData = JSON.parse(fileLoadedEvent.target.result)
42 this.connection.request('/restore-wallet', backupData).then(resp => {
43 if (resp.status === 'fail') {
44 this.props.showError(new Error(resp.msg))
47 this.props.showRestoreSuccess()
50 fileReader.readAsText(files[0], 'UTF-8')
52 const fileElement = document.getElementById('bytom-restore-file-upload')
53 fileElement.value = ''
57 const element = document.getElementById('bytom-restore-file-upload')
62 const lang = this.props.core.lang
63 const newButton = <button className='btn btn-primary' onClick={this.backup.bind(this)}>
64 {lang === 'zh' ? '备份' : 'Backup'}
66 const restoreButton = <button className='btn btn-primary' onClick={this.restore.bind(this)}>
67 {lang === 'zh' ? '恢复' : 'Restore'}
71 <div className={componentClassNames(this, 'flex-container', styles.mainContainer)}>
72 <PageTitle title={lang === 'zh' ? '备份与恢复' : 'Backup and restore'}/>
77 <input id='bytom-restore-file-upload' type='file' style={{'display': 'none', 'alignItems': 'center', 'fontSize': '12px'}}
78 onChange={this.handleFileChange.bind(this)}/>
85 const mapStateToProps = (state) => ({
87 navAdvancedState: state.app.navAdvancedState,
90 const mapDispatchToProps = (dispatch) => ({
92 dispatch({type: 'ERROR', payload: err})
94 showRestoreSuccess: () => dispatch({type: 'RESTORE_SUCCESS'})
97 export default connect(