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:52:42 +01:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
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-08-11 14:38:49 +02:00
|
|
|
|
2024-10-27 11:52:42 +01:00
|
|
|
export default function guestsTable() {
|
2024-10-27 11:33:40 +01:00
|
|
|
const [guests, setGuests] = useState<Array<Guest>>([]);
|
|
|
|
|
2024-10-27 11:52:42 +01:00
|
|
|
function loadGuests() {
|
2024-10-27 14:02:10 +01:00
|
|
|
fetch("/api/guests.json")
|
2024-10-27 11:52:42 +01:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
setGuests(data.data.map((record: any) => {
|
|
|
|
return ({
|
|
|
|
id: record.id,
|
|
|
|
name: record.attributes.name,
|
|
|
|
email: record.attributes.email,
|
|
|
|
group_name: record.attributes.group_name,
|
|
|
|
status: record.attributes.status
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}, (error) => {
|
|
|
|
return [];
|
2024-10-27 11:10:45 +00:00
|
|
|
});
|
2024-10-27 11:33:40 +01:00
|
|
|
};
|
|
|
|
|
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-10-27 19:24:21 +01:00
|
|
|
const handleGuestChange = (e: React.MouseEvent<HTMLElement>, status:string) => {
|
2024-10-27 14:02:10 +01:00
|
|
|
fetch("/api/guests/bulk_update.json",
|
|
|
|
{
|
|
|
|
method: 'POST',
|
2024-10-27 19:24:21 +01:00
|
|
|
body: JSON.stringify({ properties: { status: status }, guest_ids: [e.currentTarget.getAttribute('data-guest-id')] }),
|
2024-10-27 14:02:10 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
})
|
2024-10-27 19:03:56 +01:00
|
|
|
.then(() => loadGuests())
|
2024-10-27 11:33:40 +01:00
|
|
|
.catch((error) => console.error(error));
|
2024-10-27 11:52:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
guests.length === 0 && loadGuests();
|
2024-10-27 19:24:21 +01:00
|
|
|
const ctaClassName = "text-white py-1 px-2 mx-1 rounded";
|
2024-08-11 12:03:11 +02:00
|
|
|
|
|
|
|
return (
|
2024-08-11 13:12:03 +02:00
|
|
|
<div className="w-full relative overflow-x-auto shadow-md sm:rounded-lg">
|
|
|
|
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
|
2024-08-11 19:44:05 +02:00
|
|
|
<caption className="p-5 text-lg font-semibold text-left rtl:text-right text-gray-900 bg-white dark:text-white dark:bg-gray-800">
|
|
|
|
Guests
|
|
|
|
<p className="mt-1 text-sm font-normal text-gray-500 dark:text-gray-400">
|
|
|
|
There are {guests.length} guests in the list
|
|
|
|
</p>
|
|
|
|
</caption>
|
2024-08-11 13:12:03 +02:00
|
|
|
<thead className="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
|
|
|
|
<tr>
|
|
|
|
<th scope="col" className="px-6 py-3">
|
|
|
|
Name
|
|
|
|
</th>
|
|
|
|
<th scope="col" className="px-6 py-3">
|
|
|
|
Email
|
|
|
|
</th>
|
|
|
|
<th scope="col" className="px-6 py-3">
|
2024-08-11 18:52:24 +02:00
|
|
|
Group
|
2024-08-11 13:12:03 +02:00
|
|
|
</th>
|
2024-08-11 19:23:19 +02:00
|
|
|
<th scope="col" className="px-6 py-3">
|
|
|
|
Status
|
|
|
|
</th>
|
2024-10-27 11:05:56 +01:00
|
|
|
<th>Actions</th>
|
2024-08-11 13:12:03 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2024-08-18 18:41:44 +02:00
|
|
|
{guests.map((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">
|
|
|
|
<th scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
|
|
|
{guest.name}
|
|
|
|
</th>
|
|
|
|
<td className="px-6 py-4">
|
|
|
|
{guest.email}
|
|
|
|
</td>
|
|
|
|
<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',
|
|
|
|
{
|
|
|
|
'bg-gray-400': guest.status === 'Considered',
|
|
|
|
'bg-blue-400': guest.status === 'Invited',
|
|
|
|
'bg-green-600': guest.status === 'Confirmed',
|
|
|
|
'bg-red-400': guest.status === 'Declined',
|
2024-10-27 19:27:33 +01:00
|
|
|
'bg-yellow-400': guest.status === 'Tentative',
|
2024-08-18 18:41:44 +02:00
|
|
|
}
|
|
|
|
)}>
|
2024-08-11 19:23:19 +02:00
|
|
|
</span>
|
2024-08-18 18:41:44 +02:00
|
|
|
{guest.status}
|
|
|
|
</span>
|
|
|
|
</td>
|
2024-10-27 11:05:56 +01:00
|
|
|
<td>
|
2024-10-27 19:24:21 +01:00
|
|
|
{guest.status === 'Considered' && (<button data-guest-id={guest.id} onClick={handleInviteGuest} className={`${ctaClassName} bg-blue-400 hover:bg-blue-600`}>
|
2024-10-27 11:05:56 +01:00
|
|
|
Invite
|
|
|
|
</button>)}
|
2024-10-27 19:41:00 +01:00
|
|
|
{(guest.status === 'Invited' || guest.status === 'Tentative') && (
|
2024-10-27 19:37:24 +01:00
|
|
|
<>
|
|
|
|
<button data-guest-id={guest.id} onClick={handleConfirmGuest} className={`${ctaClassName} bg-green-500 hover:bg-green-600`}>Confirm</button>
|
2024-10-27 19:41:00 +01:00
|
|
|
{guest.status != 'Tentative' && <button data-guest-id={guest.id} onClick={handleDeclineGuest} className={`${ctaClassName} bg-yellow-500 hover:bg-yellow-700`}>Tentative</button>}
|
2024-10-27 19:37:24 +01:00
|
|
|
<button data-guest-id={guest.id} onClick={handleDeclineGuest} className={`${ctaClassName} bg-red-500 hover:bg-red-600`}>Decline</button>
|
|
|
|
</>
|
|
|
|
)}
|
2024-10-27 11:05:56 +01:00
|
|
|
</td>
|
2024-08-18 18:41:44 +02:00
|
|
|
</tr>
|
|
|
|
))}
|
2024-08-11 13:12:03 +02:00
|
|
|
</tbody>
|
|
|
|
</table>
|
2024-08-11 12:03:11 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|