-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathAcceptBidAssetPage.js
135 lines (115 loc) · 3.25 KB
/
AcceptBidAssetPage.js
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
import React from 'react'
import PropTypes from 'prop-types'
import { Loader, Container, Message } from 'semantic-ui-react'
import { t } from '@dapps/modules/translation/utils'
import { isOpen } from 'shared/listing'
import { ASSET_TYPES } from 'shared/asset'
import { fetchMANABalance } from 'modules/wallet/utils'
import AcceptBidParcelPage from './AcceptBidParcelPage'
import AcceptBidEstatePage from './AcceptBidEstatePage'
import { bidType } from 'components/types'
export default class AcceptBidAssetPage extends React.PureComponent {
static propTypes = {
id: PropTypes.string.isRequired,
bid: bidType,
onFetchBidById: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
assetType: PropTypes.string.isRequired,
isConnected: PropTypes.bool.isRequired,
isBidLoading: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired
}
bidderBalanceFetched = false
constructor(props) {
super(props)
this.state = { bidderHasBalance: true }
}
componentWillMount() {
const { bid, onFetchBidById } = this.props
if (!bid) {
onFetchBidById()
} else {
this.fetchBidderBalance(bid)
}
}
componentWillUpdate(nextProps) {
const { bid, isConnected, isLoading, isBidLoading } = nextProps
if (isConnected && !bid && !isLoading && !isBidLoading) {
return this.handleOnAccessDenied()
}
if (bid && isConnected && !this.bidderBalanceFetched) {
this.fetchBidderBalance(bid)
}
}
async fetchBidderBalance(bid) {
const { bidder, price } = bid
const balance = await fetchMANABalance(bidder)
this.bidderBalanceFetched = true
this.setState({
bidderHasBalance: parseInt(balance, 10) >= parseInt(price, 10)
})
}
handleConfirm = () => {
const { bid, onConfirm } = this.props
onConfirm(bid)
}
handleOnAccessDenied = () => {
this.props.onCancel()
}
renderAcceptBidComponent() {
const { bidderHasBalance } = this.state
const { assetType, bid } = this.props
const isBidOpen = isOpen(bid)
switch (assetType) {
case ASSET_TYPES.parcel:
return (
<AcceptBidParcelPage
{...this.props}
isOpen={isBidOpen}
handleConfirm={this.handleConfirm}
bidderHasBalance={bidderHasBalance}
/>
)
case ASSET_TYPES.estate:
return (
<AcceptBidEstatePage
{...this.props}
isOpen={isBidOpen}
handleConfirm={this.handleConfirm}
bidderHasBalance={bidderHasBalance}
/>
)
default:
return null
}
}
render() {
const { bidderHasBalance } = this.state
const { bid, isLoading, isBidLoading } = this.props
if (isLoading || isBidLoading) {
return (
<div>
<Loader active size="massive" />
</div>
)
}
if (!bid) {
return null
}
return (
<div>
{!bidderHasBalance && (
<Container text>
<Message
warning
icon="warning sign"
header={t('global.warning')}
content={t('asset_bid.insufficient_funds')}
/>
</Container>
)}
{this.renderAcceptBidComponent()}
</div>
)
}
}