"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { useToast } from "@/hooks/use-toast" import { PortfolioImage } from "@/types/database" import { Loader2, Upload, Trash2, Eye, EyeOff } from "lucide-react" import Image from "next/image" export default function ArtistPortfolioPage() { const router = useRouter() const { toast } = useToast() const [loading, setLoading] = useState(true) const [uploading, setUploading] = useState(false) const [images, setImages] = useState([]) const [selectedFiles, setSelectedFiles] = useState(null) // Fetch portfolio images on mount useEffect(() => { async function fetchPortfolio() { try { const response = await fetch("/api/artists/me") if (!response.ok) throw new Error("Failed to fetch artist data") const artist = await response.json() setImages(artist.portfolioImages || []) } catch (error) { toast({ title: "Error", description: "Failed to load portfolio images", variant: "destructive" }) } finally { setLoading(false) } } fetchPortfolio() }, [toast]) const handleFileSelect = (e: React.ChangeEvent) => { if (e.target.files && e.target.files.length > 0) { setSelectedFiles(e.target.files) } } const handleUpload = async () => { if (!selectedFiles || selectedFiles.length === 0) { toast({ title: "No files selected", description: "Please select at least one image to upload", variant: "destructive" }) return } setUploading(true) try { // Upload each file for (let i = 0; i < selectedFiles.length; i++) { const file = selectedFiles[i] const formData = new FormData() formData.append("file", file) formData.append("caption", "") formData.append("tags", JSON.stringify([])) formData.append("isPublic", "true") const response = await fetch("/api/portfolio", { method: "POST", body: formData }) if (!response.ok) { throw new Error(`Failed to upload ${file.name}`) } } toast({ title: "Success", description: `${selectedFiles.length} image(s) uploaded successfully` }) // Refresh the page to show new images setSelectedFiles(null) router.refresh() // Re-fetch images const response = await fetch("/api/artists/me") const artist = await response.json() setImages(artist.portfolioImages || []) } catch (error) { console.error("Upload error:", error) toast({ title: "Error", description: error instanceof Error ? error.message : "Failed to upload images", variant: "destructive" }) } finally { setUploading(false) } } const handleDelete = async (imageId: string) => { if (!confirm("Are you sure you want to delete this image?")) { return } try { const response = await fetch(`/api/portfolio/${imageId}`, { method: "DELETE" }) if (!response.ok) throw new Error("Failed to delete image") toast({ title: "Success", description: "Image deleted successfully" }) // Remove from local state setImages(images.filter(img => img.id !== imageId)) } catch (error) { toast({ title: "Error", description: "Failed to delete image", variant: "destructive" }) } } const handleToggleVisibility = async (imageId: string, currentVisibility: boolean) => { try { const response = await fetch(`/api/portfolio/${imageId}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ isPublic: !currentVisibility }) }) if (!response.ok) throw new Error("Failed to update visibility") toast({ title: "Success", description: `Image is now ${!currentVisibility ? "public" : "private"}` }) // Update local state setImages(images.map(img => img.id === imageId ? { ...img, isPublic: !currentVisibility } : img )) } catch (error) { toast({ title: "Error", description: "Failed to update visibility", variant: "destructive" }) } } if (loading) { return (
) } return (

Portfolio Manager

Upload and manage your tattoo portfolio images

{/* Upload Section */} Upload Images Add new tattoo images to your portfolio
{selectedFiles && (

{selectedFiles.length} file(s) selected

)}
{/* Portfolio Grid */} Your Portfolio ({images.length} images) Manage visibility and delete images from your portfolio {images.length === 0 ? (

No portfolio images yet

Upload your first tattoo image above

) : (
{images.map((image) => (
{image.caption {!image.isPublic && (
Private
)}
{image.caption && (

{image.caption}

)}
))}
)}
{/* Info Card */}

Portfolio Tips

  • • Upload high-quality images of your best work
  • • Use the visibility toggle to hide images from public view
  • • Private images won't appear on your public artist profile
  • • Coming soon: Add captions, tags, and reorder images
) }