-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
88 lines (87 loc) · 2.38 KB
/
app.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
new Vue({
el: "#app",
data: {
started: false,
paymentHistory: [],
errorHistory: [],
baseURL: "http://localhost:8080"
},
computed: {
startDisabled: function() {
return this.started || this.baseURL === "";
},
hasPaymentHistory: function() {
return this.paymentHistory.length > 0;
},
hasErrors: function() {
return this.errorHistory.length > 0;
}
},
methods: {
start: function() {
this.started = true;
this.scheduleUpdate();
},
stop: function() {
this.started = false;
},
clear: function() {
this.paymentHistory = [];
this.errorHistory = [];
},
scheduleUpdate: function() {
setTimeout(() => this.getPayment(), 1000);
},
addError: function(request, err) {
this.errorHistory.unshift({
request: request,
message: err.message,
timestamp: new Date().toLocaleString(),
});
this.errorHistory = this.errorHistory.slice(0, 10);
},
addPayment: function(data) {
this.paymentHistory.unshift({
amount: data.amount,
rate: data.rate,
years: data.years,
payment: data.payment,
timestamp: new Date().toLocaleString(),
instance: data.instance,
count: data.count
});
this.paymentHistory = this.paymentHistory.slice(0, 5);
},
getPayment: function() {
let rate = Math.floor(Math.random() * 7); // random integer 0..6
let amount = 100000.0 + Math.random() * 700000.00; // random between 100,000.00 and 700,000.00
amount = Math.floor(amount * 100.0) / 100.0; // force 2 decimal digits
let years = 30;
fetch(`${this.baseURL}/payment?amount=${amount}&rate=${rate}&years=${years}`, {method: 'GET'})
.then((res) => res.json())
.then((data) => {
this.addPayment(data);
})
.catch((err) => {
this.addError(`Details: a=${amount}, r=${rate}, y=${years}`, err);
})
.then(() => {
if (this.started) {
this.scheduleUpdate();
}
});
},
crashIt: function() {
fetch(`${this.baseURL}/crash`, {method: 'GET'})
.catch((err) => {
this.addError('Crash Request', err);
});
},
resetCount() {
fetch(`${this.baseURL}/resetCount`, {method: 'GET'})
.catch((err) => {
this.addError('Reset Count Request', err);
});
}
}
});