-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.tsx
116 lines (99 loc) · 2.84 KB
/
index.tsx
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
import { actions } from 'data'
import { bindActionCreators, compose, Dispatch } from 'redux'
import { BorrowMinMaxType } from 'data/types'
import {
CoinType,
LoanType,
OfferType,
PaymentValue,
RatesType,
RemoteDataType,
SupportedCoinsType
} from 'core/types'
import { connect } from 'react-redux'
import { getData } from './selectors'
import DataError from 'components/DataError'
import Loading from './template.loading'
import React, { PureComponent } from 'react'
import Success from './template.success'
export type OwnProps = {
handleClose: () => void
loan: LoanType
offer: OfferType
}
export type LinkDispatchPropsType = {
borrowActions: typeof actions.components.borrow
}
export type SuccessStateType = {
coin: CoinType
limits: BorrowMinMaxType
payment: PaymentValue
rates: RatesType
supportedCoins: SupportedCoinsType
}
type LinkStatePropsType = {
data: RemoteDataType<string | Error, SuccessStateType>
}
type Props = OwnProps & LinkDispatchPropsType & LinkStatePropsType
export type State = { isAddrCopied: boolean; showQrCode: boolean }
class BorrowForm extends PureComponent<Props, State> {
state = { isAddrCopied: false, showQrCode: false }
componentDidMount () {
this.props.borrowActions.initializeBorrow('BTC')
}
componentWillUnmount () {
this.props.borrowActions.destroy()
}
handleRefresh = () => {
this.props.borrowActions.initializeBorrow('BTC')
}
handleSubmit = () => {
this.props.borrowActions.addCollateral()
}
copyAddress = () => {
var input = document.createElement('input')
// TODO: Borrow make dynamic
input.setAttribute(
'value',
this.props.loan.collateral.depositAddresses['BTC']
)
document.body.appendChild(input)
input.select()
var result = document.execCommand('copy')
document.body.removeChild(input)
this.setState({ isAddrCopied: true })
setTimeout(() => {
this.setState({ isAddrCopied: false })
}, 2000)
return result
}
toggleQrCode = () => {
this.setState({ showQrCode: !this.state.showQrCode })
}
render () {
const { data } = this.props
return data.cata({
Success: val => (
<Success
{...val}
{...this.props}
{...this.state}
onSubmit={this.handleSubmit}
onCopyAddress={this.copyAddress}
onToggleQrCode={this.toggleQrCode}
/>
),
Failure: () => <DataError onClick={this.handleRefresh} />,
Loading: () => <Loading />,
NotAsked: () => <Loading />
})
}
}
const mapStateToProps = (state): LinkStatePropsType => ({
data: getData(state)
})
const mapDispatchToProps = (dispatch: Dispatch): LinkDispatchPropsType => ({
borrowActions: bindActionCreators(actions.components.borrow, dispatch)
})
const enhance = compose(connect(mapStateToProps, mapDispatchToProps))
export default enhance(BorrowForm)