Changeset 821dc0e for frontend/src/pages


Ignore:
Timestamp:
02/08/26 16:02:01 (5 months ago)
Author:
Filip Gavrilovski <filipgavrilovski28@…>
Branches:
main
Children:
2fa6841, a5406e1
Parents:
7621d7b
Message:

add add to playlist button to song detail page

File:
1 edited

Legend:

Unmodified
Added
Removed
  • frontend/src/pages/SongDetail.tsx

    r7621d7b r821dc0e  
    1 import { useEffect, useState } from "react";
     1import { useEffect, useRef, useState } from "react";
    22import { Link, useParams } from "react-router-dom";
    33import axiosInstance from "../api/axiosInstance";
     
    7171        const [reviewComment, setReviewComment] = useState("");
    7272        const [hoverRating, setHoverRating] = useState(0);
     73        const [showPlaylistDropdown, setShowPlaylistDropdown] = useState(false);
     74        const playlistDropdownRef = useRef<HTMLDivElement>(null);
    7375        console.log(user);
    7476        useEffect(() => {
     
    7880                                const response = await axiosInstance.get(`/songs/${id}/details`);
    7981                                setSong(response.data);
    80                                 // Don't autoplay - user must click play button
    8182                        } catch (err) {
    8283                                console.error("Error fetching song details:", err);
     
    8889                if (id) fetchSong();
    8990        }, [id]);
     91
     92        // handle click outside for playlist dropdown
     93        useEffect(() => {
     94                const handleClickOutside = (event: MouseEvent) => {
     95                        if (
     96                                playlistDropdownRef.current &&
     97                                !playlistDropdownRef.current.contains(event.target as Node)
     98                        ) {
     99                                setShowPlaylistDropdown(false);
     100                        }
     101                };
     102
     103                document.addEventListener("mousedown", handleClickOutside);
     104                return () => {
     105                        document.removeEventListener("mousedown", handleClickOutside);
     106                };
     107        }, []);
    90108
    91109        const handleSubmitReview = async () => {
     
    135153                        console.error("Error toggling like:", err);
    136154                }
     155        };
     156
     157        const handleAddToPlaylist = (playlistName: string) => {
     158                console.log(`Adding song ${song?.id} to ${playlistName}`);
     159                // TODO: actual API call
     160                setShowPlaylistDropdown(false);
     161        };
     162
     163        const handleCreateNewPlaylist = () => {
     164                console.log(`Creating new playlist for song ${song?.id}`);
     165                // TODO: actual playlist creation
     166                setShowPlaylistDropdown(false);
    137167        };
    138168
     
    294324
    295325                                                        {user && (
    296                                                                 <button
    297                                                                         onClick={toggleLike}
    298                                                                         className={`flex items-center gap-2 px-5 py-2.5 rounded-full text-sm font-semibold transition-colors cursor-pointer ${
    299                                                                                 song.isLikedByCurrentUser
    300                                                                                         ? "bg-[#1db954] text-black"
    301                                                                                         : "bg-white/10 text-white hover:bg-white/20"
    302                                                                         }`}
    303                                                                 >
    304                                                                         {song.isLikedByCurrentUser ? (
    305                                                                                 <svg
    306                                                                                         className="w-5 h-5"
    307                                                                                         fill="currentColor"
    308                                                                                         viewBox="0 0 24 24"
     326                                                                <>
     327                                                                        <button
     328                                                                                onClick={toggleLike}
     329                                                                                className={`flex items-center gap-2 px-5 py-3 rounded-full text-sm font-semibold transition-colors cursor-pointer ${
     330                                                                                        song.isLikedByCurrentUser
     331                                                                                                ? "bg-[#1db954] text-black"
     332                                                                                                : "bg-white/10 text-white hover:bg-white/20"
     333                                                                                }`}
     334                                                                        >
     335                                                                                {song.isLikedByCurrentUser ? (
     336                                                                                        <svg
     337                                                                                                className="w-5 h-5"
     338                                                                                                fill="currentColor"
     339                                                                                                viewBox="0 0 24 24"
     340                                                                                        >
     341                                                                                                <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
     342                                                                                        </svg>
     343                                                                                ) : (
     344                                                                                        <svg
     345                                                                                                className="w-5 h-5"
     346                                                                                                fill="none"
     347                                                                                                stroke="currentColor"
     348                                                                                                viewBox="0 0 24 24"
     349                                                                                        >
     350                                                                                                <path
     351                                                                                                        strokeLinecap="round"
     352                                                                                                        strokeLinejoin="round"
     353                                                                                                        strokeWidth={2}
     354                                                                                                        d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"
     355                                                                                                />
     356                                                                                        </svg>
     357                                                                                )}
     358                                                                                {song.isLikedByCurrentUser ? "Liked" : "Like"}
     359                                                                        </button>
     360
     361                                                                        {/* Add to Playlist button */}
     362                                                                        <div className="relative" ref={playlistDropdownRef}>
     363                                                                                <button
     364                                                                                        onClick={() =>
     365                                                                                                setShowPlaylistDropdown(!showPlaylistDropdown)
     366                                                                                        }
     367                                                                                        className="flex items-center gap-2 px-5 py-3 rounded-full text-sm font-semibold bg-white/10 text-white hover:bg-white/20 transition-colors cursor-pointer"
    309368                                                                                >
    310                                                                                         <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
    311                                                                                 </svg>
    312                                                                         ) : (
    313                                                                                 <svg
    314                                                                                         className="w-5 h-5"
    315                                                                                         fill="none"
    316                                                                                         stroke="currentColor"
    317                                                                                         viewBox="0 0 24 24"
    318                                                                                 >
    319                                                                                         <path
    320                                                                                                 strokeLinecap="round"
    321                                                                                                 strokeLinejoin="round"
    322                                                                                                 strokeWidth={2}
    323                                                                                                 d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"
    324                                                                                         />
    325                                                                                 </svg>
    326                                                                         )}
    327                                                                         {song.isLikedByCurrentUser ? "Liked" : "Like"}
    328                                                                 </button>
     369                                                                                        <svg
     370                                                                                                className="w-5 h-5"
     371                                                                                                fill="none"
     372                                                                                                stroke="currentColor"
     373                                                                                                viewBox="0 0 24 24"
     374                                                                                        >
     375                                                                                                <path
     376                                                                                                        strokeLinecap="round"
     377                                                                                                        strokeLinejoin="round"
     378                                                                                                        strokeWidth={2}
     379                                                                                                        d="M12 4v16m8-8H4"
     380                                                                                                />
     381                                                                                        </svg>
     382                                                                                        Add to Playlist
     383                                                                                </button>
     384
     385                                                                                {/* Playlist dropdown */}
     386                                                                                {showPlaylistDropdown && (
     387                                                                                        <div className="absolute left-0 top-full mt-2 w-56 bg-[#282828] rounded-lg shadow-2xl py-1 z-50 border border-white/10">
     388                                                                                                <div className="px-3 py-2 text-xs text-gray-400 border-b border-white/10">
     389                                                                                                        Add to playlist
     390                                                                                                </div>
     391                                                                                                <button
     392                                                                                                        onClick={() => handleAddToPlaylist("Hello")}
     393                                                                                                        className="w-full text-left px-4 py-2.5 text-sm text-white hover:bg-white/10 transition-colors"
     394                                                                                                >
     395                                                                                                        Hello
     396                                                                                                </button>
     397                                                                                                <button
     398                                                                                                        onClick={() => handleAddToPlaylist("Dimi")}
     399                                                                                                        className="w-full text-left px-4 py-2.5 text-sm text-white hover:bg-white/10 transition-colors"
     400                                                                                                >
     401                                                                                                        Dimi
     402                                                                                                </button>
     403                                                                                                <button
     404                                                                                                        onClick={() => handleAddToPlaylist("lmao")}
     405                                                                                                        className="w-full text-left px-4 py-2.5 text-sm text-white hover:bg-white/10 transition-colors"
     406                                                                                                >
     407                                                                                                        Kako hello world ama ti si mojot world
     408                                                                                                </button>
     409                                                                                                <button
     410                                                                                                        onClick={handleCreateNewPlaylist}
     411                                                                                                        className="w-full text-left px-4 py-2.5 text-sm text-[#1db954] hover:bg-white/10 transition-colors border-t border-white/10 flex items-center gap-2"
     412                                                                                                >
     413                                                                                                        <svg
     414                                                                                                                className="w-4 h-4"
     415                                                                                                                fill="none"
     416                                                                                                                stroke="currentColor"
     417                                                                                                                viewBox="0 0 24 24"
     418                                                                                                        >
     419                                                                                                                <path
     420                                                                                                                        strokeLinecap="round"
     421                                                                                                                        strokeLinejoin="round"
     422                                                                                                                        strokeWidth={2}
     423                                                                                                                        d="M12 4v16m8-8H4"
     424                                                                                                                />
     425                                                                                                        </svg>
     426                                                                                                        Create new playlist
     427                                                                                                </button>
     428                                                                                        </div>
     429                                                                                )}
     430                                                                        </div>
     431                                                                </>
    329432                                                        )}
    330433                                                </div>
Note: See TracChangeset for help on using the changeset viewer.