/* Copyright (C) 2024 Manuel Bustillo*/ 'use client'; import { createGuest, updateGuest } from '@/app/api/guests'; import { Group, Guest, GuestStatus, guestStatuses } from '@/app/lib/definitions'; import { capitalize } 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 GuestFormDialog({ groups, onCreate, onHide, guest, visible }: { groups: Group[], onCreate?: () => void, onHide: () => void, guest?: Guest, visible: boolean, }) { const [name, setName] = useState(guest?.name || ''); const [group, setGroup] = useState(guest?.groupId || null); const [status, setStatus] = useState(guest?.status || null); function resetForm() { setName(''); setGroup(null); setStatus(null); } function submitGuest() { if (!(name && group && status)) { return } if (guest?.id !== undefined) { guest.name = name; guest.groupId = group; guest.status = status; updateGuest(guest).then(() => { resetForm(); onCreate && onCreate(); }); } else { guest && createGuest({name: name, groupId: group, status: status}, () => { resetForm(); onCreate && onCreate(); }); } } return ( <>
setName(e.target.value)} /> setGroup(e.target.value)} options={ groups.map((group) => { return { label: group.name, value: group.id }; }) } /> setStatus(e.target.value)} options={ guestStatuses.map((status) => { return { label: capitalize(status), value: status }; }) } />
); }