93 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-10-27 21:11:45 +00:00
/* Copyright (C) 2024 Manuel Bustillo*/
2024-11-17 16:10:01 +01:00
'use client';
import { loadGroups } from '@/app/api/groups';
import { loadGuests } from '@/app/api/guests';
2024-11-17 16:10:01 +01:00
import { Group, Guest } from '@/app/lib/definitions';
import { classNames } from '@/app/ui/components/button';
import GuestFormDialog from '@/app/ui/components/guest-form-dialog';
2024-12-08 12:45:15 +01:00
import GroupFormDialog from '@/app/ui/components/group-form-dialog';
2024-11-17 16:10:01 +01:00
import GroupsTable from '@/app/ui/groups/table';
import SkeletonTable from '@/app/ui/guests/skeleton-row';
2024-11-17 16:10:01 +01:00
import GuestsTable from '@/app/ui/guests/table';
import { TabPanel, TabView } from 'primereact/tabview';
import { Suspense, useState } from 'react';
2024-08-11 13:12:03 +02:00
export default function Page() {
2024-11-17 16:32:10 +01:00
function refreshGuests() {
loadGuests((guests) => {
setGuests(guests);
setGuestsLoaded(true);
});
2024-11-17 16:10:01 +01:00
}
2024-11-17 16:34:58 +01:00
function refreshGroups() {
loadGroups((groups) => {
setGroups(groups);
setGroupsLoaded(true);
});
}
2024-11-17 16:10:01 +01:00
const [groupsLoaded, setGroupsLoaded] = useState(false);
const [groups, setGroups] = useState<Array<Group>>([]);
2024-12-08 12:45:15 +01:00
const [groupBeingEdited, setGroupBeingEdited] = useState<Group | undefined>(undefined);
2024-11-17 16:10:01 +01:00
const [guestsLoaded, setGuestsLoaded] = useState(false);
const [guests, setGuests] = useState<Array<Guest>>([]);
const [guestBeingEdited, setGuestBeingEdited] = useState<Guest | undefined>(undefined);
2024-11-17 16:10:01 +01:00
2024-11-17 16:34:58 +01:00
!groupsLoaded && refreshGroups();
2024-11-17 16:32:10 +01:00
!guestsLoaded && refreshGuests();
2024-11-17 16:10:01 +01:00
2024-08-11 12:34:16 +02:00
return (
<div className="w-full">
<TabView>
<TabPanel header="Guests" leftIcon="pi pi-users mx-2">
2024-11-17 16:10:01 +01:00
<div className="flex flex-col w-full items-center justify-between">
<button onClick={() => setGuestBeingEdited({})} className={classNames('primary')}>Add new</button>
<GuestFormDialog
key={guestBeingEdited?.id}
groups={groups}
onCreate={() => { refreshGuests(); setGuestBeingEdited(undefined) }}
guest={guestBeingEdited}
visible={guestBeingEdited !== undefined}
onHide={() => { setGuestBeingEdited(undefined) }}
/>
<Suspense fallback={<SkeletonTable />}>
<GuestsTable
guests={guests}
onUpdate={refreshGuests}
onEdit={(guest) => setGuestBeingEdited(guest)}
/>
</Suspense>
</div>
</ TabPanel>
<TabPanel header="Groups" leftIcon="pi pi-sitemap mx-2">
2024-12-08 12:45:15 +01:00
<div className="flex flex-col w-full items-center justify-between">
<button onClick={() => setGroupBeingEdited({})} className={classNames('primary')}>Add new</button>
2024-12-08 12:45:15 +01:00
<GroupFormDialog
key={groupBeingEdited?.id}
groups={groups}
onCreate={() => { refreshGroups(); setGroupBeingEdited(undefined) }}
group={groupBeingEdited}
visible={groupBeingEdited !== undefined}
onHide={() => { setGroupBeingEdited(undefined) }}
/>
<Suspense fallback={<SkeletonTable />}>
<GroupsTable
groups={groups}
onUpdate={refreshGroups}
onEdit={(group) => setGroupBeingEdited(group)}
/>
</Suspense>
</div>
</ TabPanel>
</ TabView>
2024-08-11 12:34:16 +02:00
</div>
2024-08-11 12:34:16 +02:00
);
}