Manuel Bustillo 3cca58cfb0
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 1m53s
Playwright Tests / test (pull_request) Successful in 3m47s
Add copyright notice / copyright_notice (pull_request) Successful in 58s
Define a dialog to create a new guest
2024-11-17 16:10:01 +01:00

106 lines
3.2 KiB
TypeScript

/* Copyright (C) 2024 Manuel Bustillo*/
'use client';
import { Group, Guest } from '@/app/lib/definitions';
import { getCsrfToken } from '@/app/lib/utils';
import CreationDialog from '@/app/ui/components/creation-dialog';
import GroupsTable from '@/app/ui/groups/table';
import AffinityGroupsTree from '@/app/ui/guests/affinity-groups-tree';
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';
export default function Page() {
function loadGroups() {
fetch("/api/groups")
.then((response) => response.json())
.then((data) => {
setGroups(data.map((record: any) => {
return ({
id: record.id,
name: record.name,
color: record.color,
attendance: {
considered: record.considered,
invited: record.invited,
confirmed: record.confirmed,
tentative: record.tentative,
declined: record.declined,
total: record.total,
}
});
}));
setGroupsLoaded(true);
}, (error) => {
return [];
});
}
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,
});
}));
setGuestsLoaded(true);
}, (error) => {
return [];
});
};
const updateGuestStatus = (id: string, status: string) => {
fetch("/api/guests/bulk_update.json",
{
method: 'POST',
body: JSON.stringify({ properties: { status: status }, guest_ids: [id] }),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then(() => loadGuests())
.catch((error) => console.error(error));
}
const [groupsLoaded, setGroupsLoaded] = useState(false);
const [groups, setGroups] = useState<Array<Group>>([]);
const [guestsLoaded, setGuestsLoaded] = useState(false);
const [guests, setGuests] = useState<Array<Guest>>([]);
!groupsLoaded && loadGroups();
!guestsLoaded && loadGuests();
return (
<div className="w-full">
<AffinityGroupsTree />
<TabView>
<TabPanel header="Guests" leftIcon="pi pi-users mx-2">
<div className="flex flex-col w-full items-center justify-between">
<CreationDialog groups={groups} onCreate={loadGuests} />;
<Suspense fallback={<SkeletonTable />}>
<GuestsTable guests={guests} updateGuestStatus={updateGuestStatus} />
</Suspense>
</div>
</ TabPanel>
<TabPanel header="Groups" leftIcon="pi pi-sitemap mx-2">
<div className="flex w-full items-center justify-between">
<Suspense fallback={<SkeletonTable />}>
<GroupsTable groups={groups} />
</Suspense>
</div>
</ TabPanel>
</ TabView>
</div>
);
}