'use client'; import { AbstractApi } from "@/app/api/abstract-api"; import { Guest } from "@/app/lib/guest"; import { Invitation, InvitationSerializer } from "@/app/lib/invitation"; import { draggable, dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'; import { useEffect, useRef } from "react"; import { useState } from "react"; function InvitationCard(invitation: Invitation, allGuests: Guest[]) { const [guests, setGuests] = useState(invitation.guests); const ref = useRef(null); useEffect(() => { if (ref.current) { return dropTargetForElements({ element: ref.current, onDrop: (data) => { console.log('Dropped guest ID:', data.source.element.dataset.guestId); const guestId = data.source.element.dataset.guestId; if (guestId) { const guestToAdd = allGuests.find((guest) => guest.id === guestId); if (guestToAdd) { setGuests((prevGuests) => [...prevGuests, guestToAdd]); } } }, }); } }, []); return (
{guests.length === 0 ? (

(empty invitation)

) : (
    {guests.map((guest) => (
  • {guest.name}
  • ))}
)}
) } function GuestCard(guest: Guest) { const ref = useRef(null); useEffect(() => { if (ref.current) { return draggable({ element: ref.current, onDragStart: () => console.log('Something started dragging in me!') }); } }, [guest.id]); return (

{guest.name}

) } export default function InvitationsBoard({ guests, invitations }: { guests: Array, invitations: Array }) { const api = new AbstractApi(); const serializer = new InvitationSerializer(); // Filter out guests that are already in invitations const availableGuests = guests.filter( (guest) => !invitations.some((invitation) => invitation.guests.some((invitedGuest) => invitedGuest.id === guest.id) ) ); // Sort invitations to display those without guests at the top const sortedInvitations = [...invitations].sort((a, b) => { if (a.guests.length === 0 && b.guests.length > 0) return -1; if (a.guests.length > 0 && b.guests.length === 0) return 1; return 0; }); function handleCreateInvitation() { api.create(serializer, new Invitation(), () => { console.log("Invitation created successfully"); }); } return (
{/* Left Column: Guests */}

{guests.length} guests without invitation

{availableGuests.map((guest, index) => GuestCard(guest))}
{/* Right Column: Invitations */}

{invitations.length} invitations

{sortedInvitations.map((invitation, index) => (InvitationCard(invitation, guests)))}
); }