diff --git a/app/api/guests.tsx b/app/api/guests.tsx index 0eff296..549356c 100644 --- a/app/api/guests.tsx +++ b/app/api/guests.tsx @@ -18,17 +18,16 @@ export function loadGuests(onLoad?: (guests: Guest[]) => void) { }); }; -export function updateGuestStatus(id: string, status: string) { - fetch("/api/guests/bulk_update.json", +export function updateGuest(guest: Guest) { + fetch(`/api/guests/${guest.id}`, { - method: 'POST', - body: JSON.stringify({ properties: { status: status }, guest_ids: [id] }), + method: 'PUT', + body: JSON.stringify({ guest: { name: guest.name, status: guest.status } }), headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), } }) - .then(() => loadGuests((guests) => null)) .catch((error) => console.error(error)); } diff --git a/app/dashboard/guests/page.tsx b/app/dashboard/guests/page.tsx index c9d7203..d575715 100644 --- a/app/dashboard/guests/page.tsx +++ b/app/dashboard/guests/page.tsx @@ -10,7 +10,7 @@ import SkeletonTable from '@/app/ui/guests/skeleton-row'; import GuestsTable from '@/app/ui/guests/table'; import { TabPanel, TabView } from 'primereact/tabview'; import { Suspense, useState } from 'react'; -import { loadGuests, updateGuestStatus } from '@/app/api/guests'; +import { loadGuests } from '@/app/api/guests'; import { loadGroups } from '@/app/api/groups'; export default function Page() { @@ -46,7 +46,7 @@ export default function Page() {
}> - +
diff --git a/app/lib/definitions.ts b/app/lib/definitions.ts index f1d3641..8e7e6ae 100644 --- a/app/lib/definitions.ts +++ b/app/lib/definitions.ts @@ -18,12 +18,13 @@ export type Customer = { image_url: string; }; +export type GuestStatus = 'considered' | 'invited' | 'confirmed' | 'declined' | 'tentative'; export type Guest = { id: string; name: string; group_name?: string; color?: string; - status?: 'considered' | 'invited' | 'confirmed' | 'declined' | 'tentative'; + status?: GuestStatus } export type Expense = { diff --git a/app/ui/guests/table.tsx b/app/ui/guests/table.tsx index 7b6d427..2e6dfae 100644 --- a/app/ui/guests/table.tsx +++ b/app/ui/guests/table.tsx @@ -2,39 +2,19 @@ 'use client'; -import { Guest } from '@/app/lib/definitions'; -import { getCsrfToken } from '@/app/lib/utils'; +import { updateGuest } from '@/app/api/guests'; +import { Guest, GuestStatus } from '@/app/lib/definitions'; import { classNames } from '@/app/ui/components/button'; import clsx from 'clsx'; -import React from 'react'; import InlineTextField from '../components/form/inlineTextField'; import TableOfContents from '../components/table-of-contents'; -export default function guestsTable({ guests, updateGuestStatus }: { guests: Guest[], updateGuestStatus: (id: string, status: string) => void }) { - 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)); +export default function guestsTable({ guests }: { guests: Guest[] }) { + const handleGuestChange = (guest: Guest, status: GuestStatus) => { + guest.status = status; + updateGuest(guest); } - - const handleGuestChange = (e: React.MouseEvent, status: string) => { - updateGuestStatus(e.currentTarget.getAttribute('data-guest-id') || '', status); - } - - return ( ( - { guest.name = newName; handleGuestUpdate(guest) }} /> + { guest.name = newName; updateGuest(guest) }} /> {guest.group_name} @@ -65,14 +45,14 @@ export default function guestsTable({ guests, updateGuestStatus }: { guests: Gue - {guest.status === 'considered' && ()} {(guest.status === 'invited' || guest.status === 'tentative') && ( <> - - {guest.status != 'tentative' && } - + + {guest.status != 'tentative' && } + )}