-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.tsx
109 lines (95 loc) · 2.96 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
import { bindActionCreators, Dispatch } from 'redux'
import { connect, ConnectedProps } from 'react-redux'
import React, { PureComponent } from 'react'
import { actions } from 'data'
import {
CoinType,
FiatType,
InterestLimitsType,
InterestRateType,
RemoteDataType,
SupportedCoinsType
} from 'core/types'
import { InterestMinMaxType, RatesType } from 'data/types'
import DataError from 'components/DataError'
import { getData } from './selectors'
import Loading from './template.loading'
import Success from './template.success'
class DepositForm extends PureComponent<Props> {
componentDidMount () {
this.handleInitializeDepositForm()
}
handleDisplayToggle = isCoin => {
const { displayCoin } = this.props.data.getOrElse({
displayCoin: false
})
if (isCoin === displayCoin) return
this.props.formActions.clearFields(
'interestDepositForm',
false,
false,
'depositAmount'
)
this.props.interestActions.setCoinDisplay(isCoin)
}
handleRefresh = () => {
this.handleInitializeDepositForm()
}
handleInitializeDepositForm = () => {
const { coin, walletCurrency } = this.props.data.getOrElse({
coin: 'BTC' as CoinType,
walletCurrency: 'GBP' as FiatType
})
this.props.interestActions.initializeDepositForm(coin, walletCurrency)
}
handleSubmit = () => {
const { coin } = this.props.data.getOrElse({ coin: 'BTC' as CoinType })
this.props.interestActions.submitDepositForm(coin)
}
render () {
const { data } = this.props
return data.cata({
Success: val => (
<Success
{...val}
{...this.props}
onSubmit={this.handleSubmit}
handleDisplayToggle={this.handleDisplayToggle}
/>
),
Failure: () => <DataError onClick={this.handleRefresh} />,
Loading: () => <Loading />,
NotAsked: () => <Loading />
})
}
}
const mapStateToProps = (state): LinkStatePropsType => ({
data: getData(state)
})
const mapDispatchToProps = (dispatch: Dispatch): LinkDispatchPropsType => ({
analyticsActions: bindActionCreators(actions.analytics, dispatch),
formActions: bindActionCreators(actions.form, dispatch),
interestActions: bindActionCreators(actions.components.interest, dispatch)
})
const connector = connect(mapStateToProps, mapDispatchToProps)
export type LinkDispatchPropsType = {
analyticsActions: typeof actions.analytics
formActions: typeof actions.form
interestActions: typeof actions.components.interest
}
export type SuccessStateType = {
coin: CoinType
depositLimits: InterestMinMaxType
displayCoin: boolean
formErrors: { depositAmount?: 'ABOVE_MAX' | 'BELOW_MIN' | boolean }
interestLimits: InterestLimitsType
interestRate: InterestRateType
rates: RatesType
supportedCoins: SupportedCoinsType
walletCurrency: FiatType
}
type LinkStatePropsType = {
data: RemoteDataType<string | Error, SuccessStateType>
}
type Props = ConnectedProps<typeof connector>
export default connector(DepositForm)