diff --git a/src/app/dashboard/listings/page.tsx b/src/app/dashboard/listings/page.tsx index c6c818394..cd16fe68e 100644 --- a/src/app/dashboard/listings/page.tsx +++ b/src/app/dashboard/listings/page.tsx @@ -1,8 +1,5 @@ 'use client' -import { useState, useEffect, useCallback } from 'react' -import { useSession } from 'next-auth/react' -import { useRouter } from 'next/navigation' import Link from 'next/link' import { Package, @@ -18,7 +15,6 @@ import { ChevronLeft, ChevronRight, } from 'lucide-react' -import { apiFetch } from '@/lib/api/client' import { useTranslations } from 'next-intl' import { ConfirmDialog } from '@/components/ui/ConfirmDialog' import Heading from '@/components/ui/Heading' @@ -28,119 +24,37 @@ import { ListingImage } from '@/components/marketplace/ListingImage' import { LISTING_STATUS_CONFIG, LISTING_STATUS, formatCHF } from '@/config/marketplace' import type { ListingStatus } from '@/config/marketplace' import { getConditionBadge } from '@/config/erfassung/conditions' - -interface MyListing { - id: string - title: string - price_chf: number - category: string - condition: string - status: string - view_count: number - favorite_count: number - created_at: string - thumbnail: string | null -} +import { useMyListings } from '@/hooks/useMyListings' export default function MyListingsPage() { const t = useTranslations('dashboard.listings') - const { data: session, status: sessionStatus } = useSession() - const STATUS_TABS: { value: string; label: string }[] = [ + const { + sessionStatus, + listings, + isLoading, + error, + statusFilter, + deletingId, + duplicatingId, + pendingDeleteId, + page, + totalPages, + total, + fetchListings, + handleStatusFilterChange, + doDelete, + handleDuplicate, + setPendingDeleteId, + } = useMyListings({ loadError: t('loadError') }) + + const STATUS_TABS = [ { value: '', label: t('tabAll') }, { value: LISTING_STATUS.ACTIVE, label: t('tabActive') }, { value: LISTING_STATUS.SOLD, label: t('tabSold') }, { value: LISTING_STATUS.DRAFT, label: t('tabDraft') }, { value: LISTING_STATUS.RESERVED, label: t('tabReserved') }, ] - const router = useRouter() - const [listings, setListings] = useState([]) - const [isLoading, setIsLoading] = useState(true) - const [error, setError] = useState(null) - const [statusFilter, setStatusFilter] = useState('') - const [deletingId, setDeletingId] = useState(null) - const [duplicatingId, setDuplicatingId] = useState(null) - const [pendingDeleteId, setPendingDeleteId] = useState(null) - const [page, setPage] = useState(1) - const [totalPages, setTotalPages] = useState(1) - const [total, setTotal] = useState(0) - - const fetchListings = useCallback(async (fetchPage = 1) => { - setIsLoading(true) - setError(null) - - const params = new URLSearchParams() - if (statusFilter) params.set('status', statusFilter) - params.set('page', String(fetchPage)) - - const result = await apiFetch<{ items: MyListing[]; page: number; totalPages: number; total: number }>(`/api/listings/mine?${params.toString()}`) - - if (result.success && result.data) { - setListings(result.data.items) - setPage(result.data.page) - setTotalPages(result.data.totalPages) - setTotal(result.data.total) - } else { - setError(result.error || t('loadError')) - } - setIsLoading(false) - }, [statusFilter, t]) - - useEffect(() => { - if (sessionStatus === 'loading') return - if (!session?.user) { - router.push('/auth/login') - return - } - let cancelled = false - async function load() { - setIsLoading(true) - setError(null) - const params = new URLSearchParams() - if (statusFilter) params.set('status', statusFilter) - params.set('page', '1') - const result = await apiFetch<{ items: MyListing[]; page: number; totalPages: number; total: number }>(`/api/listings/mine?${params.toString()}`) - if (cancelled) return - if (result.success && result.data) { - setListings(result.data.items) - setPage(result.data.page) - setTotalPages(result.data.totalPages) - setTotal(result.data.total) - } else { - setError(result.error || t('loadError')) - } - setIsLoading(false) - } - load() - return () => { cancelled = true } - }, [session, sessionStatus, router, statusFilter, t]) - - const handleStatusFilterChange = (value: string) => { - setStatusFilter(value) - setPage(1) - } - - const doDelete = async () => { - if (!pendingDeleteId) return - const id = pendingDeleteId - setPendingDeleteId(null) - setDeletingId(id) - const result = await apiFetch(`/api/listings/${id}`, { method: 'DELETE' }) - if (result.success) { - setListings(prev => prev.filter(l => l.id !== id)) - setTotal(prev => prev - 1) - } - setDeletingId(null) - } - - const handleDuplicate = async (id: string) => { - setDuplicatingId(id) - const result = await apiFetch<{ id: string }>(`/api/listings/${id}/duplicate`, { method: 'POST' }) - if (result.success && result.data?.id) { - router.push(`/marketplace/sell?edit=${result.data.id}`) - } - setDuplicatingId(null) - } if (sessionStatus === 'loading') { return ( @@ -152,13 +66,10 @@ export default function MyListingsPage() { return (
- {/* Header */}
{t('pageTitle')} -

- {t('pageSubtitle')} -

+

{t('pageSubtitle')}

- {/* Status Tabs */}
{STATUS_TABS.map(tab => (
)} - {/* Empty State */} {!isLoading && !error && listings.length === 0 && ( )} - {/* Listings List */} {!isLoading && !error && listings.length > 0 && (
{listings.map((listing) => { @@ -237,12 +143,10 @@ export default function MyListingsPage() { key={listing.id} className="bg-white dark:bg-neutral-800 rounded-xl shadow-sm border border-neutral-100 dark:border-neutral-700 p-4 flex items-center gap-4" > - {/* Thumbnail */} - {/* Info */}
{listing.title} @@ -268,14 +172,12 @@ export default function MyListingsPage() {
- {/* Status Badge */} {statusConfig && ( {statusConfig.label} )} - {/* Actions */}
)} - {/* Pagination */} {!isLoading && !error && totalPages > 1 && (

@@ -323,7 +224,7 @@ export default function MyListingsPage() {

- - +
@@ -329,20 +274,3 @@ export default function WorkshopsDashboard() {
) } - -// Simple modal -function Modal({ open, onClose, children }: { open: boolean, onClose: () => void, children: React.ReactNode }) { - if (!open) return null - return ( -
-
e.stopPropagation()}> - {children} -
-
- ) -} - - - - - diff --git a/src/hooks/useMyListings.ts b/src/hooks/useMyListings.ts new file mode 100644 index 000000000..622ef8877 --- /dev/null +++ b/src/hooks/useMyListings.ts @@ -0,0 +1,126 @@ +'use client' + +import { useState, useEffect, useCallback } from 'react' +import { useSession } from 'next-auth/react' +import { useRouter } from 'next/navigation' +import { apiFetch } from '@/lib/api/client' + +export interface MyListing { + id: string + title: string + price_chf: number + category: string + condition: string + status: string + view_count: number + favorite_count: number + created_at: string + thumbnail: string | null +} + +interface UseMyListingsErrors { + loadError: string +} + +export function useMyListings(errors: UseMyListingsErrors) { + const { data: session, status: sessionStatus } = useSession() + const router = useRouter() + + const [listings, setListings] = useState([]) + const [isLoading, setIsLoading] = useState(true) + const [error, setError] = useState(null) + const [statusFilter, setStatusFilter] = useState('') + const [deletingId, setDeletingId] = useState(null) + const [duplicatingId, setDuplicatingId] = useState(null) + const [pendingDeleteId, setPendingDeleteId] = useState(null) + const [page, setPage] = useState(1) + const [totalPages, setTotalPages] = useState(1) + const [total, setTotal] = useState(0) + + const fetchListings = useCallback(async (fetchPage: number, filter: string) => { + setIsLoading(true) + setError(null) + + const params = new URLSearchParams() + if (filter) params.set('status', filter) + params.set('page', String(fetchPage)) + + const result = await apiFetch<{ + items: MyListing[] + page: number + totalPages: number + total: number + }>(`/api/listings/mine?${params.toString()}`) + + if (result.success && result.data) { + setListings(result.data.items) + setPage(result.data.page) + setTotalPages(result.data.totalPages) + setTotal(result.data.total) + } else { + setError(result.error || errors.loadError) + } + setIsLoading(false) + }, [errors.loadError]) + + useEffect(() => { + if (sessionStatus === 'loading') return + if (!session?.user) { + router.push('/auth/login') + return + } + let cancelled = false + fetchListings(1, statusFilter).then(() => { + if (cancelled) { + setIsLoading(false) + } + }) + return () => { cancelled = true } + }, [session, sessionStatus, router, statusFilter, fetchListings]) + + const handleStatusFilterChange = (value: string) => { + setStatusFilter(value) + setPage(1) + } + + const doDelete = async () => { + if (!pendingDeleteId) return + const id = pendingDeleteId + setPendingDeleteId(null) + setDeletingId(id) + const result = await apiFetch(`/api/listings/${id}`, { method: 'DELETE' }) + if (result.success) { + setListings(prev => prev.filter(l => l.id !== id)) + setTotal(prev => prev - 1) + } + setDeletingId(null) + } + + const handleDuplicate = async (id: string) => { + setDuplicatingId(id) + const result = await apiFetch<{ id: string }>(`/api/listings/${id}/duplicate`, { method: 'POST' }) + if (result.success && result.data?.id) { + router.push(`/marketplace/sell?edit=${result.data.id}`) + } + setDuplicatingId(null) + } + + return { + sessionStatus, + listings, + isLoading, + error, + statusFilter, + deletingId, + duplicatingId, + pendingDeleteId, + page, + totalPages, + total, + fetchListings, + handleStatusFilterChange, + doDelete, + handleDuplicate, + setPendingDeleteId, + } +} diff --git a/src/hooks/useWorkshopRegistrations.ts b/src/hooks/useWorkshopRegistrations.ts new file mode 100644 index 000000000..1ec9c2143 --- /dev/null +++ b/src/hooks/useWorkshopRegistrations.ts @@ -0,0 +1,112 @@ +'use client' + +import { useState, useEffect, useCallback } from 'react' +import { useSession } from 'next-auth/react' +import { apiFetch } from '@/lib/api/client' + +export interface WorkshopRegistration { + id: string + workshop_title: string + workshop_slug: string + status: string + payment_status: string + payment_amount_cents: number | null + attended: boolean + rating: number | null + feedback: string | null + confirmed_at: string | null + cancelled_at: string | null + created_at: string + updated_at: string +} + +interface UseWorkshopRegistrationsErrors { + loadError: string + saveFailed: string + cancelFailed: string +} + +export function useWorkshopRegistrations(errors: UseWorkshopRegistrationsErrors) { + const { data: session, status: sessionStatus } = useSession() + + const [registrations, setRegistrations] = useState([]) + const [loading, setLoading] = useState(true) + const [error, setError] = useState('') + const [editingId, setEditingId] = useState(null) + const [editRating, setEditRating] = useState(5) + const [editFeedback, setEditFeedback] = useState('') + const [saving, setSaving] = useState(false) + const [pendingCancelId, setPendingCancelId] = useState(null) + + const fetchRegistrations = useCallback(async () => { + const result = await apiFetch<{ registrations: WorkshopRegistration[] }>('/api/user/workshop-registrations') + if (result.success && result.data) { + setRegistrations(result.data.registrations || []) + } else { + setError(result.error || errors.loadError) + } + setLoading(false) + }, [errors.loadError]) + + useEffect(() => { + if (!session?.user) return + fetchRegistrations() + }, [session, fetchRegistrations]) + + const openEdit = (reg: WorkshopRegistration) => { + setEditingId(reg.id) + setEditRating(reg.rating || 5) + setEditFeedback(reg.feedback || '') + } + + const saveEdit = async () => { + if (!editingId) return + setSaving(true) + const result = await apiFetch(`/api/workshops/registrations/${editingId}`, { + method: 'PATCH', + body: { rating: editRating, feedback: editFeedback }, + }) + if (result.success) { + setEditingId(null) + fetchRegistrations() + } else { + setError(result.error || errors.saveFailed) + } + setSaving(false) + } + + const doCancel = async () => { + if (!pendingCancelId) return + const id = pendingCancelId + setPendingCancelId(null) + const result = await apiFetch(`/api/workshops/registrations/${id}`, { + method: 'PATCH', + body: { action: 'cancel' }, + }) + if (result.success) { + fetchRegistrations() + } else { + setError(result.error || errors.cancelFailed) + } + } + + return { + session, + sessionStatus, + registrations, + loading, + error, + editingId, + editRating, + editFeedback, + saving, + pendingCancelId, + setEditingId, + setEditRating, + setEditFeedback, + setPendingCancelId, + openEdit, + saveEdit, + doCancel, + } +}