Define a dialog to create a new guest
This commit is contained in:
parent
71d9a91d18
commit
3cca58cfb0
@ -1,29 +1,101 @@
|
|||||||
/* Copyright (C) 2024 Manuel Bustillo*/
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
||||||
|
|
||||||
import AffinityGroupsTree from '@/app/ui/guests/affinity-groups-tree';
|
'use client';
|
||||||
import GuestsTable from '@/app/ui/guests/table';
|
|
||||||
import React, { Suspense } from 'react';
|
import { Group, Guest } from '@/app/lib/definitions';
|
||||||
import SkeletonTable from '@/app/ui/guests/skeleton-row';
|
import { getCsrfToken } from '@/app/lib/utils';
|
||||||
import { TabView, TabPanel } from 'primereact/tabview';
|
import CreationDialog from '@/app/ui/components/creation-dialog';
|
||||||
import GroupsTable from '@/app/ui/groups/table';
|
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() {
|
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 (
|
return (
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<AffinityGroupsTree />
|
<AffinityGroupsTree />
|
||||||
|
|
||||||
<TabView>
|
<TabView>
|
||||||
<TabPanel header="Guests" leftIcon="pi pi-users mx-2">
|
<TabPanel header="Guests" leftIcon="pi pi-users mx-2">
|
||||||
<div className="flex w-full items-center justify-between">
|
<div className="flex flex-col w-full items-center justify-between">
|
||||||
|
<CreationDialog groups={groups} onCreate={loadGuests} />;
|
||||||
<Suspense fallback={<SkeletonTable />}>
|
<Suspense fallback={<SkeletonTable />}>
|
||||||
<GuestsTable />
|
<GuestsTable guests={guests} updateGuestStatus={updateGuestStatus} />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
</ TabPanel>
|
</ TabPanel>
|
||||||
<TabPanel header="Groups" leftIcon="pi pi-sitemap mx-2">
|
<TabPanel header="Groups" leftIcon="pi pi-sitemap mx-2">
|
||||||
<div className="flex w-full items-center justify-between">
|
<div className="flex w-full items-center justify-between">
|
||||||
<Suspense fallback={<SkeletonTable />}>
|
<Suspense fallback={<SkeletonTable />}>
|
||||||
<GroupsTable />
|
<GroupsTable groups={groups} />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
</ TabPanel>
|
</ TabPanel>
|
||||||
|
@ -5,7 +5,7 @@ import clsx from "clsx";
|
|||||||
type ButtonColor = 'primary' | 'blue' | 'green' | 'red' | 'yellow';
|
type ButtonColor = 'primary' | 'blue' | 'green' | 'red' | 'yellow';
|
||||||
|
|
||||||
export function classNames(type: ButtonColor) {
|
export function classNames(type: ButtonColor) {
|
||||||
return(clsx("text-white py-1 px-2 mx-1 rounded", {
|
return (clsx("text-white py-1 px-2 mx-1 rounded disabled:opacity-50 disabled:cursor-not-allowed", {
|
||||||
'bg-blue-400 hover:bg-blue-600': type === 'primary' || type === 'blue',
|
'bg-blue-400 hover:bg-blue-600': type === 'primary' || type === 'blue',
|
||||||
'bg-green-500 hover:bg-green-600': type === 'green',
|
'bg-green-500 hover:bg-green-600': type === 'green',
|
||||||
'bg-red-500 hover:bg-red-600': type === 'red',
|
'bg-red-500 hover:bg-red-600': type === 'red',
|
||||||
|
60
app/ui/components/creation-dialog.tsx
Normal file
60
app/ui/components/creation-dialog.tsx
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
||||||
|
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { Group } from '@/app/lib/definitions';
|
||||||
|
import { getCsrfToken } from '@/app/lib/utils';
|
||||||
|
import { classNames } from '@/app/ui/components/button';
|
||||||
|
import { Dialog } from 'primereact/dialog';
|
||||||
|
import { Dropdown } from 'primereact/dropdown';
|
||||||
|
import { FloatLabel } from 'primereact/floatlabel';
|
||||||
|
import { InputText } from 'primereact/inputtext';
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function CreationDialog({ groups, onCreate }: { groups: Group[], onCreate?: () => void }) {
|
||||||
|
const [visible, setVisible] = useState(false);
|
||||||
|
|
||||||
|
const [name, setName] = useState('');
|
||||||
|
const [group, setGroup] = useState(null);
|
||||||
|
function createGuest() {
|
||||||
|
fetch("/api/guests", {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ name: name, group_id: group }),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRF-TOKEN': getCsrfToken(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data) => {
|
||||||
|
console.log(data);
|
||||||
|
setVisible(false);
|
||||||
|
onCreate && onCreate();
|
||||||
|
})
|
||||||
|
.catch((error) => console.error(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button onClick={() => setVisible(true)} className={classNames('primary')}>Add new</button>
|
||||||
|
|
||||||
|
<Dialog header="Add guest" visible={visible} style={{ width: '50vw' }} onHide={() => { if (!visible) return; setVisible(false); }}>
|
||||||
|
<div className="card flex justify-evenly py-5">
|
||||||
|
<FloatLabel>
|
||||||
|
<InputText id="username" className='rounded-sm' value={name} onChange={(e) => setName(e.target.value)} />
|
||||||
|
<label htmlFor="username">Username</label>
|
||||||
|
</FloatLabel>
|
||||||
|
<FloatLabel>
|
||||||
|
<Dropdown id="group" className='rounded-sm min-w-32' value={group} onChange={(e) => setGroup(e.target.value)} options={
|
||||||
|
groups.map((group) => {
|
||||||
|
return { label: group.name, value: group.id };
|
||||||
|
})
|
||||||
|
} />
|
||||||
|
<label htmlFor="group">Group</label>
|
||||||
|
</FloatLabel>
|
||||||
|
<button className={classNames('primary')} onClick={createGuest} disabled={!(name.length > 0 && group)}>Create</button>
|
||||||
|
</div>
|
||||||
|
</Dialog>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
@ -2,40 +2,10 @@
|
|||||||
|
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import TableOfContents from '../components/table-of-contents';
|
|
||||||
import React, { useState } from 'react';
|
|
||||||
import { Group } from '@/app/lib/definitions';
|
import { Group } from '@/app/lib/definitions';
|
||||||
|
import TableOfContents from '../components/table-of-contents';
|
||||||
|
|
||||||
export default function GroupsTable() {
|
export default function GroupsTable({ groups }: { groups: Group[] }) {
|
||||||
const [groups, setGroups] = useState<Array<Group>>([]);
|
|
||||||
const [groupsLoaded, setGroupsLoaded] = useState(false);
|
|
||||||
|
|
||||||
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 [];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
!groupsLoaded && loadGroups();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableOfContents
|
<TableOfContents
|
||||||
|
@ -2,34 +2,15 @@
|
|||||||
|
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
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';
|
import { getCsrfToken } from '@/app/lib/utils';
|
||||||
import { classNames } from '@/app/ui/components/button';
|
import { classNames } from '@/app/ui/components/button';
|
||||||
import TableOfContents from '../components/table-of-contents';
|
import clsx from 'clsx';
|
||||||
|
import React from 'react';
|
||||||
import InlineTextField from '../components/form/inlineTextField';
|
import InlineTextField from '../components/form/inlineTextField';
|
||||||
|
import TableOfContents from '../components/table-of-contents';
|
||||||
|
|
||||||
export default function guestsTable() {
|
export default function guestsTable({ guests, updateGuestStatus }: { guests: Guest[], updateGuestStatus: (id: string, status: string) => void }) {
|
||||||
const [guests, setGuests] = useState<Array<Guest>>([]);
|
|
||||||
|
|
||||||
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<HTMLElement>) => handleGuestChange(e, 'invited');
|
const handleInviteGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'invited');
|
||||||
const handleConfirmGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'confirmed');
|
const handleConfirmGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'confirmed');
|
||||||
const handleDeclineGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'declined');
|
const handleDeclineGuest = (e: React.MouseEvent<HTMLElement>) => handleGuestChange(e, 'declined');
|
||||||
@ -48,21 +29,11 @@ export default function guestsTable() {
|
|||||||
.catch((error) => console.error(error));
|
.catch((error) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const handleGuestChange = (e: React.MouseEvent<HTMLElement>, status: string) => {
|
const handleGuestChange = (e: React.MouseEvent<HTMLElement>, status: string) => {
|
||||||
fetch("/api/guests/bulk_update.json",
|
updateGuestStatus(e.currentTarget.getAttribute('data-guest-id') || '', status);
|
||||||
{
|
|
||||||
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 (
|
return (
|
||||||
<TableOfContents
|
<TableOfContents
|
||||||
|
Loading…
x
Reference in New Issue
Block a user