forked from cs4241-22a/a2-shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
107 lines (93 loc) · 2.85 KB
/
scripts.js
File metadata and controls
107 lines (93 loc) · 2.85 KB
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
// Add some Javascript code here, to run on the front end.
let numResponse = 1;
const submit = function (e) {
e.preventDefault();
const name = document.getElementById("name").value;
const gameName = document.getElementById("gameName").value;
const score = document.getElementById("score").value;
const comment = document.getElementById("comment").value;
if (name.trim() === "" || gameName.trim() === "" || score.trim() === ""|| comment.trim() === "") {
alert("Please answer all questions.");
return false;
} else {
numResponse++;
const jsonData = {
responseNum: numResponse,
name: name,
gameName: gameName,
score: score,
comment: comment,
};
let body = JSON.stringify(jsonData);
fetch("/submit", {
method: "POST",
body,
}).then(function (response) {
update();
clear();
});
return true;
}
};
function update() {
let table = document.getElementById("responses");
table.innerHTML =
"<tr><th>Response #</th><th>Name</th><th>Game Name</th><th>Score</th><th>Comment</th><th>Recommendation</th><th>Delete Response</th></tr>";
fetch("/getResponses", {
method: "GET",
})
.then((response) => response.json())
.then(function (json) {
let index = 0;
for (let response of json) {
response.responseNum = index;
let row = table.insertRow(-1);
console.log(index);
let responseNum = row.insertCell(0);
let name = row.insertCell(1);
let gameName = row.insertCell(2);
let score = row.insertCell(3);
let comment = row.insertCell(4);
let degree = row.insertCell(5);
let modify = row.insertCell(6);
response.responseNum = index + 1;
row.cells[0].innerHTML = response.responseNum;
row.cells[1].innerHTML = response.name;
row.cells[2].innerHTML = response.gameName;
row.cells[3].innerHTML = response.score;
row.cells[4].innerHTML = response.comment;
row.cells[5].innerHTML = response.degree;
row.cells[6].innerHTML =
`<button class='deleteButton' onclick=deleteRow(${index})>Delete</button>`;
index++;
}
});
}
function clear() {
document.getElementById("name").value = "";
document.getElementById("gameName").value = "";
document.getElementById("score").value = "";
document.getElementById("comment").value = "";
}
function deleteRow(rowIndex) {
let confirmDelete = confirm(
"Are you sure you want to delete this response?"
);
if (confirmDelete) {
const json = {
deletingResponse: rowIndex,
};
let body = JSON.stringify(json);
fetch("/delete", {
method: "POST",
body,
}).then(function () {
update();
});
}
}
window.onload = function () {
const button = document.querySelector("button");
button.onclick = submit;
update();
};