-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathBidAssetForm.js
200 lines (177 loc) · 5.23 KB
/
BidAssetForm.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React from 'react'
import PropTypes from 'prop-types'
import addDays from 'date-fns/add_days'
import dateFnsFormat from 'date-fns/format'
import differenceInDays from 'date-fns/difference_in_days'
import { Form, Button, Input, Message, Icon } from 'semantic-ui-react'
import { t } from '@dapps/modules/translation/utils'
import { assetType, bidType } from 'components/types'
import {
preventDefault,
formatDate,
formatMana,
sanitizePrice
} from 'lib/utils'
import { isParcel } from 'shared/parcel'
import {
DEFAULT_DAY_INTERVAL,
MINIMUM_DAY_INTERVAL,
MAXIMUM_BID_DAY_INTERVAL,
MINIMUM_ASSET_PRICE
} from 'shared/listing'
import TxStatus from 'components/TxStatus'
import './BidAssetForm.css'
const INPUT_FORMAT = 'YYYY-MM-DD'
export default class BidAssetForm extends React.PureComponent {
static propTypes = {
asset: assetType,
assetName: PropTypes.string,
bid: bidType,
isTxIdle: PropTypes.bool,
isDisabled: PropTypes.bool,
onBid: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired,
balance: PropTypes.number.isRequired
}
constructor(props) {
super(props)
const { bid } = props
this.state = {
price: bid ? bid.price : '',
expiresAt: bid
? dateFnsFormat(parseInt(bid.expires_at, 10), INPUT_FORMAT)
: this.formatFutureDate(DEFAULT_DAY_INTERVAL),
formErrors: []
}
}
handlePriceChange = e => {
// Dots and commas are not allowed
const price = sanitizePrice(e.currentTarget.value)
this.setState({
formErrors: [],
price
})
}
handleExpiresAtChange = e => {
this.setState({
expiresAt: e.currentTarget.value,
formErrors: []
})
}
handleClearFormErrors = () => {
this.setState({ formErrors: [] })
}
handlePublish = () => {
const { asset, onBid } = this.props
const { price, expiresAt } = this.state
const formErrors = []
const today = new Date()
today.setHours(0, 0, 0, 0)
if (differenceInDays(expiresAt, today) < MINIMUM_DAY_INTERVAL) {
formErrors.push(
t('asset_bid.errors.minimum_expiration', {
date: this.formatFutureDate(MINIMUM_DAY_INTERVAL + 1)
})
)
}
if (differenceInDays(expiresAt, today) > MAXIMUM_BID_DAY_INTERVAL) {
formErrors.push(
t('asset_bid.errors.maximum_expiration', {
date: this.formatFutureDate(MAXIMUM_BID_DAY_INTERVAL)
})
)
}
if (price < MINIMUM_ASSET_PRICE) {
formErrors.push(
t('asset_bid.errors.minimum_land_price', {
value: formatMana(MINIMUM_ASSET_PRICE, ''),
asset_name: isParcel(asset) ? t('name.parcel') : t('name.estate')
})
)
}
if (price > Number.MAX_SAFE_INTEGER) {
formErrors.push(
t('asset_bid.errors.maximum_land_price', {
value: formatMana(Number.MAX_SAFE_INTEGER, ''),
asset_name: isParcel(asset) ? t('name.parcel') : t('name.estate')
})
)
}
if (formErrors.length === 0) {
onBid({
asset_id: asset.id,
expires_at: this.toUTCTimestamp(expiresAt),
price: parseInt(price)
})
} else {
this.setState({ formErrors })
}
}
toUTCTimestamp(expiresAt) {
const date = new Date(expiresAt)
return date.getTime() + date.getTimezoneOffset() * 60000
}
formatFutureDate(addedDays, date = new Date()) {
date = addDays(date, addedDays)
return formatDate(date, INPUT_FORMAT)
}
render() {
const { isTxIdle, assetName, isDisabled, onCancel, balance } = this.props
const { price, expiresAt, formErrors } = this.state
const canBid = price <= balance
return (
<Form
className="BidForm"
onSubmit={preventDefault(this.handlePublish)}
error={!!formErrors}
>
<Form.Field>
<label>{t('global.price')}</label>
<Input
type="number"
placeholder={t('asset_bid.price_placeholder', {
asset_name: assetName
})}
value={price}
required={true}
onChange={this.handlePriceChange}
/>
{price > balance ? (
<label className="warning">
<Icon name="warning sign" />
{t('asset_bid.not_enough')}
</label>
) : null}
</Form.Field>
<Form.Field>
<label>{t('asset_bid.expiration')}</label>
<Input
type="date"
placeholder={t('asset_bid.expiration_placeholder')}
value={expiresAt}
required={true}
onChange={this.handleExpiresAtChange}
/>
</Form.Field>
<TxStatus.Idle isIdle={isTxIdle} />
{formErrors.length > 0 ? (
<Message error onDismiss={this.handleClearFormErrors}>
{formErrors.map((error, index) => <div key={index}>{error}</div>)}
</Message>
) : null}
<div className="modal-buttons">
<Button onClick={onCancel} type="button">
{t('global.cancel')}
</Button>
<Button
type="submit"
primary={true}
disabled={isTxIdle || isDisabled || price <= 0 || !canBid}
>
{t('global.submit')}
</Button>
</div>
</Form>
)
}
}