2024-10-27 21:11:45 +00:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
2024-10-27 11:52:42 +01:00
|
|
|
'use client';
|
2024-08-11 14:38:49 +02:00
|
|
|
|
2024-10-27 11:33:40 +01:00
|
|
|
import { Guest } from '@/app/lib/definitions';
|
2024-10-27 14:02:10 +01:00
|
|
|
import { getCsrfToken } from '@/app/lib/utils';
|
2024-11-17 16:10:01 +01:00
|
|
|
import { classNames } from '@/app/ui/components/button';
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import React from 'react';
|
2024-11-11 07:45:16 +01:00
|
|
|
import InlineTextField from '../components/form/inlineTextField';
|
2024-11-17 16:10:01 +01:00
|
|
|
import TableOfContents from '../components/table-of-contents';
|
2024-08-11 14:38:49 +02:00
|
|
|
|
2024-11-17 16:10:01 +01:00
|
|
|
export default function guestsTable({ guests, updateGuestStatus }: { guests: Guest[], updateGuestStatus: (id: string, status: string) => void }) {
|
2024-10-27 19:24:21 +01:00
|
|
|
const handleInviteGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'invited');
|
|
|
|
const handleConfirmGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'confirmed');
|
|
|
|
const handleDeclineGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'declined');
|
2024-10-27 19:37:24 +01:00
|
|
|
const handleTentativeGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'tentative');
|
2024-10-27 11:33:40 +01:00
|
|
|
|
2024-11-17 16:10:01 +01:00
|
|
|
const handleGuestUpdate = (guest: Guest) => {
|
2024-11-11 07:45:16 +01:00
|
|
|
fetch(`/api/guests/${guest.id}`,
|
2024-10-27 14:02:10 +01:00
|
|
|
{
|
2024-11-17 16:10:01 +01:00
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify({ guest: { name: guest.name } }),
|
2024-10-27 14:02:10 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
})
|
2024-10-27 11:33:40 +01:00
|
|
|
.catch((error) => console.error(error));
|
2024-10-27 11:52:42 +01:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:10:01 +01:00
|
|
|
|
|
|
|
const handleGuestChange = (e: React.MouseEvent<HTMLElement>, status: string) => {
|
|
|
|
updateGuestStatus(e.currentTarget.getAttribute('data-guest-id') || '', status);
|
|
|
|
}
|
|
|
|
|
2024-08-11 12:03:11 +02:00
|
|
|
|
|
|
|
return (
|
2024-11-10 20:55:03 +01:00
|
|
|
<TableOfContents
|
|
|
|
headers={['Name', 'Group', 'Status', 'Actions']}
|
|
|
|
caption='Guests'
|
|
|
|
elements={guests}
|
|
|
|
rowRender={(guest) => (
|
|
|
|
<tr key={guest.id} className="bg-white border-b odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800">
|
2024-11-11 07:45:16 +01:00
|
|
|
<td scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
2024-11-17 16:10:01 +01:00
|
|
|
<InlineTextField initialValue={guest.name} onChange={(newName) => { guest.name = newName; handleGuestUpdate(guest) }} />
|
2024-11-11 07:45:16 +01:00
|
|
|
</td>
|
2024-11-10 20:55:03 +01:00
|
|
|
<td className="px-6 py-4">
|
|
|
|
{guest.group_name}
|
|
|
|
</td>
|
|
|
|
<td className="px-6 py-4">
|
|
|
|
<span className="flex items-center text-sm dark:text-white me-3">
|
|
|
|
<span className={clsx(
|
|
|
|
'flex w-2.5 h-2.5 rounded-full me-1.5 flex-shrink-0',
|
|
|
|
{
|
2024-11-16 00:45:45 +01:00
|
|
|
'bg-gray-400': guest.status === 'considered',
|
|
|
|
'bg-blue-400': guest.status === 'invited',
|
|
|
|
'bg-green-600': guest.status === 'confirmed',
|
|
|
|
'bg-red-400': guest.status === 'declined',
|
|
|
|
'bg-yellow-400': guest.status === 'tentative',
|
2024-11-10 20:55:03 +01:00
|
|
|
}
|
|
|
|
)}>
|
|
|
|
</span>
|
|
|
|
{guest.status}
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
<td>
|
2024-11-16 00:45:45 +01:00
|
|
|
{guest.status === 'considered' && (<button data-guest-id={guest.id} onClick={handleInviteGuest} className={classNames('blue')}>
|
2024-11-10 20:55:03 +01:00
|
|
|
Invite
|
|
|
|
</button>)}
|
2024-11-16 00:45:45 +01:00
|
|
|
{(guest.status === 'invited' || guest.status === 'tentative') && (
|
2024-11-10 20:55:03 +01:00
|
|
|
<>
|
|
|
|
<button data-guest-id={guest.id} onClick={handleConfirmGuest} className={classNames('green')}>Confirm</button>
|
2024-11-16 00:45:45 +01:00
|
|
|
{guest.status != 'tentative' && <button data-guest-id={guest.id} onClick={handleTentativeGuest} className={classNames('yellow')}>Tentative</button>}
|
2024-11-10 20:55:03 +01:00
|
|
|
<button data-guest-id={guest.id} onClick={handleDeclineGuest} className={classNames('red')}>Decline</button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
/>
|
2024-11-17 16:10:01 +01:00
|
|
|
);
|
2024-08-11 12:03:11 +02:00
|
|
|
}
|