Adds deleting trackers and hits

This commit is contained in:
2025-10-30 10:58:58 +01:00
parent 7f03d83f72
commit 076cf40b07

View File

@@ -162,7 +162,7 @@
<div class="container">
<h1>Setra</h1>
<form onsubmit="createTrigger(event)" class="create-form">
<form onsubmit="createTracker(event)" class="create-form">
<label for="name-input">Name:</label>
<input id="name-input" type="text">
@@ -174,6 +174,7 @@
<th>ID</th>
<th>Name</th>
<th>Created at</th>
<th></th>
</tr>
</table>
@@ -185,6 +186,7 @@
<th>Agent</th>
<th>Language</th>
<th>Created at</th>
<th></th>
</tr>
</table>
@@ -204,17 +206,17 @@
const input = document.getElementById("api-key-input");
localStorage.setItem("api-key", input.value);
document.getElementById("dialog").style.display = "none";
loadTriggers();
loadTrackers();
}
function init() {
if (localStorage.getItem("api-key")) {
document.getElementById("dialog").style.display = "none";
loadTriggers();
loadTrackers();
}
}
function loadTriggers() {
function loadTrackers() {
const apiKey = localStorage.getItem("api-key");
if (!apiKey) {
return;
@@ -232,6 +234,7 @@
<th>ID</th>
<th>Name</th>
<th>Created at</th>
<th></th>
</tr>`;
trackers.forEach((tracker) => {
@@ -241,12 +244,16 @@
<td>${tracker.name}</td>
<td>${tracker.created_at}</td>
<td>
<button onclick="loadTrigger('${tracker.id}')">
Show more
<button onclick="loadTracker('${tracker.id}')">
Show
</button>
<button onclick="copyLink('${tracker.id}')">
Copy link
Copy
</button>
<button onclick="deleteTracker('${tracker.id}')">
Delete
</button>
</td>
</tr>`;
@@ -255,7 +262,7 @@
}).catch((err) => alert(`Couldn't fetch trackers: ${err}`));
}
function loadTrigger(id) {
function loadTracker(id) {
const apiKey = localStorage.getItem("api-key");
if (!apiKey) {
return;
@@ -276,6 +283,7 @@
<th>Agent</th>
<th>Language</th>
<th>Created at</th>
<th></th>
</tr>`;
hits.forEach((hit) => {
@@ -286,13 +294,18 @@
<td>${hit.agent}</td>
<td>${hit.language}</td>
<td>${hit.created_at}</td>
<td>
<button onclick="deleteHit('${hit.id}', '${id}')">
Delete
</button>
</td>
</tr>`;
});
}).catch((err) => alert(`Couldn't unwrap json: ${err}`));
}).catch((err) => alert(`Couldn't fetch hits: ${err}`));
}
function createTrigger(event) {
function createTracker(event) {
event.preventDefault();
const apiKey = localStorage.getItem("api-key");
if (!apiKey) {
@@ -307,10 +320,37 @@
Authorization: apiKey,
"Content-Type": "application/json",
}
}).then(async (res) => {
console.log(await res.text());
loadTriggers();
}).catch((err) => alert(`Couldn't create trigger: ${err}`));
}).then(loadTrackers).catch((err) => alert(`Couldn't create tracker: ${err}`));
}
function deleteTracker(id) {
const apiKey = localStorage.getItem("api-key");
if (!apiKey) {
return;
}
if (!confirm("Are you sure you want to delete this tracker?")) return;
fetch(`${window.location.origin}/tracker/${id}`, {
method: "DELETE",
headers: {
Authorization: apiKey,
}
}).then(loadTrackers).catch((err) => alert(`Couldn't fetch tracker: ${err}`));
}
function deleteHit(id, trackerId) {
const apiKey = localStorage.getItem("api-key");
if (!apiKey) {
return;
}
if (!confirm("Are you sure you want to delete this hit?")) return;
fetch(`${window.location.origin}/hit/${id}`, {
method: "DELETE",
headers: {
Authorization: apiKey,
}
}).then(() => loadTracker(trackerId)).catch((err) => alert(`Couldn't delete hit: ${err}`));
}
function copyLink(id) {