2023-07-08 05:38:43 +02:00
|
|
|
import { useContext, useRef } from 'react';
|
2023-07-18 18:09:22 +02:00
|
|
|
import { useMessages } from 'hooks';
|
2023-07-08 05:38:43 +02:00
|
|
|
import { Form, FormRow, FormButtons, SubmitButton, PopupTrigger, Icon, Popup } from 'react-basics';
|
|
|
|
import { ReportContext } from 'components/pages/reports/Report';
|
2023-07-18 18:09:22 +02:00
|
|
|
import { REPORT_PARAMETERS, WEBSITE_EVENT_FIELDS } from 'lib/constants';
|
2023-07-08 05:38:43 +02:00
|
|
|
import Icons from 'components/icons';
|
|
|
|
import BaseParameters from '../BaseParameters';
|
2023-07-18 18:09:22 +02:00
|
|
|
import FieldAddForm from '../FieldAddForm';
|
2023-07-08 05:38:43 +02:00
|
|
|
import ParameterList from '../ParameterList';
|
|
|
|
import styles from './InsightsParameters.module.css';
|
|
|
|
|
|
|
|
export function InsightsParameters() {
|
|
|
|
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
|
2023-07-18 18:09:22 +02:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-07-08 05:38:43 +02:00
|
|
|
const ref = useRef(null);
|
|
|
|
const { parameters } = report || {};
|
|
|
|
const { websiteId, dateRange, fields, filters, groups } = parameters || {};
|
|
|
|
const queryEnabled = websiteId && dateRange && fields?.length;
|
2023-07-18 18:09:22 +02:00
|
|
|
const fieldOptions = Object.keys(WEBSITE_EVENT_FIELDS).map(key => WEBSITE_EVENT_FIELDS[key]);
|
2023-07-08 05:38:43 +02:00
|
|
|
|
|
|
|
const parameterGroups = [
|
|
|
|
{ label: formatMessage(labels.fields), group: REPORT_PARAMETERS.fields },
|
|
|
|
{ label: formatMessage(labels.filters), group: REPORT_PARAMETERS.filters },
|
|
|
|
{ label: formatMessage(labels.breakdown), group: REPORT_PARAMETERS.groups },
|
|
|
|
];
|
|
|
|
|
|
|
|
const parameterData = {
|
|
|
|
fields,
|
|
|
|
filters,
|
|
|
|
groups,
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmit = values => {
|
|
|
|
runReport(values);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleAdd = (group, value) => {
|
|
|
|
const data = parameterData[group];
|
|
|
|
|
|
|
|
if (!data.find(({ name }) => name === value.name)) {
|
|
|
|
updateReport({ parameters: { [group]: data.concat(value) } });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemove = (group, index) => {
|
|
|
|
const data = [...parameterData[group]];
|
|
|
|
data.splice(index, 1);
|
|
|
|
updateReport({ parameters: { [group]: data } });
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddButton = ({ group }) => {
|
|
|
|
return (
|
|
|
|
<PopupTrigger>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Plus />
|
|
|
|
</Icon>
|
|
|
|
<Popup position="bottom" alignment="start">
|
|
|
|
{(close, element) => {
|
|
|
|
return (
|
|
|
|
<FieldAddForm
|
2023-07-18 18:09:22 +02:00
|
|
|
fields={fieldOptions}
|
2023-07-08 05:38:43 +02:00
|
|
|
group={group}
|
|
|
|
element={element}
|
|
|
|
onAdd={handleAdd}
|
|
|
|
onClose={close}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Popup>
|
|
|
|
</PopupTrigger>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-07-18 18:09:22 +02:00
|
|
|
<Form ref={ref} values={parameters} onSubmit={handleSubmit}>
|
2023-07-08 05:38:43 +02:00
|
|
|
<BaseParameters />
|
2023-07-18 18:09:22 +02:00
|
|
|
{parameterGroups.map(({ label, group }) => {
|
|
|
|
return (
|
|
|
|
<FormRow key={label} label={label} action={<AddButton group={group} onAdd={handleAdd} />}>
|
|
|
|
<ParameterList
|
|
|
|
items={parameterData[group]}
|
|
|
|
onRemove={index => handleRemove(group, index)}
|
2023-07-08 05:38:43 +02:00
|
|
|
>
|
2023-07-18 18:09:22 +02:00
|
|
|
{({ name, value }) => {
|
|
|
|
return (
|
|
|
|
<div className={styles.parameter}>
|
|
|
|
{group === REPORT_PARAMETERS.fields && (
|
|
|
|
<>
|
|
|
|
<div>{name}</div>
|
|
|
|
<div className={styles.op}>{value}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{group === REPORT_PARAMETERS.filters && (
|
|
|
|
<>
|
|
|
|
<div>{name}</div>
|
|
|
|
<div className={styles.op}>{value[0]}</div>
|
|
|
|
<div>{value[1]}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{group === REPORT_PARAMETERS.groups && (
|
|
|
|
<>
|
|
|
|
<div>{name}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ParameterList>
|
|
|
|
</FormRow>
|
|
|
|
);
|
|
|
|
})}
|
2023-07-08 05:38:43 +02:00
|
|
|
<FormButtons>
|
|
|
|
<SubmitButton variant="primary" disabled={!queryEnabled} loading={isRunning}>
|
|
|
|
{formatMessage(labels.runQuery)}
|
|
|
|
</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InsightsParameters;
|