-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
453 lines (405 loc) · 15.4 KB
/
main.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
function getFileFromURL(url, sheetName, onSuccess, onErr) {
loadingModal.show();
let id = getSheetID(url);
let params = new URLSearchParams(); // magical API to generate the query string for us
params.set("id", id);
params.set("sheetName", sheetName);
let loadTime = new Performance(`load ${sheetName}`);
const URL_BASE = `https://googlesheets-proxy.herokuapp.com`;
let getUrl = `${URL_BASE}/dl?${params.toString()}`; // loading through the proxy for CORS reasons
fetch(getUrl, {
method: "GET",
}).then(response => {
return response.json();
}).then(data => {
onSuccess(data)
loadTime.log();
}).catch((error) => {
console.error('Error:', error);
if (onErr) onErr(error);
});
}
function sortResources(res) {
res.sort(function(a, b) {
if (isUnverified(a) && !isUnverified(b)) {
return 1;
}
if (!isUnverified(a) && isUnverified(b)) {
return -1;
}
return 0;
});
return res;
}
function createElementWithClass(type, class_name, text, style) {
let element = document.createElement(type);
element.className = class_name;
if (style) element.style = style;
if (text) element.textContent = text;
return element;
}
function loadResourceData(resName, callback) {
loadingModal.show();
function onErr(e) {
loadingModal.hide();
showErrorDialog(e);
throw new Error(e);
}
if (App.data.resourceData[resName]) {
callback(App.data.resourceData[resName]);
return;
} else {
getFileFromURL(App.master, resName, onResLoadSuccess, onErr);
function onResLoadSuccess(data) {
let _data = data.text;
let parsed = Papa.parse(_data, PAPA_OPTIONS).data;
if (parsed) {
let final = sortResources(parsed);
App.data.resourceData[resName] = final;
// cacheTimeStampedData(resName, final, 9e5); // 15 minutes
callback(App.data.resourceData[resName]);
loadingModal.hide();
} else {
onErr("Invalid data received!");
}
}
}
}
function renderButtons(resources) {
let div = document.getElementById("resource-buttons");
div.innerHTML = '';
resources.forEach(resource => {
let button = createElementWithClass(
"button",
"btn resource-btn btn-primary",
resource
);
button.onclick = function() {
App.data.selectedResources = App.data.selectedResources || [];
this.selected = !this.selected;
if (this.selected) {
App.data.selectedResources.push(resource);
this.classList.add('bg-success', 'text-light');
} else {
App.data.selectedResources.remove(resource);
this.classList.remove('bg-success', 'text-light');
}
this.blur();
onUserInput();
}
div.appendChild(button);
});
}
function setElementStyleProp(elem, property, value) {
if (elem) {
elem.style[property] = value;
}
}
function toggleElementDisplay(selector) {
let elem = document.querySelector(selector);
if (elem) {
let d = elem.style.display;
(d === 'none') ? setElementStyleProp(elem, "display", "block"): setElementStyleProp(elem, "display", "none");
}
}
function createRow(k, v, icon, textClass) {
function getClass() {
if (textClass) return textClass;
return "fs-6 text-wrap d-inline";
}
function createTelLink(phNo) {
//Removing non-digits alone from the phone number
var actualPhNo = phNo.replace(/\D/g, "");
if (!actualPhNo || actualPhNo === '' || actualPhNo.length < 7 || actualPhNo.length > 15)
return false;
else
return `<a href="tel://${actualPhNo}">${phNo}</a>`;
}
if ("phone" == k) {
let tempv = v;
v = "";
let phoneNos = tempv.split(/[,/]+/);
for (const phoneNo of phoneNos) {
let temp = createTelLink(phoneNo.trim());
if (temp)
v = v + " " + temp;
else
v = tempv;
}
}
return {
k: k,
v: v,
icon: Boolean(icon),
str: `<div style='width: 100%; text-align: left;' class='m-1'>
<div class="${getClass()}" style='font-weight: 600'> ${icon ? icon : k} </div>
<div class="${getClass()}" style='font-weight: 400'> ${v} </div> </div>`
}
}
// Converts an object to a markdown-formatted string representation.
function stringifyObject(obj) {
let type = '';
let city = '';
let _details = [];
for (let key in obj) {
if (!Boolean(key) || !Boolean(obj[key])) continue;
if (!Boolean(key.trim()) || !Boolean(obj[key].trim())) continue;
if ((/.*(verified)|(timestamp)|(service provider state).*/i).test(key)) continue;
if ((/.*type of service.*/i).test(key)) {
type = obj[key];
} else if ((/.*city/i).test(key)) {
city = obj[key];
} else {
_details.push(`*${key}*: ${obj[key]}`);
}
}
let re = new RegExp(`\\*${type} `, 'gi');
let verified = isUnverified(obj) ? "" : " [VERIFIED]";
let details = _details.join('\n').replace(re, "*");
let final = `*${type.toLocaleUpperCase()}* in *${city}${verified}*\n\n${details}`
final += `\n\nGet more resources at ${document.location.href}${getCurrentQueryString()}`
return final;
}
function sendAlert(text) {
let existing = document.getElementById('copy-alert')
if (existing) {
existing.id = '';
document.body.removeChild(existing);
}
let alert = document.createElement('div');
alert.id = 'copy-alert'
alert.className = 'alert bg-warning rounded';
alert.textContent = text;
document.body.appendChild(alert);
alert.style.animation = 'fadein 2s';
}
function copyToClipboard(str) {
navigator.clipboard.writeText(str).then(function() {
sendAlert("Copied!");
}, function() {});
}
function renderCard(obj) {
let status = isUnverified(obj) ? "warning" : "success";
let vString = isUnverified(obj) ? `<i class="fas fa-exclamation-circle"></i> Unverified` : `<i class="fas fa-check"></i> Verified`;
let container = document.getElementById("information");
let final = [];
let normalised = {};
for (let key in obj) {
if ((/.*(verified)|(timestamp)|(service provider state).*/i).test(key)) continue;
if (!Boolean(key) || !Boolean(obj[key])) continue;
if (!Boolean(key.trim()) || !Boolean(obj[key].trim())) continue;
if ((/.*number.*/i).test(key) && (!obj[key] || obj[key] && obj[key].length < 7)) continue;
if ((/.*type of service.*/i).test(key)) {
final.unshift({
icon: true,
str: `<div class='mb-4'>
<div class='badge bg-primary w-auto'>${obj[key]}</div>
<div class='badge bg-${status} w-auto'> ${vString}</div>
</div>`
});
continue;
};
for (let category in normaliser) {
if (normaliser[category].re.test(key)) {
normaliser[category].value = obj[key];
normalised[key] = category;
}
}
if (!Object.keys(normalised).includes(key)) {
final.push(createRow(key, obj[key]));
} else {
final.push(createRow(normalised[key],
obj[key], normaliser[normalised[key]].icon,
normaliser[normalised[key]].class));
}
}
if (final.length == 0) {
return;
}
final.sort((b, a) => {
if (a.icon && !b.icon) return 1;
if (b.icon && !a.icon) return -1;
return 0;
})
final = final.map((itm) => itm.str);
let stringifiedObj = stringifyObject(obj);
final.push(createRow(
"Share",
`<a class='d-inline' target='blank' href='https://api.whatsapp.com/send?text=${encodeURIComponent(stringifiedObj)}'><i class="fa fa-whatsapp fs-1 mx-1" aria-hidden="true"></i></a>
<a class='d-inline' onclick='copyToClipboard(\`${stringifiedObj}\`)'>
<i class="fa fa-clone fs-1 mx-1" aria-hidden="true"></i>
</a>`,
null,
`align-middle d-inline`
).str);
let cardGen =
`<div class="card h-100 ml-2 mt-4 shadow">
<div class="card-body bg-gradient">
<div class="d-flex flex-column">
${final.join("\n")}
</div>
</div>
</div>
`;
let fragment = document.createElement('div');
fragment.className = "col-lg-6 col-12 px-0 py-1";
fragment.innerHTML = cardGen;
container.append(fragment); // this is a lot faster than innerHTML even though innerHTML is
// traditionally super fast because setting innerHTML forces the browser to re-render older
// cards that haven't changed, simply because the string itself changed
}
function populateStateDropdown() {
// Inserts State Options into the states dropdown at the start of the page
let statesDropdown = document.getElementById("states-dropdown");
statesDropdown.innerHTML = "<option>[Select a state]</option>"; // Initialize dropdown with a placeholder value
(states).forEach(element => {
// Creates an option tag for each state in the states array
let option = document.createElement("option");
option.innerText = element;
statesDropdown.appendChild(option);
})
}
function onStateDropdownChange() {
// Renders relevant buttons and cards when a state is selected from the states dropdown
let dropdownValue = document.getElementById("states-dropdown").value;
let waits = 0;
if (dropdownValue !== "[Select a state]") {
App.data.state = dropdownValue;
} else {
App.data.state = null;
}
onUserInput();
}
function renderStateResourceData(list, stateName, resName) {
// renders cards
loadingModal.show();
let perf = new Performance(`render ${resName} data for ${stateName}`);
let container = document.getElementById("information");
let title = document.querySelector("label[for='information']");
title.innerHTML = `${resName} in ${stateName}`;
let navLink = document.getElementById('nav-link');
App.currentUrl = `http://${document.location.hostname}/${getCurrentQueryString()}`;
navLink.innerHTML = `<a href='${App.currentUrl}'>Link to this section</a>
<a href='#' onclick="copyToClipboard('${App.currentUrl}')">(Copy to clipboard)</a>`;
setElementStyleProp(title, "display", "block");
container.innerHTML = "";
if (list.length == 0) {
let apology = document.createElement('div');
apology.className = `align-items-center justify-content-center`;
apology.innerHTML = `<i class="fa fa-frown-o" aria-hidden="true"></i> No resources could be found matching the selected parameters. Please try again later, we're constantly working to add and verify new leads.`;
container.appendChild(apology);
loadingModal.hide();
return;
}
list.forEach(item => {
renderCard(item)
})
perf.log();
loadingModal.hide();
}
function onUserInput() {
let submit = document.getElementById('submit-button');
submit.disabled = !((App.data.selectedResources && App.data.selectedResources.length > 0) && App.data.state);
return submit.disabled;
}
function submitButtonHandler() {
loadingModal.show();
let finalResources = []
let resources = App.data.selectedResources;
let length = 0;
if (resources) {
length = resources.length;
}
let loadedCount = 0;
let nextCalled = false;
function next() {
let res = finalResources.flat(1).filter((o) => (o["Service Provider State"] == App.data.state));
let resName = resources.join(", ");
renderStateResourceData(res, App.data.state, resName);
}
let callback = function(d) {
finalResources.push(d);
loadedCount += 1;
if (loadedCount === length && !nextCalled) {
nextCalled = true;
next();
}
}
for (let x of resources) {
attempts += 1;
loadResourceData(x, callback);
}
}
function getCurrentQueryString() {
let params = new URLSearchParams();
if (!App.data.state || !App.data.selectedResources) {
return '';
} else {
params.set('resources', App.data.selectedResources.join("|"));
params.set('state', App.data.state);
}
return `?${params.toString()}`;
}
function init() {
// Instantiate a reusable modal
Modal = new bootstrap.Modal(document.getElementById("reusable-modal"), {});
// Instantiate a loading modal
loadingModal = new bootstrap.Modal(document.getElementById("loading-modal"), {
backdrop: "static" // Note: setting data-bs-backdrop on the modal div doesn't work
});
loadingModal.show();
let submit = document.getElementById('submit-button');
submit.disabled = true;
submit.onclick = submitButtonHandler;
let resTitle = document.querySelector("label[for='information']");
setElementStyleProp(resTitle, "display", "none");
document.querySelector("#states-dropdown").onchange = onStateDropdownChange;
if (!String.prototype.replaceAll) { // polyfill replaceAll
String.prototype.replaceAll = function(arg1, arg2) {
let toRet = this;
while (toRet.includes(arg1)) {
toRet = toRet.replace(arg1, arg2);
}
return toRet;
}
}
function onGetMasterSuccess(data) {
if (data.status === "OK") {
let resourceList = [] // list of resources
console.log(data.text);
let resData = Papa.parse(data.text, PAPA_OPTIONS).data;
for (let item of resData) {
resourceList.push(item.Category);
}
App.masterLoaded = true;
populateStateDropdown();
renderButtons(resourceList);
if (document.location.search) {
loadingModal.show();
let params = new URLSearchParams(document.location.search);
let resources = params.get('resources');
let state = params.get('state');
if (resources && state) {
resources = resources.split('|');
let valid = resources.filter(x => resourceList.indexOf(x) !== -1);
for (let x of document.querySelectorAll('.resource-btn')) {
if (valid.includes(x.innerHTML.trim())) {
x.click();
}
}
}
document.getElementById('states-dropdown').selectedIndex = states.indexOf(state) + 1;
onStateDropdownChange();
let submit = document.getElementById('submit-button');
submit.disabled = false;
submitButtonHandler();
}
if (!document.location.search) loadingModal.hide();
} else {
showErrorDialog(`Loading master sheet failed failed with error ${data.status}`);
throw new Error(`Loading master sheet failed failed with error ${data.status}`);
}
}
getFileFromURL(App.master, "Index", onGetMasterSuccess); // get file, since we don't have a cached version of the file.
}
window.addEventListener('DOMContentLoaded', () => init());