"use client"; import { useState, useRef } from "react"; import Image from "next/image"; import { UserMetaCardProps } from "@/interface/user"; import { uploadMedia } from "@/service/mediaService"; export default function UserMetaCard({ data }: { data: UserMetaCardProps }) { const [previewImage, setPreviewImage] = useState( data.data?.profile?.avatar_url || "/images/no-images.jpg" ); const [isUploading, setIsUploading] = useState(false); const fileInputRef = useRef(null); const handleAvatarClick = () => { if (!isUploading) { fileInputRef.current?.click(); } }; const handleFileChange = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) return; try { setIsUploading(true); console.log("Selected file:", file); const objectUrl = URL.createObjectURL(file); setPreviewImage(objectUrl); const uploadedMedia = await uploadMedia(file); console.log("Upload thành công:", uploadedMedia); } catch (error) { console.error("Lỗi khi upload avatar:", error); setPreviewImage(data.data?.profile?.avatar_url || "/images/no-images.jpg"); alert("Không thể tải ảnh lên. Vui lòng thử lại!"); } finally { setIsUploading(false); if (fileInputRef.current) { fileInputRef.current.value = ""; } } }; return ( <>
avatar
{isUploading ? ( ) : ( )}

{data.data?.profile?.display_name || "Full Name"}

{data.data?.roles?.map((role) => role.name).join(", ") || "No roles available"}

{data.data?.profile?.bio || "No bio available"}

{data.data?.profile?.location || "user location"}

); }