diff --git a/src/lib/components/groove/CreatePlaylistDialog.svelte b/src/lib/components/groove/CreatePlaylistDialog.svelte index 8c9297a..fc6e4c4 100644 --- a/src/lib/components/groove/CreatePlaylistDialog.svelte +++ b/src/lib/components/groove/CreatePlaylistDialog.svelte @@ -1,11 +1,10 @@ @@ -48,22 +42,21 @@ Make it memorable -
- - -
+ +
diff --git a/src/lib/components/groove/PlaylistListing.svelte b/src/lib/components/groove/PlaylistListing.svelte index 8656b72..b83a058 100644 --- a/src/lib/components/groove/PlaylistListing.svelte +++ b/src/lib/components/groove/PlaylistListing.svelte @@ -26,35 +26,6 @@ const library = getLibraryState(); const player = getPlayerState(); - - const playPlaylist: SubmitFunction = async () => { - return async ({ update, result }) => { - await update({ - invalidateAll: false, - reset: false - }); - - if (result.type === 'success' && result.data) { - player.applyStatus(result.data); - } - }; - }; - - // TODO: Refactor - const deletePlaylist: SubmitFunction = async ({ action }) => { - const playlistId = parseInt(action.pathname.split('/playlists/')[1].split('/')[0]); - - return async ({ update, result }) => { - await update({ - invalidateAll: false, - reset: false - }); - - if (result.type === 'success') { - library.playlists = library.playlists.filter((p) => p.id !== playlistId); - } - }; - }; @@ -79,17 +50,23 @@
-
- -
+ -
- -
+
diff --git a/src/lib/library.svelte.ts b/src/lib/library.svelte.ts index 854ead7..2133eb2 100644 --- a/src/lib/library.svelte.ts +++ b/src/lib/library.svelte.ts @@ -14,6 +14,35 @@ class LibraryState { this.playlists = new SvelteMap(playlists.map((p) => [p.id, p])); } + async createPlaylist(name: string, fetch: typeof globalThis.fetch) { + const formData = new FormData(); + + formData.set('name', name); + + const response = await fetch(`/playlists?/create`, { + method: 'POST', + headers: { + 'x-sveltekit-action': 'true' + }, + body: formData + }); + + if (!response.ok) { + return; + } + + const result = deserialize(await response.text()); + + if (result.type === 'success' && result.data && 'playlist' in result.data) { + const playlist = result.data.playlist as Playlist; + + this.playlists.set(playlist.id, playlist); + return playlist; + } + + return; + } + async addTrackToPlaylist(playlistId: number, trackHash: string, fetch: typeof globalThis.fetch) { const response = await fetch(`/playlists/${playlistId}/add-track/${trackHash}`, { method: 'POST', @@ -72,7 +101,12 @@ class LibraryState { } } - async swapPlaylistTracks(playlistId: number, aIndex: number, bIndex: number) { + async swapPlaylistTracks( + playlistId: number, + aIndex: number, + bIndex: number, + fetch: typeof globalThis.fetch + ) { const formData = new FormData(); formData.set('a', aIndex.toString()); @@ -103,6 +137,26 @@ class LibraryState { } } } + + async deletePlaylist(playlistId: number, fetch: typeof globalThis.fetch) { + const response = await fetch(`/playlists/${playlistId}?/delete`, { + method: 'POST', + headers: { + 'x-sveltekit-action': 'true' + }, + body: new FormData() + }); + + if (!response.ok) { + return; + } + + const result = deserialize(await response.text()); + + if (result.type === 'success') { + this.playlists.delete(playlistId); + } + } } const LIBRARY_KEY = Symbol('GROOVE_LIBRARY'); diff --git a/src/routes/playlists/+page.svelte b/src/routes/playlists/+page.svelte index 3d9329f..a1d6853 100644 --- a/src/routes/playlists/+page.svelte +++ b/src/routes/playlists/+page.svelte @@ -8,7 +8,7 @@
- {#each library.playlists as playlist} + {#each library.playlists as [_, playlist]} {/each}
diff --git a/src/routes/playlists/[id]/+page.svelte b/src/routes/playlists/[id]/+page.svelte index 561d9f2..f2efb1f 100644 --- a/src/routes/playlists/[id]/+page.svelte +++ b/src/routes/playlists/[id]/+page.svelte @@ -36,7 +36,7 @@ const aIndex = e.dataTransfer?.getData('text/plain')!; const bIndex = (e.currentTarget as HTMLElement).getAttribute('data-playlist-index')!; - library.swapPlaylistTracks(playlist.id, Number(aIndex), Number(bIndex)); + library.swapPlaylistTracks(playlist.id, Number(aIndex), Number(bIndex), fetch); }