From 7fa0adbff9f8cf45fe6c52b305b6bc6e1777bd22 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 27 Feb 2025 09:26:40 -0500 Subject: [PATCH] changing the playlist updates the artist list --- pyrite.py | 7 +++++++ static/pyrite.js | 28 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pyrite.py b/pyrite.py index 07489f4..581436e 100644 --- a/pyrite.py +++ b/pyrite.py @@ -51,6 +51,13 @@ async def get_tracks(request: Request, albumartist: str, album: str, date: str, return tracks +@app.get("/artists/") +async def get_artists(request: Request, playlist: str = ''): + """Select all artists in the database.""" + artists = await db.get_artists(request, playlist) + return artists + + @app.on_event("startup") async def startup(): app.state.db_pool = await asyncpg.create_pool(**config.db) diff --git a/static/pyrite.js b/static/pyrite.js index ec05a40..5aa365a 100644 --- a/static/pyrite.js +++ b/static/pyrite.js @@ -116,6 +116,21 @@ function populate_albums(data) { } } +function populate_artists(data) { + clear_artists(); + clear_albums(); + clear_tracks(); + for (let i = 0; i < data.length; i++) { + let artist = document.importNode(document.querySelector('#artist_template').content, true); + artist.querySelector('label').setAttribute('for', 'artist_' + i); + artist.querySelector('input').setAttribute('id', 'artist_' + i); + artist.querySelector('input').setAttribute('value', data[i]); + artist.querySelector('.artist_name').textContent = data[i]; + + document.querySelector('#artist_list').appendChild(artist); + } +} + function select_track(event) { let track = {} track['artist'] = document.querySelector('input[name=artist]:checked').value; @@ -134,9 +149,16 @@ function select_track(event) { playTrack(); } -function select_playlist(event) { - let playlist = document.querySelector('#playlist').value; - document.cookie = "playlist=" + playlist; +async function select_playlist(event) { + let playlist = document.querySelector('#playlist-select').value; + document.cookie = "playlist=" + playlist + ";max-age=" + 60*60*24*365; + + let q = new URLSearchParams(); + q.set('playlist', playlist); + let res = await fetch("./artists/?" + q.toString()); + let data = await res.json(); + + populate_artists(data); } function clear_tracks() {