99 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-10-27 21:11:45 +00:00
/* Copyright (C) 2024 Manuel Bustillo*/
'use client';
2024-08-11 14:38:49 +02:00
import clsx from 'clsx';
import React, { useState, useEffect } from 'react';
import { Guest } from '@/app/lib/definitions';
2024-10-27 14:02:10 +01:00
import { getCsrfToken } from '@/app/lib/utils';
import {classNames} from '@/app/ui/components/button';
import TableOfContents from '../components/table-of-contents';
2024-08-11 14:38:49 +02:00
export default function guestsTable() {
const [guests, setGuests] = useState<Array<Guest>>([]);
function loadGuests() {
2024-10-27 14:02:10 +01:00
fetch("/api/guests.json")
.then((response) => response.json())
.then((data) => {
setGuests(data.data.map((record: any) => {
return ({
id: record.id,
name: record.attributes.name,
group_name: record.attributes.group_name,
status: record.attributes.status
});
}));
}, (error) => {
return [];
});
};
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');
const handleTentativeGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'tentative');
const handleGuestChange = (e: React.MouseEvent<HTMLElement>, status:string) => {
2024-10-27 14:02:10 +01:00
fetch("/api/guests/bulk_update.json",
{
method: 'POST',
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(),
}
})
.then(() => loadGuests())
.catch((error) => console.error(error));
}
guests.length === 0 && loadGuests();
2024-08-11 12:03:11 +02:00
return (
<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">
<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.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',
'bg-yellow-400': guest.status === 'Tentative',
}
)}>
</span>
{guest.status}
</span>
</td>
<td>
{guest.status === 'Considered' && (<button data-guest-id={guest.id} onClick={handleInviteGuest} className={classNames('blue')}>
Invite
</button>)}
{(guest.status === 'Invited' || guest.status === 'Tentative') && (
<>
<button data-guest-id={guest.id} onClick={handleConfirmGuest} className={classNames('green')}>Confirm</button>
{guest.status != 'Tentative' && <button data-guest-id={guest.id} onClick={handleTentativeGuest} className={classNames('yellow')}>Tentative</button>}
<button data-guest-id={guest.id} onClick={handleDeclineGuest} className={classNames('red')}>Decline</button>
</>
)}
</td>
</tr>
)}
/>
);
2024-08-11 12:03:11 +02:00
}