From: Chris Fulljames Date: Sun, 16 Mar 2025 20:09:26 +0000 (-0400) Subject: Add to playlist without refreshing page X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=ee31e845d48b2ac4033f5ab11dd6e22d486ec034;p=littlesongplace.git Add to playlist without refreshing page --- diff --git a/main.py b/main.py index 6103b4d..4e71aa0 100644 --- a/main.py +++ b/main.py @@ -817,7 +817,7 @@ def append_to_playlist(): flash_and_log(f"Added '{song_data['title']}' to {plist_data['name']}", "success") - return redirect(request.referrer) + return {"status": "success", "messages": get_flashed_messages()} @app.post("/edit-playlist/") def edit_playlist_post(playlistid): diff --git a/static/nav.js b/static/nav.js index 4926f83..f2998a8 100644 --- a/static/nav.js +++ b/static/nav.js @@ -67,19 +67,49 @@ function urlIsOnSameSite(targetUrl) { return targetUrl.origin === currentUrl.origin; } +var m_messageBoxTimeout; + async function handleAjaxResponse(response) { if (response.status != 200) { // Got an error; redirect to the error page window.location.href = response.url; } - // Update URL in browser window - var url = new URL(response.url); - // Get page content from response - var text = await response.text(); - window.history.pushState(text, "", url); + if (response.headers.get("content-type") === "application/json") + { + // JSON response - show message popup + + var data = await response.json(); + var messageBox = document.getElementById("message-box"); + messageBox.textContent = data.messages[0]; + + if (data.status === "success") + { + messageBox.style["border-color"] = "var(--blue)"; + } + else + { + messageBox.style["border-color"] = "red"; + } + + // Unhide message box for 5 seconds + clearTimeout(m_messageBoxTimeout); + messageBox.hidden = false; + m_messageBoxTimeout = setTimeout(() => {messageBox.hidden = true;}, 5000); + } + else + { + // HTML response + + // Update URL in browser window + var url = new URL(response.url); - updatePageState(text); + // Get page content from response + var text = await response.text(); + window.history.pushState(text, "", url); + + updatePageState(text); + } } // Populate page state from history stack when user navigates back diff --git a/static/styles.css b/static/styles.css index b3006ae..12bc5cf 100644 --- a/static/styles.css +++ b/static/styles.css @@ -94,6 +94,16 @@ div.page-header { margin: 20px; } +#message-box { + box-sizing: border-box; + margin: 10px; + border-width: 3px; + border-radius: 10px; + border-style: solid; + padding: 10px; + background-color: var(--yellow); +} + .title-image { image-rendering: pixelated; width: 512px; diff --git a/templates/base.html b/templates/base.html index 667651a..0def433 100644 --- a/templates/base.html +++ b/templates/base.html @@ -5,7 +5,7 @@ - + @@ -62,8 +62,11 @@
-
+ + + +