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

110 lines
3.1 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,
Text,
2023-05-20 18:02:08 +02:00
TextField,
2023-06-16 05:15:31 +02:00
TooltipPopup,
2023-05-20 18:02:08 +02:00
} 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-05-20 18:02:08 +02:00
import styles from './FunnelParameters.module.css';
2023-06-03 08:10:59 +02:00
import BaseParameters from '../BaseParameters';
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-06-05 12:08:43 +02:00
const handleSubmit = data => {
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
return (
2023-06-03 08:10:59 +02:00
<Form ref={ref} values={parameters} onSubmit={handleSubmit}>
<BaseParameters />
<FormRow label={formatMessage(labels.window)}>
<FormInput
name="window"
rules={{ required: formatMessage(labels.required), pattern: /[0-9]+/ }}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormRow label={formatMessage(labels.urls)} action={<AddUrlButton onAdd={handleAddUrl} />}>
<div className={styles.urls}>
{parameters?.urls?.map((url, index) => {
return (
<div key={index} className={styles.url}>
<Text>{url}</Text>
2023-06-16 05:15:31 +02:00
<TooltipPopup
2023-06-03 08:10:59 +02:00
className={styles.icon}
label={formatMessage(labels.remove)}
position="right"
>
<Icon onClick={handleRemoveUrl.bind(null, index)}>
<Icons.Close />
</Icon>
2023-06-16 05:15:31 +02:00
</TooltipPopup>
2023-06-03 08:10:59 +02:00
</div>
);
})}
</div>
</FormRow>
<FormButtons>
<SubmitButton variant="primary" disabled={queryDisabled} loading={isRunning}>
{formatMessage(labels.runQuery)}
</SubmitButton>
</FormButtons>
</Form>
2023-05-20 18:02:08 +02:00
);
}
2023-06-03 08:10:59 +02:00
function AddUrlButton({ onAdd }) {
const { formatMessage, labels } = useMessages();
2023-05-20 18:02:08 +02:00
return (
2023-06-03 08:10:59 +02:00
<PopupTrigger>
2023-06-16 05:15:31 +02:00
<TooltipPopup label={formatMessage(labels.addUrl)}>
2023-06-03 08:10:59 +02:00
<Icon>
<Icons.Plus />
</Icon>
2023-06-16 05:15:31 +02:00
</TooltipPopup>
2023-06-03 08:10:59 +02:00
<Popup position="bottom" alignment="start">
2023-06-23 10:19:08 +02:00
{(close, element) => {
const { right, bottom } = element.getBoundingClientRect();
return <UrlAddForm onSave={onAdd} onClose={close} style={{ left: right, top: bottom }} />;
2023-06-03 08:10:59 +02:00
}}
</Popup>
</PopupTrigger>
2023-05-20 18:02:08 +02:00
);
}
export default FunnelParameters;