umami/components/pages/reports/funnel/FunnelParameters.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-06-15 08:48:11 +02:00
import { useContext, useRef } from 'react';
2023-05-20 18:02:08 +02:00
import { useMessages } from 'hooks';
import {
Icon,
Form,
FormButtons,
FormInput,
FormRow,
2023-06-03 08:10:59 +02:00
PopupTrigger,
Popup,
2023-05-20 18:02:08 +02:00
SubmitButton,
TextField,
} from 'react-basics';
import Icons from 'components/icons';
2023-06-03 08:10:59 +02:00
import UrlAddForm from './UrlAddForm';
import { ReportContext } from 'components/pages/reports/Report';
2023-06-03 08:10:59 +02:00
import BaseParameters from '../BaseParameters';
2023-07-02 07:02:49 +02:00
import ParameterList from '../ParameterList';
2023-05-20 18:02:08 +02:00
export function FunnelParameters() {
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
2023-05-20 18:02:08 +02:00
const { formatMessage, labels } = useMessages();
const ref = useRef(null);
2023-06-03 08:10:59 +02:00
const { parameters } = report || {};
const { websiteId, dateRange, urls } = parameters || {};
const queryDisabled = !websiteId || !dateRange || urls?.length < 2;
2023-05-20 18:02:08 +02:00
2023-07-02 07:02:49 +02:00
const handleSubmit = (data, e) => {
e.stopPropagation();
e.preventDefault();
2023-06-05 12:08:43 +02:00
if (!queryDisabled) {
runReport(data);
}
2023-05-20 18:02:08 +02:00
};
const handleAddUrl = url => {
2023-06-03 08:10:59 +02:00
updateReport({ parameters: { urls: parameters.urls.concat(url) } });
};
const handleRemoveUrl = (index, e) => {
e.stopPropagation();
const urls = [...parameters.urls];
urls.splice(index, 1);
2023-06-03 08:10:59 +02:00
updateReport({ parameters: { urls } });
};
2023-05-20 18:02:08 +02:00
2023-07-02 07:02:49 +02:00
const AddUrlButton = () => {
return (
<PopupTrigger>
<Icon>
<Icons.Plus />
</Icon>
<Popup position="bottom" alignment="start">
{(close, element) => {
return <UrlAddForm element={element} onAdd={handleAddUrl} onClose={close} />;
}}
</Popup>
</PopupTrigger>
);
};
2023-05-20 18:02:08 +02:00
return (
2023-07-02 07:02:49 +02:00
<Form ref={ref} values={parameters} onSubmit={handleSubmit} preventSubmit={true}>
2023-06-03 08:10:59 +02:00
<BaseParameters />
<FormRow label={formatMessage(labels.window)}>
<FormInput
name="window"
rules={{ required: formatMessage(labels.required), pattern: /[0-9]+/ }}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
2023-07-02 07:02:49 +02:00
<FormRow label={formatMessage(labels.urls)} action={<AddUrlButton />}>
<ParameterList items={urls} onRemove={handleRemoveUrl} />
2023-06-03 08:10:59 +02:00
</FormRow>
<FormButtons>
<SubmitButton variant="primary" disabled={queryDisabled} loading={isRunning}>
{formatMessage(labels.runQuery)}
</SubmitButton>
</FormButtons>
</Form>
2023-05-20 18:02:08 +02:00
);
}
export default FunnelParameters;