diff --git a/src/components/playlist/PlaylistList.tsx b/src/components/playlist/PlaylistList.tsx index dddaeaa..bc32a60 100644 --- a/src/components/playlist/PlaylistList.tsx +++ b/src/components/playlist/PlaylistList.tsx @@ -1,19 +1,26 @@ -import React, { useState } from 'react'; -import { useQuery } from 'react-query'; +import React, { useRef, useState } from 'react'; +import { useQuery, useQueryClient } from 'react-query'; import { useHistory } from 'react-router-dom'; -import { Tag } from 'rsuite'; +import { Form, Input, Popover, Whisper } from 'rsuite'; import settings from 'electron-settings'; import useSearchQuery from '../../hooks/useSearchQuery'; -import { getPlaylists } from '../../api/api'; +import { createPlaylist, getPlaylists } from '../../api/api'; import ListViewType from '../viewtypes/ListViewType'; import PageLoader from '../loader/PageLoader'; import GenericPage from '../layout/GenericPage'; import GenericPageHeader from '../layout/GenericPageHeader'; import GridViewType from '../viewtypes/GridViewType'; +import { StyledButton, StyledInputGroup } from '../shared/styled'; +import { errorMessages, isFailedResponse } from '../../shared/utils'; +import { notifyToast } from '../shared/toast'; +import { AddPlaylistButton } from '../shared/ToolbarButtons'; const PlaylistList = () => { const history = useHistory(); + const queryClient = useQueryClient(); + const playlistTriggerRef = useRef(); const [sortBy] = useState('name'); + const [newPlaylistName, setNewPlaylistName] = useState(''); const [viewType, setViewType] = useState( settings.getSync('playlistViewType') || 'list' ); @@ -27,6 +34,23 @@ const PlaylistList = () => { 'comment', ]); + const handleCreatePlaylist = async (name: string) => { + try { + const res = await createPlaylist(name); + + if (isFailedResponse(res)) { + notifyToast('error', errorMessages(res)[0]); + } else { + await queryClient.refetchQueries(['playlists'], { + active: true, + }); + notifyToast('success', `Playlist "${name}" created!`); + } + } catch (err) { + notifyToast('error', err); + } + }; + const handleRowClick = (_e: any, rowData: any) => { history.push(`playlist/${rowData.id}`); }; @@ -45,7 +69,50 @@ const PlaylistList = () => { header={ {playlists.length} playlists} + subtitle={ + +
+ + setNewPlaylistName(e)} + /> + +
+ { + handleCreatePlaylist(newPlaylistName); + playlistTriggerRef.current.close(); + }} + > + Create + +
+ + } + > + + playlistTriggerRef.current.state.isOverlayShown + ? playlistTriggerRef.current.close() + : playlistTriggerRef.current.open() + } + /> +
+ } searchQuery={searchQuery} handleSearch={(e: any) => setSearchQuery(e)} clearSearchQuery={() => setSearchQuery('')} diff --git a/src/components/shared/ToolbarButtons.tsx b/src/components/shared/ToolbarButtons.tsx index 89fa3ba..bb185a1 100644 --- a/src/components/shared/ToolbarButtons.tsx +++ b/src/components/shared/ToolbarButtons.tsx @@ -100,20 +100,24 @@ export const DownloadButton = ({ ...rest }) => { export const ShuffleButton = ({ ...rest }) => { return ( - - - Shuffle - - + } tabIndex={0} {...rest}> + Shuffle + ); }; export const ClearQueueButton = ({ ...rest }) => { return ( - - - Clear - - + } tabIndex={0} {...rest}> + Clear + + ); +}; + +export const AddPlaylistButton = ({ ...rest }) => { + return ( + } tabIndex={0} {...rest}> + Add playlist + ); };