2024-11-17 16:10:01 +01:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
|
|
|
'use client';
|
|
|
|
|
2024-11-17 19:18:20 +01:00
|
|
|
import { createGuest, updateGuest } from '@/app/api/guests';
|
|
|
|
import { Group, Guest } from '@/app/lib/definitions';
|
2024-11-17 16:10:01 +01:00
|
|
|
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';
|
|
|
|
|
2024-11-17 19:18:20 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
function resetForm() {
|
|
|
|
setName('');
|
|
|
|
setGroup(null);
|
|
|
|
}
|
2024-11-17 16:10:01 +01:00
|
|
|
|
2024-11-17 16:32:10 +01:00
|
|
|
function submitGuest() {
|
2024-11-17 19:18:20 +01:00
|
|
|
if (!(name && group)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (guest?.id !== undefined) {
|
|
|
|
guest.name = name;
|
|
|
|
guest.groupId = group;
|
|
|
|
updateGuest(guest).then(() => {
|
|
|
|
resetForm();
|
|
|
|
onCreate && onCreate();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
createGuest(name, group, () => {
|
|
|
|
resetForm();
|
|
|
|
onCreate && onCreate();
|
|
|
|
});
|
|
|
|
}
|
2024-11-17 16:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
|
2024-11-17 19:18:20 +01:00
|
|
|
<Dialog header="Add guest" visible={visible} style={{ width: '50vw' }} onHide={onHide}>
|
2024-11-17 16:10:01 +01:00
|
|
|
<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>
|
2024-11-17 19:18:20 +01:00
|
|
|
<button className={classNames('primary')} onClick={submitGuest} disabled={!(name.length > 0 && group)}>
|
|
|
|
{guest?.id !== undefined ? 'Update' : 'Create'}
|
|
|
|
</button>
|
2024-11-17 16:10:01 +01:00
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|