From f0ff4d59da4461493a10cead5b45dbc72307db3e Mon Sep 17 00:00:00 2001 From: jeffvli Date: Tue, 21 Sep 2021 05:46:21 -0700 Subject: [PATCH] Add functionality to playlist edit button --- src/api/api.ts | 18 +++++ src/components/playlist/PlaylistView.tsx | 94 ++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index 0484943..51a47f2 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -529,6 +529,24 @@ export const createPlaylist = async (name: string) => { return data; }; +export const updatePlaylist = async ( + playlistId: string, + name: string, + comment: string, + isPublic: boolean +) => { + const { data } = await api.get(`/updatePlaylist`, { + params: { + playlistId, + name, + comment, + public: isPublic, + }, + }); + + return data; +}; + export const clearPlaylist = async (playlistId: string) => { // Specifying the playlistId without any songs will empty the existing playlist const { data } = await api.get(`/createPlaylist`, { diff --git a/src/components/playlist/PlaylistView.tsx b/src/components/playlist/PlaylistView.tsx index f58cb8d..54a149b 100644 --- a/src/components/playlist/PlaylistView.tsx +++ b/src/components/playlist/PlaylistView.tsx @@ -1,8 +1,8 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useRef } from 'react'; import fs from 'fs'; import path from 'path'; import settings from 'electron-settings'; -import { ButtonToolbar } from 'rsuite'; +import { ButtonToolbar, Form, Input, Popover, Whisper } from 'rsuite'; import { useQuery, useQueryClient } from 'react-query'; import { useParams, useHistory } from 'react-router-dom'; import { @@ -19,6 +19,7 @@ import { getPlaylist, updatePlaylistSongsLg, updatePlaylistSongs, + updatePlaylist, } from '../../api/api'; import { useAppDispatch, useAppSelector } from '../../redux/hooks'; import { @@ -53,6 +54,11 @@ import { addProcessingPlaylist, removeProcessingPlaylist, } from '../../redux/miscSlice'; +import { + StyledButton, + StyledCheckbox, + StyledInputGroup, +} from '../shared/styled'; interface PlaylistParams { id: string; @@ -62,6 +68,7 @@ const PlaylistView = ({ ...rest }) => { const dispatch = useAppDispatch(); const history = useHistory(); const queryClient = useQueryClient(); + const editTriggerRef = useRef(); const { id } = useParams(); const playlistId = rest.id ? rest.id : id; const { isLoading, isError, data, error }: any = useQuery( @@ -69,6 +76,10 @@ const PlaylistView = ({ ...rest }) => { () => getPlaylist(playlistId), { refetchOnWindowFocus: false } ); + const [editName, setEditName] = useState(''); + const [editDescription, setEditDescription] = useState(''); + const [editPublic, setEditPublic] = useState(false); + const [isSubmittingEdit, setIsSubmittingEdit] = useState(false); const [localPlaylistData, setLocalPlaylistData] = useState(data); const [isModified, setIsModified] = useState(false); const playQueue = useAppSelector((state) => state.playQueue); @@ -96,6 +107,9 @@ const PlaylistView = ({ ...rest }) => { useEffect(() => { // Set the local playlist data on any changes setLocalPlaylistData(data?.song); + setEditName(data?.name); + setEditDescription(data?.comment); + setEditPublic(data?.public); }, [data]); useEffect(() => { @@ -217,6 +231,27 @@ const PlaylistView = ({ ...rest }) => { dispatch(removeProcessingPlaylist(data.id)); }; + const handleEdit = async () => { + setIsSubmittingEdit(true); + const res = await updatePlaylist( + data.id, + editName, + editDescription, + editPublic + ); + + if (isFailedResponse(res)) { + notifyToast('error', errorMessages(res)[0]); + } else { + queryClient.refetchQueries(['playlist'], { + active: true, + }); + } + + setIsSubmittingEdit(false); + editTriggerRef.current.close(); + }; + const handleDelete = async () => { try { const res = await deletePlaylist(data.id); @@ -306,10 +341,57 @@ const PlaylistView = ({ ...rest }) => { } onClick={() => setLocalPlaylistData(data?.song)} /> - + +
+ + setEditName(e)} + /> + + + setEditDescription(e)} + /> + + setEditPublic(!editPublic)} + > + Public + +
+ + Edit + +
+ + } + > + +
+