wedding-planner-frontend/app/ui/components/group-form-dialog.tsx

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-12-08 12:45:15 +01:00
/* Copyright (C) 2024 Manuel Bustillo*/
'use client';
import { createGroup, updateGroup } from '@/app/api/groups';
import { Group } from '@/app/lib/definitions';
import { classNames } from '@/app/ui/components/button';
import { Dialog } from 'primereact/dialog';
import { ColorPicker } from 'primereact/colorpicker';
import { Dropdown } from 'primereact/dropdown';
import { FloatLabel } from 'primereact/floatlabel';
import { InputText } from 'primereact/inputtext';
import { useState } from 'react';
export default function GroupFormDialog({groups, onCreate, onHide, group, visible }: {
groups: Group[],
onCreate?: () => void,
onHide: () => void,
group?: Group,
visible: boolean,
}) {
const [name, setName] = useState(group?.name || '');
const [icon, setIcon] = useState(group?.icon || '');
const [color, setColor] = useState<string>(group?.color || '');
const [parentId, setParentId] = useState(group?.parentId || '');
function resetForm() {
setName('');
setIcon('');
setColor('');
setParentId('');
}
function submitGroup() {
if (!(name)) {
return
}
if (group?.id !== undefined) {
group.name = name;
group.icon = icon;
group.color = color;
group.parentId = parentId;
updateGroup(group).then(() => {
resetForm();
onCreate && onCreate();
});
} else {
group && createGroup({name, icon, color, parentId}, () => {
resetForm();
onCreate && onCreate();
});
}
}
return (
<>
<Dialog header="Add group" visible={visible} style={{ width: '60vw' }} onHide={onHide}>
<div className="card flex justify-evenly py-5">
<FloatLabel>
<InputText id="name" className='rounded-sm' value={name} onChange={(e) => setName(e.target.value)} />
<label htmlFor="name">Name</label>
</FloatLabel>
<FloatLabel>
<InputText id="icon" className='rounded-sm' value={icon} onChange={(e) => setIcon(e.target.value)} />
<label htmlFor="icon">Icon</label>
</FloatLabel>
<FloatLabel>
<ColorPicker value={color} format='hex' onChange={(e) => setColor(`#${e.value}`)} />
<label htmlFor="color" />
</FloatLabel>
<FloatLabel>
<Dropdown id="parentId" className='rounded-sm min-w-32' value={parentId} onChange={(e) => setParentId(e.target.value)} options={
groups.map((group) => {
return { label: group.name, value: group.id };
})
} />
<label htmlFor="parentId">Parent</label>
</FloatLabel>
<button className={classNames('primary')} onClick={submitGroup} disabled={!(name.length > 0)}>
{group?.id !== undefined ? 'Update' : 'Create'}
</button>
</div>
</Dialog>
</>
);
}