| [0d9725c] | 1 | import { useEffect, useState } from "react";
|
|---|
| 2 | import { useParams, useNavigate } from "react-router-dom";
|
|---|
| [fd81a18] | 3 | import ArtistView from "./ArtistView";
|
|---|
| 4 | import ListenerView from "./ListenerView";
|
|---|
| 5 |
|
|---|
| 6 | interface MusicalEntityDTO {
|
|---|
| 7 | id: number;
|
|---|
| 8 | title: string;
|
|---|
| 9 | genre: string;
|
|---|
| 10 | type: string;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | interface ArtistContributionDTO {
|
|---|
| 14 | musicalEntityId: number;
|
|---|
| 15 | title: string;
|
|---|
| 16 | role: string;
|
|---|
| 17 | entityType: string;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | interface Playlist {
|
|---|
| 21 | id: number;
|
|---|
| 22 | name: string;
|
|---|
| 23 | cover: string;
|
|---|
| 24 | creatorName: string;
|
|---|
| 25 | }
|
|---|
| [0d9725c] | 26 |
|
|---|
| 27 | interface User {
|
|---|
| 28 | id: number;
|
|---|
| 29 | username: string;
|
|---|
| 30 | fullName: string;
|
|---|
| 31 | userType: string;
|
|---|
| [fd81a18] | 32 | followers: number;
|
|---|
| 33 | following: number;
|
|---|
| 34 |
|
|---|
| 35 | musicalEntities?: {
|
|---|
| 36 | contributions: ArtistContributionDTO[];
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | likes?: {
|
|---|
| 40 | likedEntities: MusicalEntityDTO[];
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | createdPlaylists?: Playlist[];
|
|---|
| [0d9725c] | 44 | }
|
|---|
| 45 |
|
|---|
| 46 | const UserDetail = () => {
|
|---|
| 47 | const { userId } = useParams();
|
|---|
| 48 | const navigate = useNavigate();
|
|---|
| 49 | const [user, setUser] = useState<User | null>(null);
|
|---|
| [a3ae097] | 50 | const [error, setError] = useState<string | null>(null);
|
|---|
| [0d9725c] | 51 |
|
|---|
| 52 | useEffect(() => {
|
|---|
| 53 | const fetchUser = async () => {
|
|---|
| [a3ae097] | 54 | setError(null);
|
|---|
| 55 | try {
|
|---|
| 56 | const response = await fetch(`http://localhost:8080/users/${userId}`);
|
|---|
| 57 |
|
|---|
| 58 | if (!response.ok) {
|
|---|
| 59 | const errorData = await response.json();
|
|---|
| 60 | throw new Error(errorData.error || "Failed to fetch user");
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | const data = await response.json();
|
|---|
| 64 | setUser(data);
|
|---|
| 65 | } catch (err: any) {
|
|---|
| 66 | setError(err.message);
|
|---|
| 67 | }
|
|---|
| [0d9725c] | 68 | };
|
|---|
| 69 | fetchUser();
|
|---|
| 70 | }, [userId]);
|
|---|
| 71 |
|
|---|
| [a3ae097] | 72 | if (error) {
|
|---|
| 73 | return (
|
|---|
| 74 | <div className="p-6 bg-red-50 border border-red-200 text-red-700 rounded-lg">
|
|---|
| 75 | <h2 className="font-bold">Error</h2>
|
|---|
| 76 | <p>{error}</p>
|
|---|
| 77 | </div>
|
|---|
| 78 | );
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| [0d9725c] | 81 | if (!user) return <div className="p-6">Loading...</div>;
|
|---|
| 82 |
|
|---|
| 83 | return (
|
|---|
| 84 | <div className="container mx-auto p-6">
|
|---|
| [fd81a18] | 85 | <button
|
|---|
| 86 | onClick={() => navigate(-1)}
|
|---|
| [0d9725c] | 87 | className="mb-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
|
|---|
| 88 | >
|
|---|
| [fd81a18] | 89 | ← Back
|
|---|
| 90 | </button>
|
|---|
| 91 |
|
|---|
| [0d9725c] | 92 | <div className="bg-white shadow-lg rounded-lg p-8">
|
|---|
| [fd81a18] | 93 | <div className="flex items-start gap-6 mb-8">
|
|---|
| 94 | <div className="shrink-0">
|
|---|
| 95 | <div className="w-32 h-32 rounded-full bg-linear-to-br from-blue-400 to-purple-500 flex items-center justify-center text-white text-4xl font-bold shadow-lg">
|
|---|
| 96 | {user.fullName.charAt(0).toUpperCase()}
|
|---|
| [0d9725c] | 97 | </div>
|
|---|
| 98 | </div>
|
|---|
| 99 |
|
|---|
| [fd81a18] | 100 | <div className="flex-1">
|
|---|
| 101 | <h1 className="text-4xl font-bold mb-2">{user.fullName}</h1>
|
|---|
| 102 | <span className="inline-block px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm font-medium mb-4">
|
|---|
| 103 | {user.userType}
|
|---|
| 104 | </span>
|
|---|
| 105 |
|
|---|
| 106 | <div className="flex gap-6 mb-4 text-gray-700">
|
|---|
| 107 | <div className="flex flex-col">
|
|---|
| 108 | <span className="text-2xl font-bold">{user.followers}</span>
|
|---|
| 109 | <span className="text-sm text-gray-500">Followers</span>
|
|---|
| 110 | </div>
|
|---|
| 111 | <div className="flex flex-col">
|
|---|
| 112 | <span className="text-2xl font-bold">{user.following}</span>
|
|---|
| 113 | <span className="text-sm text-gray-500">Following</span>
|
|---|
| 114 | </div>
|
|---|
| [0d9725c] | 115 | </div>
|
|---|
| [fd81a18] | 116 |
|
|---|
| 117 | <button className="px-6 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors font-medium">
|
|---|
| 118 | Follow
|
|---|
| 119 | </button>
|
|---|
| [0d9725c] | 120 | </div>
|
|---|
| [fd81a18] | 121 | </div>
|
|---|
| 122 |
|
|---|
| 123 | {user.userType === "ARTIST" && user.musicalEntities?.contributions && (
|
|---|
| 124 | <ArtistView contributions={user.musicalEntities.contributions} />
|
|---|
| 125 | )}
|
|---|
| 126 |
|
|---|
| 127 | {user.userType === "LISTENER" && user.likes?.likedEntities && (
|
|---|
| 128 | <ListenerView
|
|---|
| 129 | likedEntities={user.likes.likedEntities}
|
|---|
| 130 | playlists={user.createdPlaylists}
|
|---|
| 131 | />
|
|---|
| [0d9725c] | 132 | )}
|
|---|
| 133 | </div>
|
|---|
| 134 | </div>
|
|---|
| 135 | );
|
|---|
| 136 | };
|
|---|
| 137 |
|
|---|
| 138 | export default UserDetail;
|
|---|