Skip to content

Commit

Permalink
Let user delete Tracks from playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
Arikatsu committed Nov 24, 2022
1 parent ac78e50 commit ce8c5af
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/backend/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ export async function addTrackToPlaylist(playlistId: string, track: TrackData):
* @param playlistId The ID of the playlist.
* @param index The index of the track to remove.
*/
export async function removeTrackFromPlaylist(playlistId: string, index: string): Promise<boolean> {
export async function removeTrackFromPlaylist(playlistId: string, index: number): Promise<boolean> {
const response = await fetch(`${targetRoute}/playlist/${playlistId}?type=remove`, {
method: "PATCH", headers: { Authorization: token() },
method: "PATCH", headers: {
Authorization: token(),
"Content-Type": "application/json"
},
body: JSON.stringify({ index })
});

Expand Down
1 change: 1 addition & 0 deletions src/css/Playlist.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
color: #fff;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;

&:hover {
background-color: transparent;
Expand Down
8 changes: 5 additions & 3 deletions src/ui/components/playlist/PlaylistTrack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { player, playFromResult } from "@backend/audio";

import Button from "@components/common/Button";
import { displayModal } from "@components/common/Modal";
import { faPause, faPlay, faAdd, faShare, faCopy, faDownload } from "@fortawesome/free-solid-svg-icons";
import { faPause, faPlay, faAdd, faShare, faCopy, faDownload, faDeleteLeft } from "@fortawesome/free-solid-svg-icons";

import "@css/Playlist.scss";

interface IProps {
track: TrackData;
onClick: () => void;
setTrack: () => void;
removeTrack: () => void;
}

interface IState {
Expand Down Expand Up @@ -64,7 +65,7 @@ class PlaylistTrack extends React.Component<IProps, IState> {
render() {
const track = this.props.track;
return (
<div className="PlaylistTrack" key={track.id} onClick={this.props.onClick}>
<div className="PlaylistTrack" key={track.id} onClick={this.props.setTrack}>

<Button
id="statusButton"
Expand All @@ -85,6 +86,7 @@ class PlaylistTrack extends React.Component<IProps, IState> {

<div className="PlaylistTrackButtons">
<Button icon={faAdd} className="TrackOptionsButtons" tooltip="Add to playlist" onClick={() => displayModal("PlaylistTrackAddModal")} />
<Button icon={faDeleteLeft} className="TrackOptionsButtons" tooltip="Remove track" onClick={this.props.removeTrack} />
<Button icon={faShare} className="TrackOptionsButtons" tooltip="Open track source" onClick={this.openTrackSource} />
<Button icon={faCopy} className="TrackOptionsButtons" tooltip="Copy track URL" onClick={this.copyTrackURL} />
<Button icon={faDownload} className="TrackOptionsButtons" tooltip="Download track" onClick={this.preview2} />
Expand Down
23 changes: 19 additions & 4 deletions src/ui/components/playlist/PlaylistTracks.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from "react";

import { Playlist, TrackData } from "@backend/types";
import { addTrackToPlaylist, removeTrackFromPlaylist, fetchAllPlaylists } from "@backend/playlist";
import emitter from "@backend/events";

import PlaylistTrack from "@components/playlist/PlaylistTrack";
import Modal from "@components/common/Modal";

import "@css/Playlist.scss";
import Modal from "@components/common/Modal";
import { addTrackToPlaylist, fetchAllPlaylists } from "@backend/playlist";

interface IProps {
tracks: TrackData[];
playlistId: string;
}

interface IState {
Expand Down Expand Up @@ -43,8 +47,14 @@ class PlaylistTracks extends React.Component<IProps, IState> {
this.hideModal();
const playlistId = (document.getElementById("PlaylistTrackAddModal-PlaylistSelect") as HTMLSelectElement).value;
await addTrackToPlaylist(playlistId, this.state.track);
emitter.emit("playlist-update");
};

deleteFromPlaylist = async (index) => {
await removeTrackFromPlaylist(this.props.playlistId, index);
emitter.emit("playlist-update");
}

componentDidUpdate() {
// Scroll to the top of the page when the results change.
document.documentElement.scrollTop = 0;
Expand All @@ -53,9 +63,14 @@ class PlaylistTracks extends React.Component<IProps, IState> {
render() {
return (
<>
{this.props.tracks.map((track) => {
{this.props.tracks.map((track, index) => {
return (
<PlaylistTrack key={track.id} track={track} onClick={() => this.setState({ track: track })} />
<PlaylistTrack
key={track.id}
track={track}
setTrack={() => this.setState({ track: track })}
removeTrack={() => this.deleteFromPlaylist(index)}
/>
);
})}

Expand Down
11 changes: 10 additions & 1 deletion src/ui/pages/PlaylistPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Modal, { displayModal } from "@components/common/Modal";

import "@css/Playlist.scss";
import { loadPlaylists } from "@backend/user";
import emitter from "@backend/events";

interface IState {
playlist: Playlist;
Expand Down Expand Up @@ -63,8 +64,16 @@ class PlaylistPage extends React.Component<any, IState> {
this.hideModal();
}
};

emitter.on("playlist-update", async () => {
await loadPlaylists();
this.setState({
playlist: fetchPlaylist(this.props.match.params.id),
});
});
}


componentWillUnmount() {
window.onclick = null;
}
Expand Down Expand Up @@ -98,7 +107,7 @@ class PlaylistPage extends React.Component<any, IState> {
</div>
<div className="PlaylistContent">
{this.state.playlist.tracks.length > 0 ? (
<PlaylistTracks tracks={this.state.playlist.tracks} />
<PlaylistTracks tracks={this.state.playlist.tracks} playlistId={this.state.playlist.id} />
) : (
<h2 id="NoTracksMessage">No tracks found.</h2>
)}
Expand Down

0 comments on commit ce8c5af

Please sign in to comment.