Skip to content

Commit

Permalink
Add playlist creation from web view
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Dec 31, 2024
1 parent 57059ee commit 126b1f7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
4 changes: 3 additions & 1 deletion backend/api/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ public IActionResult UpdateSong([FromForm] SongForm data)
if (info.Albums[key].Source != null) // We only attempt to update sources if one was provided
{
info.Albums[key].Path = source == null ? null : $"{key}.webp";

var oldSource = info.Albums[key].Source;
info.Albums[key].Source = source;
if (source != null && info.Albums[key].Source != source)
if (source != null && oldSource != source) // Source image changed
{
if (!Utils.SaveUrlAsImage(_client, source, GetImagePath(folder, key, "webp")))
{
Expand Down
3 changes: 1 addition & 2 deletions backend/api/Controllers/PlaylistController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Euphonia.API.Models;
using Euphonia.API.Models.Request;
using Euphonia.API.Models.Request;
using Euphonia.API.Models.Response;
using Euphonia.API.Services;
using Euphonia.Common;
Expand Down
15 changes: 10 additions & 5 deletions web/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
text-overflow: ellipsis;
}

/* Hovering a song */
.song:hover {
box-shadow: 0 0.5em 1em -0.125em rgba(200, 200, 200, 0.1), 0 0px 0 1px rgba(200, 200, 200, 0.02) !important;
}

/* Selected song */
.current {
box-shadow: 0 0.5em 1em -0.125em rgba(50, 110, 167, 0.2), 0 0px 0 1px rgba(50, 110, 167, 0.05) !important;
}

/* https://github.com/jgthms/bulma/issues/2398 */
.card:last-child {
margin-bottom: 1.5rem;
Expand Down Expand Up @@ -135,9 +145,4 @@
#full-player {
max-width: 100%;
}
}

/* Selected song */
.current {
box-shadow: 0 0.5em 1em -0.125em rgba(50, 110, 167, 0.2), 0 0px 0 1px rgba(50, 110, 167, 0.05) !important;
}
27 changes: 27 additions & 0 deletions web/src/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,31 @@ export async function validateIntegrity() {
alert(`Unexpected error: ${err}`);
console.error(err);
});
}

export async function createPlaylist(name, onSuccess, onFailure) {
const data = new FormData();
data.append("Name", name);

fetch(`${apiTarget}playlist/add`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${adminToken}`
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(json => {
if (json.success) {
onSuccess();
} else {
alert(`Failed to create playlist: ${json.reason}`);
onFailure();
}
})
.catch((err) => {
alert(`Failed to create playlist: ${err}`);
console.error(err);
onFailure();
});
}
20 changes: 18 additions & 2 deletions web/src/main/getMusics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as wanakana from 'wanakana';
import { registerNowPlayingAsync, registerScrobbleAsync } from "./lastfm"
import { archiveSong, favoriteSong, getApiToken, isLoggedIn, logOff, validateIntegrity } from '../common/api';
import { archiveSong, createPlaylist, favoriteSong, getApiToken, isLoggedIn, logOff, validateIntegrity } from '../common/api';
import { spawnSongNode } from './song';

let json;
Expand Down Expand Up @@ -259,7 +259,7 @@ function displayPlaylists(playlists, id, filter) {
html += getPlaylistHtml("default", "Unnamed");
}
html += `
<div class="song card requires-admin ${isLoggedIn() ? "" : "is-hidden"}">
<div id="new-playlist" class="song card requires-admin ${isLoggedIn() ? "" : "is-hidden"}">
<div class="card-content has-text-centered">
<p>
Create new playlist
Expand All @@ -268,6 +268,22 @@ function displayPlaylists(playlists, id, filter) {
</div>
`;
document.getElementById(id).innerHTML = html;

document.getElementById("new-playlist").addEventListener("click", createNewPlaylist);
}

function createNewPlaylist(_)
{
var playlistName = window.prompt("Enter new playlist name");
if (playlistName) {
createPlaylist(playlistName, () => {
json.playlists[playlistName] = {
name: playlistName,
description: null
};
displayPlaylists(json.playlists, "playlistlist", "");
}, () => {});
}
}

/// Update the song HTML of the element given in parameter
Expand Down

0 comments on commit 126b1f7

Please sign in to comment.