diff --git a/app/lib/utils.ts b/app/lib/utils.ts index b7f7cff..8c1a35a 100644 --- a/app/lib/utils.ts +++ b/app/lib/utils.ts @@ -7,6 +7,13 @@ export const formatCurrency = (amount: number) => { }); }; +export const getCsrfToken = () => { + return document.cookie + .split("; ") + .find((row) => row.startsWith("csrf-token")) + ?.split("=")[1] || 'unknown'; +} + export const formatDateToLocal = ( dateStr: string, locale: string = 'en-US', diff --git a/app/ui/guests/affinity-groups-tree.tsx b/app/ui/guests/affinity-groups-tree.tsx index 17e1371..58bb7cf 100644 --- a/app/ui/guests/affinity-groups-tree.tsx +++ b/app/ui/guests/affinity-groups-tree.tsx @@ -44,7 +44,7 @@ export default function AffinityGroupsTree() { if (nodes.length > 0) { return; } - fetch("http://localhost:3001/groups.json") + fetch("/api/groups.json") .then((response) => response.json()) .then((data) => { setNodes(data.data.map((record: any) => { diff --git a/app/ui/guests/table.tsx b/app/ui/guests/table.tsx index f9094ec..5eb896e 100644 --- a/app/ui/guests/table.tsx +++ b/app/ui/guests/table.tsx @@ -2,11 +2,14 @@ import clsx from 'clsx'; import React, { useState, useEffect } from 'react'; -import { Guest } from '@/app/lib/definitions'; +import { Guest } from '@/app/lib/definitions'; +import { getCsrfToken } from '@/app/lib/utils'; export default function guestsTable() { + const [guests, setGuests] = useState>([]); + function loadGuests() { - fetch("http://localhost:3001/guests.json") + fetch("/api/guests.json") .then((response) => response.json()) .then((data) => { setGuests(data.data.map((record: any) => { @@ -21,9 +24,22 @@ export default function guestsTable() { }, (error) => { return []; }); - } + }; - const [guests, setGuests] = useState>([]); + + const handleInviteGuest = (e: React.MouseEvent) => { + fetch("/api/guests/bulk_update.json", + { + method: 'POST', + body: JSON.stringify({ properties: { status: "invited" }, 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(); @@ -50,6 +66,7 @@ export default function guestsTable() { Status + Actions @@ -79,6 +96,11 @@ export default function guestsTable() { {guest.status} + + {guest.status === 'Considered' && ()} + ))}