-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
131 lines (110 loc) · 3.78 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
var selectedMonth = "";
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function dropDown() {
document.getElementById("dropdown").classList.toggle("show");
}
//Remove spaces from postcode
function removeSpaces(postcode) {
return postcode.replace(/\s/g, "");
}
//Function which filters categories by number of crimes
function categoriesIterator(policeObj) {
let uniquCats = new Set([]);
policeObj.forEach(element => {
uniquCats.add(element.category);
});
uniquCats = Array.from(uniquCats);
let numByCat = [];
for (let i = 0; i < uniquCats.length; i++) {
let count = 0;
for (let j = 0; j < policeObj.length; j++) {
if (uniquCats[i] === policeObj[j].category) {
count++;
}
}
numByCat.push(count);
count = 0;
}
let objByCat = {};
for (let i = 0; i < uniquCats.length; i++) {
objByCat[uniquCats[i]] = numByCat[i];
}
return objByCat;
}
let results = document.querySelector(".result");
let search = document.querySelector("#searchbutton");
search.addEventListener("click", query);
function query() {
let e = document.querySelector("ul");
e.innerHTML = "";
let postcode = document.querySelector("#searchfield").value;
//API call to validate postcode
let valid = new XMLHttpRequest();
let urlValid = `https://api.postcodes.io/postcodes/${postcode}/validate`;
valid.onreadystatechange = function() {
if (valid.readyState == 4 && valid.status == 200) {
var response = JSON.parse(valid.responseText);
if (response.result) {
location(postcode);
} else {
// Delay alert after old data has been cleared
function first() {
setTimeout(function() {
alert("Please, enter a valid postcode, e.g. SW1A 1AA");
}, 500);
}
function second() {
let numCrimes = document.querySelector(".numberOfCrimes");
numCrimes.textContent = "Number of crimes:";
}
first();
second();
}
}
};
valid.open("GET", urlValid, true);
valid.send();
function location(postcode) {
postcode = removeSpaces(postcode);
let xhr = new XMLHttpRequest();
let urlLocation = `https://api.postcodes.io/postcodes/${postcode}`;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var lat = response.result.latitude;
var long = response.result.longitude;
let month = document.querySelector("#month").value;
let year = document.querySelector("#year").value;
policeAPI(lat, long, month, year);
}
};
xhr.open("GET", urlLocation, true);
xhr.send();
}
}
// / Police API
let policeAPI = function(la, lo, month, year) {
var xhr = new XMLHttpRequest();
var URL = `https://data.police.uk/api/crimes-at-location?date=${year}-${month}&lat=${la}&lng=${lo}`;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var policeObj = JSON.parse(xhr.responseText);
let totalCrimes = policeObj.length;
let crimeNum = document.querySelector(".numberOfCrimes");
crimeNum.textContent = `Number of crimes: ${totalCrimes}`;
let categories = Object.keys(categoriesIterator(policeObj));
let numbers = Object.values(categoriesIterator(policeObj));
for (let i = 0; i < categories.length; i++) {
let newLine = document.createElement("li");
let parentCrimes = document.querySelector(".categoriesOfCrimes");
parentCrimes.appendChild(newLine);
newLine.setAttribute("class", "crimes");
newLine.textContent = `${categories[i]}: ${numbers[i]}`;
}
//POPULATE WITH CATEGORIES WITH COUNT OF CRIMES
}
};
xhr.open("GET", URL, true);
xhr.send();
};