-
Notifications
You must be signed in to change notification settings - Fork 0
/
rew-calculator.js
300 lines (257 loc) · 10.2 KB
/
rew-calculator.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const DEFAULT_SUBMIT_URL = 'https://presale.review.network'
const API_URL = 'https://calculator.review.network'
function handleErrors(response) {
if (!response.ok) {
throw response
}
return response
}
function throttle(callback, wait, context = this) {
let timeout = null
let callbackArgs = null
const later = () => {
callback.apply(context, callbackArgs)
timeout = null
}
return function() {
if (!timeout) {
callbackArgs = arguments
timeout = setTimeout(later, wait)
}
}
}
;(() => {
class RewCalculator {
constructor (elementOrSelector, { onInit = () => {} }) {
if (!elementOrSelector) {
throw Error('Element or selector not passed')
}
const container = typeof elementOrSelector === 'string'
? document.querySelector(elementOrSelector)
: elementOrSelector
this.el = this.createCalculator()
container.append(this.el)
this.setupUI()
this.attachEvents()
this
.loadCurrencies()
.then(() => this.loadCurrentStage())
.then(() => {
this.ui.$inputContainer.classList.add('rc-input--visible')
onInit()
})
}
loadCurrencies () {
return fetch(`${API_URL}/currencies`)
.then(handleErrors)
.then(r => r.json())
.then(r => r.content)
.then(currencies => {
this.ui.$currency.innerHTML = currencies
.map(currency => `<option value="${currency}">${currency}</option>`)
})
}
loadCurrentStage () {
return fetch(`${API_URL}/stage`)
.then(handleErrors)
.then(r => r.json())
.then(r => r.content)
.then(stage => {
this.minInvestments = stage.min_investments
let selectedCurrency = this.ui.$currency.value
this.ui.$currentStageName.innerHTML = stage.name
this.ui.$currentStageDates.innerHTML = new Date(stage.date_from).toLocaleDateString() + ' - ' + new Date(stage.date_to).toLocaleDateString()
this.ui.$currentStageMin.innerHTML = `Min. ${this.minInvestments[selectedCurrency]} ${selectedCurrency}`
this.ui.$currentStage.classList.add('rc-current-stage--visible')
})
}
createCalculator () {
let el = document.createElement('div')
el.innerHTML = this.renderTemplate()
return el
}
renderLoader () {
return `
<div class="rc-loader"></div>
`
}
renderAmountInput () {
return `
<input class="rc-amount" type="text" placeholder="Enter Amount..." />
`
}
renderCurrencySelect () {
return `
<div class="rc-currency-container">
<select class="rc-currency">
<option>...</option>
</select>
</div>
`
}
renderTokenAmount () {
return `
<div class="rc-calculation">
<div class="rc-calculation__item">
<div class="rc-calculation__item-title">Total REW (incl. bonus tokens)</div>
<div class="rc-calculation__item-value">
<span class="rc-total-rew"></span>
REW
</div>
</div>
<div class="rc-calculation__item">
<div class="rc-calculation__item-title">BONUS Tier</div>
<div class="rc-calculation__item-value">
<span class="rc-bonus"></span>%
</div>
</div>
</div>
`
}
renderErrorPlaceholder () {
return `
<div class="rc-error"></div>
`
}
renderSubmit () {
return `
<div>
<button class="rc-submit">Invest Now</button>
</div>
`
}
renderCurrentStage () {
return `
<div class="rc-current-stage">
<div class="rc-current-stage__name"></div>
<div class="rc-current-stage__min"></div>
<div class="rc-current-stage__dates"></div>
</div>
`
}
renderTemplate () {
return `
<div class="rc">
<div class="rc-title">
<a href="https://review.network" target="_blank" class="rc-title__logo"></a>
REW Calculator
</div>
${this.renderCurrentStage()}
<div class="rc-input">
${this.renderAmountInput()}
<div class="rc-loader-container">
${this.renderLoader()}
</div>
${this.renderCurrencySelect()}
</div>
${this.renderTokenAmount()}
${this.renderErrorPlaceholder()}
${this.renderSubmit()}
</div>
`
}
setupUI () {
this.ui = {
$loader: document.querySelector('.rc-loader'),
$error: document.querySelector('.rc-error'),
$inputContainer: document.querySelector('.rc-input'),
$amount: document.querySelector('.rc-amount'),
$currency: document.querySelector('.rc-currency'),
$totalRew: document.querySelector('.rc-total-rew'),
$bonus: document.querySelector('.rc-bonus'),
$calculation: document.querySelector('.rc-calculation'),
$submit: document.querySelector('.rc-submit'),
$currentStage: document.querySelector('.rc-current-stage'),
$currentStageName: document.querySelector('.rc-current-stage__name'),
$currentStageDates: document.querySelector('.rc-current-stage__dates'),
$currentStageMin: document.querySelector('.rc-current-stage__min'),
}
}
attachEvents () {
let calculateTokensThrottled = throttle(e => this.calculateTokens(e), 300)
this.ui.$currency.addEventListener('change', e => this.calculateTokens(e))
this.ui.$amount.addEventListener('input', e => calculateTokensThrottled(e))
this.ui.$submit.addEventListener('click', e => this.onSubmitClicked(e))
}
showError (message) {
this.ui.$error.innerHTML = message
this.ui.$error.classList.add('rc-error--visible')
}
hideError () {
this.ui.$error.innerHTML = ''
this.ui.$error.classList.remove('rc-error--visible')
}
startLoader () {
this.ui.$loader.classList.add('rc-loader--visible')
}
stopLoader () {
this.ui.$loader.classList.remove('rc-loader--visible')
}
getConversion (amount, currency) {
return fetch(`${API_URL}/convert?currency=${currency}&amount=${amount}`)
.then(handleErrors)
.then(r => r.json())
}
calculateTokens () {
let amount = this.ui.$amount.value
let currency = this.ui.$currency.value
this.ui.$currentStageMin.innerHTML = `Min. ${this.minInvestments[currency]} ${currency}`
this.ui.$submit.classList.remove('rc-submit--visible')
this.ui.$calculation.classList.remove('rc-calculation--visible')
if (!amount) {
this.hideError()
return
}
if (!parseFloat(amount, 10)) {
this.showError('The amount must be a number.')
return
} else {
this.hideError()
}
if (amount < this.minInvestments[currency]) {
this.showError('The amount must be greater than the minimum investment.')
return
} else {
this.hideError()
}
this.startLoader()
this
.getConversion(amount, currency)
.then(data => {
let { applied_bonus: bonus, total_rew: totalRew } = data.content
let percentage = bonus && bonus.percentage
this.ui.$totalRew.innerHTML = totalRew.toLocaleString(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 0
})
this.ui.$bonus.innerHTML = percentage || '0'
this.ui.$calculation.classList.add('rc-calculation--visible')
this.ui.$submit.classList.add('rc-submit--visible')
this.stopLoader()
})
.catch(err => {
this.ui.$submit.classList.remove('rc-submit--visible')
err.json().then(errorMessage => {
let errors = Object.values(errorMessage).map(errors => errors[0])
this.showError(errors.join(', '))
})
this.stopLoader()
})
}
onSubmitClicked () {
let amount = this.ui.$amount.value
if(!parseFloat(amount, 10)) {
this.showError('Invalid amount')
return
} else {
this.hideError()
}
window.location = `${DEFAULT_SUBMIT_URL}?amount=${this.ui.$amount.value}¤cy=${this.ui.$currency.value}`
}
setInvestment (amount, currency) {
this.ui.$amount.value = amount
this.ui.$currency.value = currency
}
}
window.RewCalculator = RewCalculator
})(window, document)