-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (154 loc) · 3.84 KB
/
index.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
// Places from FourSquare API
var places = [
'The Nightjar',
'Ozone Coffee Roasters',
'MEAT Mission',
'Shoreditch Grind',
'The Princess of Shoreditch',
'Casita',
'Cay Tre',
'On The Bab',
'Kêu Banh Mi Deli',
'Happiness Forgets',
'Ruby',
'Bel-Air Food',
'The Breakfast Club',
'Hoxton Grill',
'Busaba Eathai',
'The Book Club',
'Origin Coffee',
'The Old Fountain',
'Taylor St Baristas',
'Lantana Shoreditch',
'Ceviche',
'Merchants Tavern',
'Friends of Ours',
'The Attendant',
'Callooh Callay',
'NOLA',
'The Clove Club',
'Pitfield London',
'Slate Coffee',
'Fix 126',
]
// Util
var random = function(num) {
return Math.floor(Math.random()*num)
}
var randomString = function(length) {
var rand = function() { return Math.random().toString(36)+'00000000000000000' }
return Array(length+1).join(rand().slice(2, 18)).slice(0, length)
}
var addOption = function(select, value, text) {
var el = document.createElement("option")
el.textContent = text || value
el.value = value
select.appendChild(el)
}
var getQueryParam = function(param) {
var str = location.search.match(new RegExp(param + '=(.*?)($|\&)', 'i'))
return str ? str[1] : null
}
// Setup
addOption(locationSelect, 'random', 'Location (random)')
for (var i = 0; i < places.length; i++) {
addOption(locationSelect, places[i])
}
// Prefill fields based on query params
var paramMap = {
'endpoint': 'urlField',
'account_id': 'accountField',
'amount': 'amountField',
'transaction_id': 'transactionField',
}
Object.keys(paramMap).forEach(function(param) {
var urlPrefill = getQueryParam(param)
if (urlPrefill) {
document.getElementById(paramMap[param]).value = urlPrefill
}
})
// For transactions
var getAmount = function() {
var value = amountField.value
return value === '' ? -random(25) : value
}
var getISODate = function() {
return (new Date).toISOString()
}
var getRandomPlace = function() {
return places[random(places.length)]
}
var getPlace = function() {
var value = locationSelect.value
return value === 'random' ? getRandomPlace() : value
}
var getId = function(prefix) {
return prefix + '_' + randomString(22)
}
var getAccountId = function() {
var value = accountField.value
return value === '' ? getId('acc') : value
}
var getTransactionId = function() {
var value = transactionField.value
return value === '' ? getId('tx') : value
}
var generateTransaction = function() {
return {
type: 'transaction.created',
data: {
account_id: getAccountId(),
amount: getAmount(),
created: getISODate(),
currency: 'GBP',
description: getPlace(),
id: getTransactionId(),
}
}
}
var getUrl = function() {
var url = urlField.value
if (url !== '' && !/^(f|ht)tps?:\/\//i.test(url)) {
url = "http://" + url;
}
return url
}
var postRequest = function(url, data, callback) {
var request = new XMLHttpRequest()
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.status === 200) {
callback(null, JSON.parse(request.response))
} else {
callback('error', null)
}
}
}
request.open('POST', url, true)
request.setRequestHeader('Content-Type', 'application/json')
request.send(JSON.stringify(data))
}
var submit = function() {
var data = generateTransaction()
var url = getUrl()
var cb = function(err, response) {
console.log(response)
}
postRequest(url, data, cb)
}
sendButton.addEventListener('click', submit)
var generatePreview = function() {
var data = generateTransaction()
preview.innerHTML = JSON.stringify(data, null, '\t')
}
var previewShown = false
var togglePreview = function(evt) {
evt.preventDefault()
if (!previewShown) generatePreview()
previewShown = !previewShown
form.classList.toggle('hidden')
closePreview.classList.toggle('hidden')
preview.classList.toggle('hidden')
}
showPreview.addEventListener('click', togglePreview)
closePreview.addEventListener('click', togglePreview)