add a bit more polish

This commit is contained in:
Adora Laura Kalb 2023-04-28 23:39:02 +02:00
parent 63dc9125c0
commit a5b6232c77
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
6 changed files with 63 additions and 8 deletions

View file

@ -57,7 +57,7 @@ func SetupFiber() error {
fiberapp.Post("/admin/login", web.HandleAdminLoginPost)
fiberapp.Get("/admin/", web.HandleAdminLinkIndexGet)
fiberapp.Get("/admin/links/add", web.HandleAdminLinkAddGet)
fiberapp.Get("/admin/links/new", web.HandleAdminLinkNewGet)
fiberapp.Static("/admin/", "./web")

View file

@ -7,7 +7,7 @@ import (
"github.com/gofiber/fiber/v2"
)
func HandleAdminLinkAddGet(c *fiber.Ctx) error {
func HandleAdminLinkNewGet(c *fiber.Ctx) error {
return c.Render("add_link", nil)
}

View file

@ -12,6 +12,8 @@
</head>
<body>
<a href="/admin/links/new"><button type="button" id="add-new-button">Add new Shortlink</button></a>
<table>
<thead>
<tr>
@ -24,8 +26,8 @@
<tbody>
{{range .}}
<tr>
<td>{{.ID}}</td>
<td>{{.URL}}</td>
<td><a href="#" onclick="HandleCopy('{{.ID}}')">{{.ID}}</a></td>
<td><a href="{{.URL}}" target="_blank">{{.URL}}</a></td>
<td style="text-align: right;">
<button type="button" id="edit-button" onclick="HandleEdit('{{.ID}}')">
Edit
@ -43,13 +45,13 @@
<footer>
<p>go-urlsh - <a href="/">Home</a> -
<p>go-urlsh - <a href="/admin/">Home</a> -
<a href="https://codeberg.org/lauralani/go-urlsh" target="_blank">Source Code</a> -
<a href="https://codeberg.org/lauralani/go-urlsh/src/branch/main/LICENSE" target="_blank">License</a>
</p>
</footer>
<script src="/admin/link_add.js"></script>
<script src="/admin/links.js"></script>
</body>
</html>

37
web/link_add.js Normal file
View file

@ -0,0 +1,37 @@
async function HandleSubmit() {
document.getElementById("submit").active = false
let slug = document.getElementById("linkname").value
let url = document.getElementById("link").value
let description = document.getElementById("description").value
let body = {
"id" : slug,
"url" : url,
"description" : description
}
let response = await fetch("/api/v1/links", {
credentials: "include",
body: JSON.stringify(body),
mode: "same-origin",
method: "POST"
});
if (!response.ok) {
document.getElementById("dialog-heading").textContent = "Error"
document.getElementById("dialog-text").textContent = "The following error occured during the request: " + response.statusText
document.getElementById('dialog-info').showModal()
document.getElementById("submit").active = true
}
document.location = "/admin/"
}
async function HandleChange() {
console.log("HandleChange")
let buttonactive = true
if (document.getElementById("link").value === "")
{
buttonactive = false
}
document.getElementById("submit").active = buttonactive
}

View file

@ -22,8 +22,8 @@
<tbody>
{{range .}}
<tr>
<td>{{.ID}}</td>
<td>{{.URL}}</td>
<td><a onclick="HandleCopy('{{.id}}')">{{.ID}}</a></td>
<td><a href="{{.URL}}" target="_blank">{{.URL}}</a></td>
<td>
<button type="button" id="edit-button" onclick="HandleEdit('{{.ID}}')">

View file

@ -0,0 +1,16 @@
async function HandleDelete(id){
let response = await fetch("/api/v1/links/" + id, {
credentials: "include",
mode: "same-origin",
method: "DELETE"
});
if (!response.ok) {
console.log("error deleting " + id + ": " + response.statusText)
}
document.location = "/admin/"
}
async function HandleCopy(id) {
let host = window.location.protocol + "//" + window.location.host;
await navigator.clipboard.writeText(host + "/" + id);
}