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,
|
2023-05-25 06:40:02 +02:00
|
|
|
Text,
|
2023-05-20 18:02:08 +02:00
|
|
|
TextField,
|
2023-05-25 19:26:00 +02:00
|
|
|
Tooltip,
|
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';
|
2023-05-29 06:37:34 +02:00
|
|
|
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
|
|
|
|
2023-05-29 06:37:34 +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
|
|
|
};
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
const handleAddUrl = url => {
|
2023-06-03 08:10:59 +02:00
|
|
|
updateReport({ parameters: { urls: parameters.urls.concat(url) } });
|
2023-05-25 06:40:02 +02:00
|
|
|
};
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
const handleRemoveUrl = (index, e) => {
|
|
|
|
e.stopPropagation();
|
2023-05-25 06:40:02 +02:00
|
|
|
const urls = [...parameters.urls];
|
|
|
|
urls.splice(index, 1);
|
2023-06-03 08:10:59 +02:00
|
|
|
updateReport({ parameters: { urls } });
|
2023-05-25 06:40:02 +02:00
|
|
|
};
|
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>
|
|
|
|
<Tooltip
|
|
|
|
className={styles.icon}
|
|
|
|
label={formatMessage(labels.remove)}
|
|
|
|
position="right"
|
|
|
|
>
|
|
|
|
<Icon onClick={handleRemoveUrl.bind(null, index)}>
|
|
|
|
<Icons.Close />
|
|
|
|
</Icon>
|
|
|
|
</Tooltip>
|
|
|
|
</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 }) {
|
2023-05-25 06:40:02 +02:00
|
|
|
const { formatMessage, labels } = useMessages();
|
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
return (
|
2023-06-03 08:10:59 +02:00
|
|
|
<PopupTrigger>
|
|
|
|
<Tooltip label={formatMessage(labels.addUrl)}>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Plus />
|
|
|
|
</Icon>
|
|
|
|
</Tooltip>
|
|
|
|
<Popup position="bottom" alignment="start">
|
|
|
|
{close => {
|
|
|
|
return <UrlAddForm onSave={onAdd} onClose={close} />;
|
|
|
|
}}
|
|
|
|
</Popup>
|
|
|
|
</PopupTrigger>
|
2023-05-20 18:02:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FunnelParameters;
|