mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 05:57:00 +01:00
refactor(soc): extract useMyListings and useWorkshopRegistrations hooks
Removes duplicate fetch logic from dashboard/listings and consolidates state/action handlers for workshop registrations into reusable hooks. Also fixes DRY violation where fetchListings logic was duplicated in useEffect. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
d7352a5ab3
commit
b6921fed69
4 files changed
+328
-261
No files matched your search
@@ -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<MyListing[]>([])
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [statusFilter, setStatusFilter] = useState('')
|
||||
const [deletingId, setDeletingId] = useState<string | null>(null)
|
||||
const [duplicatingId, setDuplicatingId] = useState<string | null>(null)
|
||||
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(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<void>(`/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 (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<Heading level={1} className="text-2xl font-bold text-neutral-900 dark:text-white">{t('pageTitle')}</Heading>
|
||||
<p className="text-sm text-neutral-500 dark:text-neutral-400 mt-1">
|
||||
{t('pageSubtitle')}
|
||||
</p>
|
||||
<p className="text-sm text-neutral-500 dark:text-neutral-400 mt-1">{t('pageSubtitle')}</p>
|
||||
</div>
|
||||
<Link
|
||||
href="/marketplace/sell"
|
||||
@@ -169,7 +80,6 @@ export default function MyListingsPage() {
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Status Tabs */}
|
||||
<div className="flex gap-1 bg-neutral-100 dark:bg-neutral-800 rounded-lg p-1">
|
||||
{STATUS_TABS.map(tab => (
|
||||
<button
|
||||
@@ -186,7 +96,6 @@ export default function MyListingsPage() {
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Loading */}
|
||||
{isLoading && (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="w-8 h-8 text-primary-600 animate-spin" />
|
||||
@@ -194,19 +103,17 @@ export default function MyListingsPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Error */}
|
||||
{error && !isLoading && (
|
||||
<div className="bg-error-50 dark:bg-error-900/20 border border-error-200 dark:border-error-800 rounded-xl p-6 text-center">
|
||||
<AlertCircle className="w-12 h-12 text-error-500 mx-auto mb-4" />
|
||||
<p className="text-error-600 dark:text-error-300 mb-4">{error}</p>
|
||||
<Button onClick={() => fetchListings(page)} variant="destructive" className="gap-2">
|
||||
<Button onClick={() => fetchListings(page, statusFilter)} variant="destructive" className="gap-2">
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
{t('retry')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Empty State */}
|
||||
{!isLoading && !error && listings.length === 0 && (
|
||||
<EmptyState
|
||||
icon={Package}
|
||||
@@ -226,7 +133,6 @@ export default function MyListingsPage() {
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Listings List */}
|
||||
{!isLoading && !error && listings.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
{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 */}
|
||||
<Link href={`/marketplace/${listing.id}`} className="flex-shrink-0 w-16 h-16 rounded-lg overflow-hidden">
|
||||
<ListingImage src={listing.thumbnail} alt={listing.title} fallbackIconSize="w-6 h-6" />
|
||||
</Link>
|
||||
|
||||
{/* Info */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<Link href={`/marketplace/${listing.id}`} className="hover:text-primary-600 transition-colors">
|
||||
<Heading level={3} className="font-medium text-neutral-900 dark:text-white truncate">{listing.title}</Heading>
|
||||
@@ -268,14 +172,12 @@ export default function MyListingsPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status Badge */}
|
||||
{statusConfig && (
|
||||
<span className={`inline-flex px-2.5 py-1 text-xs font-semibold rounded-full ${statusConfig.color}`}>
|
||||
{statusConfig.label}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-1 flex-shrink-0">
|
||||
<Link
|
||||
href={`/marketplace/sell?edit=${listing.id}`}
|
||||
@@ -315,7 +217,6 @@ export default function MyListingsPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Pagination */}
|
||||
{!isLoading && !error && totalPages > 1 && (
|
||||
<div className="flex items-center justify-between pt-4">
|
||||
<p className="text-sm text-neutral-500 dark:text-neutral-400">
|
||||
@@ -323,7 +224,7 @@ export default function MyListingsPage() {
|
||||
</p>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => fetchListings(page - 1)}
|
||||
onClick={() => fetchListings(page - 1, statusFilter)}
|
||||
disabled={page <= 1}
|
||||
className="inline-flex items-center gap-1 px-3 py-1.5 text-sm rounded-lg border border-neutral-300 dark:border-neutral-600 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-50 dark:hover:bg-neutral-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
@@ -334,7 +235,7 @@ export default function MyListingsPage() {
|
||||
{t('paginationPage', { page, total: totalPages })}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => fetchListings(page + 1)}
|
||||
onClick={() => fetchListings(page + 1, statusFilter)}
|
||||
disabled={page >= totalPages}
|
||||
className="inline-flex items-center gap-1 px-3 py-1.5 text-sm rounded-lg border border-neutral-300 dark:border-neutral-600 text-neutral-700 dark:text-neutral-300 hover:bg-neutral-50 dark:hover:bg-neutral-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
|
||||
@@ -1,113 +1,70 @@
|
||||
'use client'
|
||||
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { Calendar, CheckCircle, XCircle, AlertCircle, ArrowLeft } from 'lucide-react'
|
||||
import { WORKSHOP_REGISTRATION_STATUS } from '@/config/workshop-registration-status'
|
||||
import Link from 'next/link'
|
||||
import { getTextColor, getStatusColors } from '@/lib/design-system'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { formatDate } from '@/lib/date-formats'
|
||||
import { apiFetch } from '@/lib/api/client'
|
||||
import { ConfirmDialog } from '@/components/ui/ConfirmDialog'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { useWorkshopRegistrations } from '@/hooks/useWorkshopRegistrations'
|
||||
import type { WorkshopRegistration } from '@/hooks/useWorkshopRegistrations'
|
||||
import React from 'react'
|
||||
|
||||
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
|
||||
function getStatusIcon(status: string) {
|
||||
switch (status) {
|
||||
case WORKSHOP_REGISTRATION_STATUS.CONFIRMED:
|
||||
return <CheckCircle className="w-5 h-5 text-success-500" />
|
||||
case WORKSHOP_REGISTRATION_STATUS.PENDING:
|
||||
return <AlertCircle className="w-5 h-5 text-warning-500" />
|
||||
case WORKSHOP_REGISTRATION_STATUS.CANCELLED:
|
||||
return <XCircle className="w-5 h-5 text-error-500" />
|
||||
default:
|
||||
return <AlertCircle className="w-5 h-5 text-neutral-500" />
|
||||
}
|
||||
}
|
||||
|
||||
function Modal({ open, onClose, children }: { open: boolean; onClose: () => void; children: React.ReactNode }) {
|
||||
if (!open) return null
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" onClick={onClose}>
|
||||
<div className="bg-white rounded-xl shadow-xl w-full max-w-lg" onClick={(e) => e.stopPropagation()}>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function WorkshopsDashboard() {
|
||||
const t = useTranslations('dashboard.workshops')
|
||||
const tDates = useTranslations('dashboard.dates')
|
||||
const { data: session, status } = useSession()
|
||||
const [registrations, setRegistrations] = useState<WorkshopRegistration[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string>('')
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [editRating, setEditRating] = useState<number>(5)
|
||||
const [editFeedback, setEditFeedback] = useState<string>('')
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [pendingCancelId, setPendingCancelId] = useState<string | null>(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 || t('loadError'))
|
||||
}
|
||||
setLoading(false)
|
||||
}, [t])
|
||||
const {
|
||||
session,
|
||||
sessionStatus,
|
||||
registrations,
|
||||
loading,
|
||||
error,
|
||||
editingId,
|
||||
editRating,
|
||||
editFeedback,
|
||||
saving,
|
||||
pendingCancelId,
|
||||
setEditingId,
|
||||
setEditRating,
|
||||
setEditFeedback,
|
||||
setPendingCancelId,
|
||||
openEdit,
|
||||
saveEdit,
|
||||
doCancel,
|
||||
} = useWorkshopRegistrations({
|
||||
loadError: t('loadError'),
|
||||
saveFailed: t('saveFailed'),
|
||||
cancelFailed: t('cancelFailed'),
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (!session?.user) return
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
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<void>(`/api/workshops/registrations/${editingId}`, {
|
||||
method: 'PATCH',
|
||||
body: { rating: editRating, feedback: editFeedback }
|
||||
})
|
||||
if (result.success) {
|
||||
setEditingId(null)
|
||||
fetchRegistrations()
|
||||
} else {
|
||||
setError(result.error || t('saveFailed'))
|
||||
}
|
||||
setSaving(false)
|
||||
}
|
||||
|
||||
const doCancel = async () => {
|
||||
if (!pendingCancelId) return
|
||||
const id = pendingCancelId
|
||||
setPendingCancelId(null)
|
||||
const result = await apiFetch<void>(`/api/workshops/registrations/${id}`, {
|
||||
method: 'PATCH',
|
||||
body: { action: 'cancel' }
|
||||
})
|
||||
if (result.success) {
|
||||
fetchRegistrations()
|
||||
} else {
|
||||
setError(result.error || t('cancelFailed'))
|
||||
}
|
||||
}
|
||||
|
||||
const getStatusIcon = (status: string) => {
|
||||
switch (status) {
|
||||
case WORKSHOP_REGISTRATION_STATUS.CONFIRMED:
|
||||
return <CheckCircle className="w-5 h-5 text-success-500" />
|
||||
case WORKSHOP_REGISTRATION_STATUS.PENDING:
|
||||
return <AlertCircle className="w-5 h-5 text-warning-500" />
|
||||
case WORKSHOP_REGISTRATION_STATUS.CANCELLED:
|
||||
return <XCircle className="w-5 h-5 text-error-500" />
|
||||
default:
|
||||
return <AlertCircle className="w-5 h-5 text-neutral-500" />
|
||||
}
|
||||
}
|
||||
|
||||
const getStatusText = (status: string) => {
|
||||
function getStatusText(status: string) {
|
||||
switch (status) {
|
||||
case WORKSHOP_REGISTRATION_STATUS.CONFIRMED: return t('statusConfirmed')
|
||||
case WORKSHOP_REGISTRATION_STATUS.PENDING: return t('statusPending')
|
||||
@@ -116,8 +73,7 @@ export default function WorkshopsDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (status === 'loading' || loading) {
|
||||
if (sessionStatus === 'loading' || loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-neutral-50 py-8">
|
||||
<div className="max-w-4xl mx-auto px-4">
|
||||
@@ -142,9 +98,7 @@ export default function WorkshopsDashboard() {
|
||||
<div className="max-w-4xl mx-auto px-4">
|
||||
<div className="bg-white rounded-xl shadow-lg p-8 text-center border-2 border-neutral-200">
|
||||
<h1 className={cn('text-2xl font-bold mb-4', getTextColor('white', 'primary'))}>{t('loginRequired')}</h1>
|
||||
<p className={cn('mb-6', getTextColor('white', 'muted'))}>
|
||||
{t('loginDesc')}
|
||||
</p>
|
||||
<p className={cn('mb-6', getTextColor('white', 'muted'))}>{t('loginDesc')}</p>
|
||||
<Link
|
||||
href="/auth/login"
|
||||
className="inline-block bg-primary-600 text-white px-6 py-3 rounded-lg hover:bg-primary-700 transition-colors min-h-[touch] touch-target"
|
||||
@@ -160,7 +114,6 @@ export default function WorkshopsDashboard() {
|
||||
return (
|
||||
<div className="min-h-screen bg-neutral-50 py-8">
|
||||
<div className="max-w-4xl mx-auto px-4">
|
||||
{/* Header */}
|
||||
<div className="mb-8">
|
||||
<Link
|
||||
href="/dashboard"
|
||||
@@ -170,22 +123,18 @@ export default function WorkshopsDashboard() {
|
||||
{t('backToDashboard')}
|
||||
</Link>
|
||||
<h1 className={cn('text-3xl font-bold mb-2', getTextColor('neutral', 'primary'))}>{t('pageTitle')}</h1>
|
||||
<p className={cn('text-sm sm:text-base', getTextColor('neutral', 'muted'))}>
|
||||
{t('pageSubtitle')}
|
||||
</p>
|
||||
<p className={cn('text-sm sm:text-base', getTextColor('neutral', 'muted'))}>{t('pageSubtitle')}</p>
|
||||
</div>
|
||||
|
||||
{/* Error Message */}
|
||||
{error && (
|
||||
<div className={cn('rounded-lg p-4 mb-6 border-2', getStatusColors('error').bg, getStatusColors('error').border)}>
|
||||
<p className={cn('text-sm', getStatusColors('error').text)}>{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Registrations */}
|
||||
{registrations.length > 0 ? (
|
||||
<div className="space-y-6">
|
||||
{registrations.map((registration) => (
|
||||
{registrations.map((registration: WorkshopRegistration) => (
|
||||
<div key={registration.id} className="bg-white rounded-xl shadow-lg p-4 sm:p-6 border-2 border-neutral-200">
|
||||
<div className="flex items-start justify-between mb-4">
|
||||
<div className="flex-1">
|
||||
@@ -205,16 +154,13 @@ export default function WorkshopsDashboard() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status specific content */}
|
||||
{registration.status === WORKSHOP_REGISTRATION_STATUS.CONFIRMED && (
|
||||
<div className="bg-primary-50 border border-primary-200 rounded-lg p-4">
|
||||
<div className="flex items-center text-primary-800">
|
||||
<CheckCircle className="w-5 h-5 mr-2" />
|
||||
<span className="font-medium">{t('confirmedTitle')}</span>
|
||||
</div>
|
||||
<p className="text-primary-700 text-sm mt-1">
|
||||
{t('confirmedDesc')}
|
||||
</p>
|
||||
<p className="text-primary-700 text-sm mt-1">{t('confirmedDesc')}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -224,9 +170,7 @@ export default function WorkshopsDashboard() {
|
||||
<AlertCircle className="w-5 h-5 mr-2" />
|
||||
<span className="font-medium">{t('pendingTitle')}</span>
|
||||
</div>
|
||||
<p className="text-warning-700 text-sm mt-1">
|
||||
{t('pendingDesc')}
|
||||
</p>
|
||||
<p className="text-warning-700 text-sm mt-1">{t('pendingDesc')}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -244,7 +188,6 @@ export default function WorkshopsDashboard() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
{registration.status !== WORKSHOP_REGISTRATION_STATUS.CANCELLED && (
|
||||
<div className="mt-4 flex gap-3">
|
||||
<button
|
||||
@@ -267,12 +210,8 @@ export default function WorkshopsDashboard() {
|
||||
) : (
|
||||
<div className="bg-white rounded-xl shadow-lg p-8 text-center">
|
||||
<Calendar className="w-16 h-16 text-neutral-300 mx-auto mb-4" />
|
||||
<h3 className="text-xl font-semibold text-neutral-900 mb-2">
|
||||
{t('emptyTitle')}
|
||||
</h3>
|
||||
<p className="text-neutral-600 mb-6">
|
||||
{t('emptyDesc')}
|
||||
</p>
|
||||
<h3 className="text-xl font-semibold text-neutral-900 mb-2">{t('emptyTitle')}</h3>
|
||||
<p className="text-neutral-600 mb-6">{t('emptyDesc')}</p>
|
||||
<Link
|
||||
href="/workshops"
|
||||
className="inline-block bg-primary-600 text-white px-6 py-3 rounded-lg hover:bg-primary-700 transition-colors"
|
||||
@@ -319,8 +258,14 @@ export default function WorkshopsDashboard() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6 flex justify-end gap-3">
|
||||
<button onClick={() => setEditingId(null)} className="px-4 py-2 rounded-lg border border-neutral-300">{t('cancel')}</button>
|
||||
<button onClick={saveEdit} disabled={saving} className="px-4 py-2 rounded-lg bg-info-600 text-white disabled:opacity-50">
|
||||
<button onClick={() => setEditingId(null)} className="px-4 py-2 rounded-lg border border-neutral-300">
|
||||
{t('cancel')}
|
||||
</button>
|
||||
<button
|
||||
onClick={saveEdit}
|
||||
disabled={saving}
|
||||
className="px-4 py-2 rounded-lg bg-info-600 text-white disabled:opacity-50"
|
||||
>
|
||||
{saving ? t('saving') : t('save')}
|
||||
</button>
|
||||
</div>
|
||||
@@ -329,20 +274,3 @@ export default function WorkshopsDashboard() {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Simple modal
|
||||
function Modal({ open, onClose, children }: { open: boolean, onClose: () => void, children: React.ReactNode }) {
|
||||
if (!open) return null
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" onClick={onClose}>
|
||||
<div className="bg-white rounded-xl shadow-xl w-full max-w-lg" onClick={(e) => e.stopPropagation()}>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<MyListing[]>([])
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [statusFilter, setStatusFilter] = useState('')
|
||||
const [deletingId, setDeletingId] = useState<string | null>(null)
|
||||
const [duplicatingId, setDuplicatingId] = useState<string | null>(null)
|
||||
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(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<void>(`/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,
|
||||
}
|
||||
}
|
||||
@@ -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<WorkshopRegistration[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState('')
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [editRating, setEditRating] = useState(5)
|
||||
const [editFeedback, setEditFeedback] = useState('')
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [pendingCancelId, setPendingCancelId] = useState<string | null>(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<void>(`/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<void>(`/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,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user