/* Copyright (C) 2024 Manuel Bustillo*/ 'use client'; import { AbstractApi } from '@/app/api/abstract-api'; import { Group, GroupSerializer } from '@/app/lib/group'; import { classNames } from '@/app/ui/components/button'; import { ColorPicker } from 'primereact/colorpicker'; 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 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(group?.color || ''); const [parentId, setParentId] = useState(group?.parentId || ''); const api = new AbstractApi(); const serializer = new GroupSerializer(); 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; api.update(serializer, group, () => { resetForm(); onCreate && onCreate(); }); } else { api.create(serializer, new Group(undefined, name, undefined, icon, undefined, parentId, color), () => { resetForm(); onCreate && onCreate(); }); } } return ( <>
setName(e.target.value)} /> setIcon(e.target.value)} /> setColor(`#${e.value}`)} /> setParentId(e.target.value)} options={ groups.map((group) => { return { label: group.name, value: group.id }; }) } />
); }