/* Copyright (C) 2024 Manuel Bustillo*/ 'use client'; import clsx from 'clsx'; import React, { useState, useEffect } from 'react'; import { Guest } from '@/app/lib/definitions'; import { getCsrfToken } from '@/app/lib/utils'; import {classNames} from '@/app/ui/components/button'; import TableOfContents from '../components/table-of-contents'; import InlineTextField from '../components/form/inlineTextField'; export default function guestsTable() { const [guests, setGuests] = useState>([]); function loadGuests() { fetch("/api/guests.json") .then((response) => response.json()) .then((data) => { setGuests(data.map((record: any) => { return ({ id: record.id, name: record.name, status: record.status, group_name: record.group.name, }); })); }, (error) => { return []; }); }; const handleInviteGuest = (e: React.MouseEvent) => handleGuestChange(e, 'invited'); const handleConfirmGuest = (e: React.MouseEvent) => handleGuestChange(e, 'confirmed'); const handleDeclineGuest = (e: React.MouseEvent) => handleGuestChange(e, 'declined'); const handleTentativeGuest = (e: React.MouseEvent) => handleGuestChange(e, 'tentative'); const handleGuestUpdate = (guest:Guest) => { fetch(`/api/guests/${guest.id}`, { method: 'PUT', body: JSON.stringify({ guest: { name: guest.name } }), headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), } }) .catch((error) => console.error(error)); } const handleGuestChange = (e: React.MouseEvent, status:string) => { fetch("/api/guests/bulk_update.json", { method: 'POST', body: JSON.stringify({ properties: { status: status }, guest_ids: [e.currentTarget.getAttribute('data-guest-id')] }), headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), } }) .then(() => loadGuests()) .catch((error) => console.error(error)); } guests.length === 0 && loadGuests(); return ( ( { guest.name = newName ; handleGuestUpdate(guest) }} /> {guest.group_name} {guest.status} {guest.status === 'considered' && ()} {(guest.status === 'invited' || guest.status === 'tentative') && ( <> {guest.status != 'tentative' && } )} )} /> ); }