2023-06-03 08:10:59 +02:00
|
|
|
import { useContext, useRef } from 'react';
|
|
|
|
import { useApi, useMessages } from 'hooks';
|
2023-07-02 07:02:49 +02:00
|
|
|
import { Form, FormRow, FormButtons, SubmitButton, PopupTrigger, Icon, Popup } from 'react-basics';
|
2023-06-03 08:10:59 +02:00
|
|
|
import { ReportContext } from 'components/pages/reports/Report';
|
2023-07-02 07:02:49 +02:00
|
|
|
import Empty from 'components/common/Empty';
|
2023-06-03 08:10:59 +02:00
|
|
|
import { DATA_TYPES } from 'lib/constants';
|
2023-06-15 08:48:11 +02:00
|
|
|
import BaseParameters from '../BaseParameters';
|
2023-07-02 07:02:49 +02:00
|
|
|
import FieldAddForm from './FieldAddForm';
|
|
|
|
import ParameterList from '../ParameterList';
|
|
|
|
import Icons from 'components/icons';
|
|
|
|
import styles from './EventDataParameters.module.css';
|
2023-06-03 08:10:59 +02:00
|
|
|
|
|
|
|
function useFields(websiteId, startDate, endDate) {
|
|
|
|
const { get, useQuery } = useApi();
|
|
|
|
const { data, error, isLoading } = useQuery(
|
|
|
|
['fields', websiteId, startDate, endDate],
|
2023-07-02 07:02:49 +02:00
|
|
|
() =>
|
|
|
|
get('/reports/event-data', {
|
|
|
|
websiteId,
|
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
|
|
|
}),
|
2023-06-03 08:10:59 +02:00
|
|
|
{ enabled: !!(websiteId && startDate && endDate) },
|
|
|
|
);
|
|
|
|
|
|
|
|
return { data, error, isLoading };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function EventDataParameters() {
|
2023-07-02 07:02:49 +02:00
|
|
|
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
|
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2023-06-03 08:10:59 +02:00
|
|
|
const ref = useRef(null);
|
|
|
|
const { parameters } = report || {};
|
2023-07-02 07:02:49 +02:00
|
|
|
const { websiteId, dateRange, fields, filters, groups } = parameters || {};
|
2023-06-03 08:10:59 +02:00
|
|
|
const { startDate, endDate } = dateRange || {};
|
|
|
|
const queryDisabled = !websiteId || !dateRange;
|
2023-07-02 07:02:49 +02:00
|
|
|
const { data, error } = useFields(websiteId, startDate, endDate);
|
|
|
|
const parametersSelected = websiteId && startDate && endDate;
|
|
|
|
const hasData = data?.length !== 0;
|
|
|
|
|
|
|
|
const parameterGroups = [
|
|
|
|
{ label: formatMessage(labels.fields), type: 'fields' },
|
|
|
|
{ label: formatMessage(labels.filters), type: 'filters' },
|
|
|
|
{ label: formatMessage(labels.groupBy), type: 'groups' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const parameterData = {
|
|
|
|
fields,
|
|
|
|
filters,
|
|
|
|
groups,
|
|
|
|
};
|
2023-06-03 08:10:59 +02:00
|
|
|
|
|
|
|
const handleSubmit = values => {
|
|
|
|
runReport(values);
|
|
|
|
};
|
|
|
|
|
2023-07-02 07:02:49 +02:00
|
|
|
const handleAdd = (type, value) => {
|
|
|
|
const data = parameterData[type];
|
|
|
|
updateReport({ parameters: { [type]: data.concat(value) } });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemove = (type, index) => {
|
|
|
|
const data = [...parameterData[type]];
|
|
|
|
data.splice(index, 1);
|
|
|
|
updateReport({ parameters: { [type]: data } });
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddButton = ({ type }) => {
|
|
|
|
return (
|
|
|
|
<PopupTrigger>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Plus />
|
|
|
|
</Icon>
|
|
|
|
<Popup position="bottom" alignment="start">
|
|
|
|
{(close, element) => {
|
|
|
|
return (
|
|
|
|
<FieldAddForm
|
|
|
|
type={type}
|
|
|
|
fields={data.map(({ eventKey, eventDataType }) => ({
|
|
|
|
name: eventKey,
|
|
|
|
type: DATA_TYPES[eventDataType],
|
|
|
|
}))}
|
|
|
|
element={element}
|
|
|
|
onAdd={handleAdd}
|
|
|
|
onClose={close}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Popup>
|
|
|
|
</PopupTrigger>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-03 08:10:59 +02:00
|
|
|
return (
|
|
|
|
<Form ref={ref} values={parameters} error={error} onSubmit={handleSubmit}>
|
2023-06-15 08:48:11 +02:00
|
|
|
<BaseParameters />
|
2023-07-02 07:02:49 +02:00
|
|
|
{!hasData && <Empty message={formatMessage(messages.noEventData)} />}
|
|
|
|
{parametersSelected &&
|
|
|
|
hasData &&
|
|
|
|
parameterGroups.map(({ label, type }) => {
|
|
|
|
return (
|
|
|
|
<FormRow key={label} label={label} action={<AddButton type={type} onAdd={handleAdd} />}>
|
|
|
|
<ParameterList
|
|
|
|
items={parameterData[type]}
|
|
|
|
onRemove={index => handleRemove(type, index)}
|
|
|
|
>
|
|
|
|
{({ name, value }) => {
|
|
|
|
return (
|
|
|
|
<div className={styles.parameter}>
|
|
|
|
{type === 'fields' && (
|
|
|
|
<>
|
|
|
|
<div className={styles.op}>{value}</div>
|
|
|
|
<div>{name}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{type === 'filters' && (
|
|
|
|
<>
|
|
|
|
<div>{name}</div>
|
|
|
|
<div className={styles.op}>{value[0]}</div>
|
|
|
|
<div>{value[1]}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{type === 'groups' && (
|
|
|
|
<>
|
|
|
|
<div>{name}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ParameterList>
|
|
|
|
</FormRow>
|
|
|
|
);
|
|
|
|
})}
|
2023-06-03 08:10:59 +02:00
|
|
|
<FormButtons>
|
|
|
|
<SubmitButton variant="primary" disabled={queryDisabled} loading={isRunning}>
|
|
|
|
{formatMessage(labels.runQuery)}
|
|
|
|
</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EventDataParameters;
|