umami/src/app/(main)/reports/[reportId]/FieldAddForm.tsx

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-07-02 07:02:49 +02:00
import { useState } from 'react';
import { createPortal } from 'react-dom';
2023-07-05 07:51:23 +02:00
import { REPORT_PARAMETERS } from 'lib/constants';
import PopupForm from './PopupForm';
import FieldSelectForm from './FieldSelectForm';
2023-06-03 08:10:59 +02:00
2023-12-03 12:07:03 +01:00
export function FieldAddForm({
fields = [],
group,
onAdd,
onClose,
}: {
fields?: any[];
group: string;
onAdd: (group: string, value: string) => void;
onClose: () => void;
}) {
2024-03-27 01:31:16 +01:00
const [selected, setSelected] = useState<{
name: string;
type: string;
value: string;
}>();
2023-06-03 08:10:59 +02:00
2023-12-03 12:07:03 +01:00
const handleSelect = (value: any) => {
2023-07-05 07:51:23 +02:00
const { type } = value;
if (group === REPORT_PARAMETERS.groups || type === 'array' || type === 'boolean') {
value.value = group === REPORT_PARAMETERS.groups ? '' : 'total';
2023-07-02 07:02:49 +02:00
handleSave(value);
return;
}
setSelected(value);
2023-06-03 08:10:59 +02:00
};
2023-12-03 12:07:03 +01:00
const handleSave = (value: any) => {
2023-07-05 07:51:23 +02:00
onAdd(group, value);
2023-06-03 08:10:59 +02:00
onClose();
};
2023-07-02 07:02:49 +02:00
return createPortal(
<PopupForm>
2023-07-05 07:51:23 +02:00
{!selected && <FieldSelectForm fields={fields} onSelect={handleSelect} />}
2023-07-02 07:02:49 +02:00
</PopupForm>,
document.body,
2023-06-03 08:10:59 +02:00
);
}
export default FieldAddForm;