umami/components/pages/reports/event-data/FieldAddForm.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-07-02 07:02:49 +02:00
import { useState } from 'react';
import { createPortal } from 'react-dom';
import PopupForm from '../PopupForm';
import FieldSelectForm from '../FieldSelectForm';
import FieldAggregateForm from '../FieldAggregateForm';
import FieldFilterForm from '../FieldFilterForm';
import styles from './FieldAddForm.module.css';
2023-06-03 08:10:59 +02:00
2023-07-02 07:02:49 +02:00
export function FieldAddForm({ fields = [], type, element, onAdd, onClose }) {
const [selected, setSelected] = useState();
2023-06-03 08:10:59 +02:00
2023-07-02 07:02:49 +02:00
const handleSelect = value => {
if (type === 'groups') {
handleSave(value);
return;
}
setSelected(value);
2023-06-03 08:10:59 +02:00
};
2023-07-02 07:02:49 +02:00
const handleSave = value => {
onAdd(type, value);
2023-06-03 08:10:59 +02:00
onClose();
};
2023-07-02 07:02:49 +02:00
return createPortal(
<PopupForm className={styles.popup} element={element} onClose={onClose}>
{!selected && <FieldSelectForm fields={fields} type={type} onSelect={handleSelect} />}
{selected && type === 'fields' && <FieldAggregateForm {...selected} onSelect={handleSave} />}
{selected && type === 'filters' && <FieldFilterForm {...selected} onSelect={handleSave} />}
</PopupForm>,
document.body,
2023-06-03 08:10:59 +02:00
);
}
export default FieldAddForm;