-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (118 loc) · 4.92 KB
/
script.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
// gets games based on title from server, then adds it to HTML
function getGames() {
event.preventDefault(); // Prevent page reload
var title = "title=" + document.getElementById("submission").value;
var url = "https://www.cheapshark.com/api/1.0/games?" + title;
var xhttp2 = new XMLHttpRequest();
xhttp2.onreadystatechange = function() {
// Wait for readyState = 4 & 200 response
if (this.readyState == 4 && this.status == 200) {
var games = JSON.parse(this.responseText); // gets array of games matching search
console.log(games); //TEST DELETE WHEN DONE
addGames(games);
document.getElementById("submission").value = "";
}
else if (this.readyState == 4) {
console.log(this.responseText);
}
};
xhttp2.open("GET", url, true);
xhttp2.send();
}
const gameForm = document.getElementById("game-form");
gameForm.addEventListener("submit", getGames, false); //triggers submission & making of ToDo via event listener
function addGames(games) {
event.preventDefault(); // Prevent page reload
document.getElementsByClassName("games", "empty")[0].remove(); //removes previous
var results = document.getElementById("game-result"); // gets the containing form
var container = document.createElement("div");
container.classList.add("container", "games");
if (games.length == 0) {
var div = document.createElement("div");
div.classList.add("empty");
div.innerHTML = "Sorry, but it looks like there's no games matching this search. Try some other search terms."
container.append(div);
}
else {
for (var i = 0; i < games.length; i++) {
var div = document.createElement("div");
// var id = "div" + i;
// div.id = id;
div.innerHTML = "Game: " + games[i].external;
container.append(div);
}
}
results.append(container);
}
var currentPage;
function showDeals() {
currentPage = 0;
getDeals(currentPage);
}
const dealForm = document.getElementById("deal-form");
dealForm.addEventListener("submit", showDeals, false); //triggers submission & making of ToDo via event listener
function prevDeal() {
event.preventDefault(); // Prevent page reload
alert("Previous"); //TEST DELETE WHEN DONE
currentPage -= 1;
getDeals(currentPage);
}
function nextDeal() {
event.preventDefault(); // Prevent page reload
alert("Next"); //TEST DELETE WHEN DONE
currentPage += 1;
getDeals(currentPage);
}
document.getElementById("prev-deal").addEventListener("click", prevDeal, false);
document.getElementById("next-deal").addEventListener("click", nextDeal, false);
function getDeals(pageNo) {
event.preventDefault(); // Prevent page reload
document.getElementsByClassName("page-label")[0].remove(); //removes previous //TEST DELETE WHEN DONE
document.getElementsByClassName("deals","empty")[0].remove(); //removes previous
var title = "title=" + document.getElementById("title").value;
var url = "https://www.cheapshark.com/api/1.0/deals?" + title;
url = url + "&sortBy=Store"; //TEST DELETE WHEN DONE
var pageUrl = url + "&pageNumber=" + pageNo;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
// Wait for readyState = 4 & 200 response
if (this.readyState == 4 && this.status == 200) {
var deals = JSON.parse(this.responseText); // gets array of games matching search
console.log(deals);
addDeals(deals);
}
else if (this.readyState == 4) {
console.log(this.responseText);
}
};
xhttp.open("GET", pageUrl, true);
xhttp.send();
var results = document.getElementById("deal-result"); // gets the containing form
var page = document.createElement("p");
page.innerHTML = "Page number " + pageNo;
page.classList.add("page-label");
results.append(page);
}
function addDeals(deals) {
event.preventDefault(); // Prevent page reload
var results = document.getElementById("deal-result"); // gets the containing form
var container = document.createElement("div");
container.classList.add("container", "deals");
if (deals.length == 0) {
var div = document.createElement("div");
div.classList.add("empty");
div.innerHTML = "Sorry, but it looks like there's no deals matching this search. Try some other search terms."
container.append(div);
}
else {
for (var i = 0; i < deals.length; i++) {
var div = document.createElement("div");
// var id = "div" + i;
// div.id = id;
div.innerHTML = "Deal: " + deals[i].salePrice + " for " + deals[i].title;
div.innerHTML = div.innerHTML + " with store ID of " + deals[i].storeID//TEST DELETE WHEN DONE
container.append(div);
}
}
results.append(container);
}