2020-07-04 06:22:28 +00:00
|
|
|
var requestsDiv = document.getElementById("requests");
|
|
|
|
|
|
|
|
function getRequests(count,allRequests) {
|
|
|
|
var reqUrl;
|
|
|
|
if (allRequests) {
|
|
|
|
reqUrl = "/api/getAllRequests";
|
|
|
|
} else {
|
|
|
|
reqUrl = "/api/getRequests";
|
|
|
|
}
|
|
|
|
reqUrl += `?count=${count}`;
|
|
|
|
fetch(reqUrl)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(requests => buildTable(requests,allRequests));
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildTable(requests,allRequests) {
|
|
|
|
var requestsDivHTML = '<table><tr><th class="request-url">URL</th><th class="request-requester">Requester</th><th class="request-score">Score</th>';
|
|
|
|
if (allRequests) requestsDivHTML += `<th class="request-state">State</td></tr>`;
|
|
|
|
requestsDivHTML += "</tr>";
|
|
|
|
for (request of requests) {
|
|
|
|
requestsDivHTML += `<tr><td class="request-url"><a href="${request.url}">${request.url}</a></td>\
|
|
|
|
<td class="request-requester">${request.requester}</td>\
|
|
|
|
<td class="request-score">${request.score}</td>`;
|
|
|
|
if (allRequests) requestsDivHTML += `<td class="request-state">${request.state}</td></tr>`;
|
|
|
|
requestsDivHTML += "</tr>";
|
|
|
|
}
|
|
|
|
requestsDivHTML += "</table>";
|
|
|
|
requestsDiv.innerHTML = requestsDivHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTable() {
|
|
|
|
var count = document.getElementById("count").value;
|
|
|
|
var allRequests = document.getElementById("allRequests").checked;
|
|
|
|
getRequests(count,allRequests);
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
function addRequestErr(msg) {
|
|
|
|
document.getElementById('addRequestError').style.display = "inline-block";
|
|
|
|
document.getElementById('addRequestError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRequestErrReset() {
|
|
|
|
document.getElementById('addRequestError').style.display = "none";
|
|
|
|
document.getElementById('addRequestError').innerText = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMessage(msg) {
|
|
|
|
document.getElementById("messageModalText").innerText = msg;
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("messageModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeMessageModal() {
|
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:20:22 +00:00
|
|
|
function openAddRequestModal() {
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("addRequestModal").style.display = "block";
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function closeAddRequestModal() {
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
const validUrlRegexes = [
|
|
|
|
/^https:\/\/www\.youtube\.com\/watch\?v=[a-zA-Z0-9_-]{11}$/
|
|
|
|
];
|
|
|
|
function validateAndSubmitRequest() {
|
|
|
|
addRequestErrReset();
|
|
|
|
var url = document.getElementById("addRequestUrl").value;
|
|
|
|
var validUrl = false;
|
|
|
|
for (var regex of validUrlRegexes) {
|
|
|
|
if (regex.test(url)) {
|
|
|
|
validUrl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validUrl) {
|
|
|
|
addRequestErr("Invalid URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetch("/api/addRequest", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(addRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
closeAddRequestModal();
|
|
|
|
updateTable();
|
|
|
|
document.getElementById("addRequestUrl").value = "";
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 06:22:28 +00:00
|
|
|
updateTable();
|
2020-07-06 03:41:42 +00:00
|
|
|
|
|
|
|
document.addEventListener("keydown", function onEvent(event) {
|
|
|
|
if (event.key === "Escape") {
|
2020-07-07 04:24:04 +00:00
|
|
|
closeMessageModal();
|
2020-07-06 03:41:42 +00:00
|
|
|
closeAddRequestModal();
|
|
|
|
}
|
|
|
|
});
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").addEventListener("click", (e) => { if (e.target === e.currentTarget) closeAddRequestModal();});
|