From 7f3197787ca57dc058ccf1677b884cd379d6a74c Mon Sep 17 00:00:00 2001 From: jeffvli Date: Mon, 22 Nov 2021 03:46:39 -0800 Subject: [PATCH] Fix playlist update --- src/api/jellyfinApi.ts | 6 +- src/components/playlist/PlaylistView.tsx | 91 +++++++++++++++--------- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/api/jellyfinApi.ts b/src/api/jellyfinApi.ts index a06062f..d183842 100644 --- a/src/api/jellyfinApi.ts +++ b/src/api/jellyfinApi.ts @@ -208,7 +208,7 @@ const normalizeScanStatus = () => { }; export const getPlaylist = async (options: { id: string }) => { - const { data } = await jellyfinApi.get(`/Items`, { + const { data } = await jellyfinApi.get(`/users/${auth.username}/items/${options.id}`, { params: { fields: 'Genres, DateCreated, MediaSources, ChildCount', ids: options.id, @@ -221,7 +221,7 @@ export const getPlaylist = async (options: { id: string }) => { }); return { - ...normalizePlaylist(data.Items[0]), + ...normalizePlaylist(data), songCount: songData.Items.length, song: (songData.Items || []).map((entry: any) => normalizeSong(entry)), }; @@ -299,7 +299,7 @@ export const updatePlaylist = async (options: { Name: options.name, Overview: options.comment, DateCreated: options.dateCreated, - Genres: genres, // Required + Genres: genres || [], // Required Tags: [], // Required PremiereDate: null, // Required ProviderIds: {}, // Required diff --git a/src/components/playlist/PlaylistView.tsx b/src/components/playlist/PlaylistView.tsx index f82fc46..2b5cfde 100644 --- a/src/components/playlist/PlaylistView.tsx +++ b/src/components/playlist/PlaylistView.tsx @@ -130,10 +130,10 @@ const PlaylistView = ({ ...rest }) => { useEffect(() => { // Set the local playlist data on any changes - dispatch(setPlaylistData(data?.song)); - setEditName(data?.title); - setEditDescription(data?.comment); - setEditPublic(data?.public); + dispatch(setPlaylistData(data?.song || [])); + setEditName(data?.title || ''); + setEditDescription(data?.comment || ''); + setEditPublic(data?.public || false); }, [data, dispatch]); useEffect(() => { @@ -310,6 +310,7 @@ const PlaylistView = ({ ...rest }) => { id: newPlaylistId, name: data.title, dateCreated: data.created, + comment: data.comment, genres: data.genres, }, }); @@ -326,29 +327,63 @@ const PlaylistView = ({ ...rest }) => { const handleEdit = async () => { setIsSubmittingEdit(true); - const res = await apiController({ - serverType: config.serverType, - endpoint: 'updatePlaylist', - args: - config.serverType === Server.Subsonic - ? { - id: data.id, - name: editName, - comment: editDescription, - isPublic: editPublic, - } - : null, - }); - if (isFailedResponse(res)) { - notifyToast('error', errorMessages(res)[0]); - } else { + if (config.serverType === Server.Subsonic) { + try { + const res = await apiController({ + serverType: config.serverType, + endpoint: 'updatePlaylist', + args: + config.serverType === Server.Subsonic + ? { + id: data.id, + name: editName, + comment: editDescription, + genres: data.genres, + isPublic: editPublic, + } + : null, + }); + + if (isFailedResponse(res)) { + notifyToast('error', errorMessages(res)[0]); + } else { + queryClient.setQueryData(['playlist', playlistId], (oldData: any) => { + return { ...oldData, title: editName, comment: editDescription, public: editPublic }; + }); + } + } catch { + notifyToast('error', 'Error saving playlist'); + } finally { + setIsSubmittingEdit(false); + } + } + + if (config.serverType === Server.Jellyfin) { + try { + apiController({ + serverType: config.serverType, + endpoint: 'updatePlaylist', + args: { + id: data.id, + name: editName, + comment: editDescription, + genres: data.genres, + isPublic: editPublic, + }, + }); + } catch { + notifyToast('error', 'Error saving playlist'); + } finally { + setIsSubmittingEdit(false); + } + + notifyToast('success', 'Saved playlist'); queryClient.setQueryData(['playlist', playlistId], (oldData: any) => { - return { ...oldData, name: editName, comment: editDescription, public: editPublic }; + return { ...oldData, title: editName, comment: editDescription, public: editPublic }; }); } - setIsSubmittingEdit(false); editTriggerRef.current.close(); }; @@ -370,14 +405,6 @@ const PlaylistView = ({ ...rest }) => { } }; - // const handleSaveJf = async () => { - // const { id: newPlaylistId } = await apiController({ - // serverType: config.serverType, - // endpoint: 'createPlaylist', - // args: { name: data.title }, - // }); - // }; - const handleDragEnd = () => { if (multiSelect.isDragging) { dispatch( @@ -575,10 +602,10 @@ const PlaylistView = ({ ...rest }) => { defaultChecked={editPublic} value={editPublic} onChange={(_v: any, e: boolean) => setEditPublic(e)} + disabled={config.serverType === Server.Jellyfin} > Public -
{ onClick={handleEdit} appearance="primary" > - Edit + Save