diff --git a/src/app/sessions/[id]/page.tsx b/src/app/sessions/[id]/page.tsx index a14b905..194990b 100644 --- a/src/app/sessions/[id]/page.tsx +++ b/src/app/sessions/[id]/page.tsx @@ -6,6 +6,7 @@ import { ChevronLeft, Users, Calendar, GlassWater, Plus, Trash2, Loader2, Sparkl import Link from 'next/link'; import AvatarStack from '@/components/AvatarStack'; import { deleteSession } from '@/services/delete-session'; +import { closeSession } from '@/services/close-session'; import { useSession } from '@/context/SessionContext'; import { useParams, useRouter } from 'next/navigation'; import { useI18n } from '@/i18n/I18nContext'; @@ -28,6 +29,7 @@ interface Session { id: string; name: string; scheduled_at: string; + ended_at?: string; } interface SessionTasting { @@ -62,6 +64,7 @@ export default function SessionDetailPage() { const { activeSession, setActiveSession } = useSession(); const [isAddingParticipant, setIsAddingParticipant] = useState(false); const [isDeleting, setIsDeleting] = useState(false); + const [isClosing, setIsClosing] = useState(false); useEffect(() => { fetchSessionData(); @@ -148,6 +151,23 @@ export default function SessionDetailPage() { } }; + const handleCloseSession = async () => { + if (!confirm('Möchtest du diese Session wirklich abschließen?')) return; + + setIsClosing(true); + const result = await closeSession(id as string); + + if (result.success) { + if (activeSession?.id === id) { + setActiveSession(null); + } + fetchSessionData(); + } else { + alert(result.error); + } + setIsClosing(false); + }; + const handleDeleteSession = async () => { if (!confirm('Möchtest du diese Session wirklich löschen? Alle Verknüpfungen gehen verloren.')) return; @@ -230,9 +250,14 @@ export default function SessionDetailPage() { )}
-
- - Tasting Session +
+
+ + Tasting Session +
+ {session.ended_at && ( + Abgeschlossen + )}

{session.name} @@ -259,22 +284,25 @@ export default function SessionDetailPage() {

- {activeSession?.id !== session.id ? ( - - ) : ( - + {!session.ended_at && ( + activeSession?.id !== session.id ? ( + + ) : ( + + ) )} + !session.ended_at ? ( + + ) : ( +
+ +
+ ) ) : (
diff --git a/src/services/close-session.ts b/src/services/close-session.ts new file mode 100644 index 0000000..9b8a694 --- /dev/null +++ b/src/services/close-session.ts @@ -0,0 +1,30 @@ +'use server'; + +import { createClient } from '@/lib/supabase/server'; +import { revalidatePath } from 'next/cache'; + +export async function closeSession(sessionId: string) { + const supabase = await createClient(); + + try { + const { error } = await supabase + .from('tasting_sessions') + .update({ + ended_at: new Date().toISOString(), + }) + .eq('id', sessionId); + + if (error) throw error; + + revalidatePath(`/sessions/${sessionId}`); + revalidatePath('/'); + + return { success: true }; + } catch (error) { + console.error('Close Session Error:', error); + return { + success: false, + error: error instanceof Error ? error.message : 'Fehler beim Beenden der Session', + }; + } +}