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

55 lines
1.5 KiB
TypeScript
Raw Normal View History

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