2020-02-14 18:52:14 +00:00
|
|
|
const refreshData = async () => {
|
2020-02-16 14:46:29 +00:00
|
|
|
let data = await fetch("/api/all").then(res => res.text());
|
2020-02-14 18:52:14 +00:00
|
|
|
data = data
|
|
|
|
.split("\n")
|
|
|
|
.filter(line => line !== "")
|
|
|
|
.map(line => line.split(","))
|
|
|
|
.map(arr => ({
|
2022-11-03 21:53:04 +00:00
|
|
|
short: arr[0],
|
2020-02-14 18:52:14 +00:00
|
|
|
long: arr[1],
|
2022-11-03 21:53:04 +00:00
|
|
|
hits: arr[2]
|
2020-02-14 18:52:14 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
displayData(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const displayData = (data) => {
|
2022-11-10 00:55:50 +00:00
|
|
|
table_box = document.querySelector(".pure-table");
|
|
|
|
if (data.length == 0) {
|
|
|
|
table_box.style.visibility = "hidden";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const table = document.querySelector("#url-table");
|
|
|
|
table_box.style.visibility = "visible";
|
|
|
|
table.innerHTML = ''; // Clear
|
|
|
|
data.map(TR)
|
|
|
|
.forEach(tr => table.appendChild(tr));
|
|
|
|
}
|
2020-02-14 18:52:14 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 01:29:01 +00:00
|
|
|
const addErrBox = async () => {
|
2022-11-09 01:18:14 +00:00
|
|
|
const controls = document.querySelector(".pure-controls");
|
|
|
|
const errBox = document.createElement("p");
|
|
|
|
errBox.setAttribute("id", "errBox");
|
|
|
|
errBox.setAttribute("style", "color:red");
|
|
|
|
errBox.innerHTML = "Short URL not valid or already in use!";
|
|
|
|
controls.appendChild(errBox);
|
|
|
|
}
|
|
|
|
|
2020-02-14 18:52:14 +00:00
|
|
|
const TR = (row) => {
|
|
|
|
const tr = document.createElement("tr");
|
|
|
|
const longTD = TD(A(row.long));
|
|
|
|
const shortTD = TD(A_INT(row.short));
|
2022-11-03 21:53:04 +00:00
|
|
|
const hitsTD = TD(row.hits);
|
2020-02-16 15:52:54 +00:00
|
|
|
const btn = deleteButton(row.short);
|
2020-02-14 18:52:14 +00:00
|
|
|
|
|
|
|
tr.appendChild(shortTD);
|
2022-11-03 19:47:38 +00:00
|
|
|
tr.appendChild(longTD);
|
2022-11-03 21:53:04 +00:00
|
|
|
tr.appendChild(hitsTD);
|
2020-02-16 15:52:54 +00:00
|
|
|
tr.appendChild(btn);
|
2020-02-14 18:52:14 +00:00
|
|
|
|
|
|
|
return tr;
|
|
|
|
};
|
|
|
|
|
|
|
|
const A = (s) => `<a href='${s}'>${s}</a>`;
|
|
|
|
const A_INT = (s) => `<a href='/${s}'>${window.location.host}/${s}</a>`;
|
|
|
|
|
2020-02-16 15:52:54 +00:00
|
|
|
const deleteButton = (shortUrl) => {
|
2022-11-10 00:55:50 +00:00
|
|
|
const td = document.createElement("td");
|
2020-02-16 15:52:54 +00:00
|
|
|
const btn = document.createElement("button");
|
|
|
|
|
|
|
|
btn.innerHTML = "×";
|
|
|
|
|
|
|
|
btn.onclick = e => {
|
|
|
|
e.preventDefault();
|
2022-11-09 23:58:15 +00:00
|
|
|
if (confirm("Do you want to delete the entry " + shortUrl + "?")) {
|
|
|
|
fetch(`/api/${shortUrl}`, {
|
|
|
|
method: "DELETE"
|
|
|
|
}).then(_ => refreshData());
|
|
|
|
}
|
2020-02-16 15:52:54 +00:00
|
|
|
};
|
2022-11-10 00:55:50 +00:00
|
|
|
td.setAttribute("name", "deleteBtn");
|
|
|
|
td.appendChild(btn);
|
|
|
|
return td;
|
2020-02-16 15:52:54 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 18:52:14 +00:00
|
|
|
const TD = (s) => {
|
|
|
|
const td = document.createElement("td");
|
2022-11-03 20:19:22 +00:00
|
|
|
const div = document.createElement("div");
|
|
|
|
div.innerHTML = s;
|
|
|
|
td.appendChild(div);
|
2020-02-14 18:52:14 +00:00
|
|
|
return td;
|
|
|
|
};
|
|
|
|
|
|
|
|
const submitForm = () => {
|
|
|
|
const form = document.forms.namedItem("new-url-form");
|
|
|
|
const longUrl = form.elements["longUrl"];
|
|
|
|
const shortUrl = form.elements["shortUrl"];
|
|
|
|
|
2020-11-09 09:30:30 +00:00
|
|
|
const url = `/api/new`;
|
2020-02-14 18:52:14 +00:00
|
|
|
|
|
|
|
fetch(url, {
|
2020-11-09 09:30:30 +00:00
|
|
|
method: "POST",
|
|
|
|
body: `${longUrl.value};${shortUrl.value}`
|
2020-02-14 18:52:14 +00:00
|
|
|
})
|
2022-11-09 00:23:41 +00:00
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
2022-11-09 00:39:23 +00:00
|
|
|
if (document.getElementById("errBox") == null) {
|
2022-11-09 01:18:14 +00:00
|
|
|
addErrBox();
|
2022-11-09 00:39:23 +00:00
|
|
|
}
|
2022-11-09 00:23:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.getElementById("errBox")?.remove();
|
|
|
|
longUrl.value = "";
|
|
|
|
shortUrl.value = "";
|
|
|
|
|
|
|
|
refreshData();
|
|
|
|
}
|
2022-11-03 19:47:38 +00:00
|
|
|
});
|
2020-02-14 18:52:14 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
await refreshData();
|
|
|
|
const form = document.forms.namedItem("new-url-form");
|
|
|
|
form.onsubmit = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
submitForm();
|
|
|
|
}
|
|
|
|
})();
|