2023-05-29 06:37:34 +02:00
|
|
|
import { useContext, useRef, useState } from 'react';
|
2023-05-20 18:02:08 +02:00
|
|
|
import { useMessages } from 'hooks';
|
|
|
|
import {
|
|
|
|
Icon,
|
|
|
|
Form,
|
|
|
|
FormButtons,
|
|
|
|
FormInput,
|
|
|
|
FormRow,
|
2023-05-25 06:54:49 +02:00
|
|
|
Modal,
|
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-05-29 06:37:34 +02:00
|
|
|
import AddUrlForm from './AddUrlForm';
|
|
|
|
import { ReportContext } from 'components/pages/reports/Report';
|
2023-05-20 18:02:08 +02:00
|
|
|
import styles from './FunnelParameters.module.css';
|
|
|
|
|
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();
|
2023-05-29 06:37:34 +02:00
|
|
|
const [show, setShow] = useState(false);
|
2023-05-20 18:02:08 +02:00
|
|
|
const ref = useRef(null);
|
2023-05-29 06:37:34 +02:00
|
|
|
const { websiteId, parameters } = report || {};
|
2023-05-25 06:40:02 +02:00
|
|
|
const queryDisabled = !websiteId || parameters?.urls?.length < 2;
|
2023-05-20 18:02:08 +02:00
|
|
|
|
|
|
|
const handleSubmit = values => {
|
2023-05-29 06:37:34 +02:00
|
|
|
runReport(values);
|
2023-05-20 18:02:08 +02:00
|
|
|
};
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
const handleAddUrl = url => {
|
|
|
|
updateReport({ parameters: { ...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-05-29 06:37:34 +02:00
|
|
|
updateReport({ parameters: { ...parameters, urls } });
|
2023-05-25 06:40:02 +02:00
|
|
|
};
|
2023-05-20 18:02:08 +02:00
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
const showAddForm = () => setShow(true);
|
|
|
|
const hideAddForm = () => setShow(false);
|
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Form ref={ref} values={parameters} onSubmit={handleSubmit}>
|
2023-05-25 06:40:02 +02:00
|
|
|
<FormRow label={formatMessage(labels.window)}>
|
2023-05-20 18:02:08 +02:00
|
|
|
<FormInput
|
|
|
|
name="window"
|
|
|
|
rules={{ required: formatMessage(labels.required), pattern: /[0-9]+/ }}
|
|
|
|
>
|
|
|
|
<TextField autoComplete="off" />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
2023-05-29 06:37:34 +02:00
|
|
|
<FormRow label={formatMessage(labels.urls)} action={<AddUrlButton onClick={showAddForm} />}>
|
2023-05-25 06:40:02 +02:00
|
|
|
<div className={styles.urls}>
|
2023-05-29 06:37:34 +02:00
|
|
|
{parameters?.urls?.map((url, index) => {
|
2023-05-25 06:40:02 +02:00
|
|
|
return (
|
|
|
|
<div key={index} className={styles.url}>
|
|
|
|
<Text>{url}</Text>
|
2023-05-29 06:37:34 +02:00
|
|
|
<Tooltip
|
|
|
|
className={styles.icon}
|
|
|
|
label={formatMessage(labels.remove)}
|
|
|
|
position="right"
|
|
|
|
>
|
|
|
|
<Icon onClick={handleRemoveUrl.bind(null, index)}>
|
|
|
|
<Icons.Close />
|
|
|
|
</Icon>
|
|
|
|
</Tooltip>
|
2023-05-25 06:40:02 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2023-05-20 18:02:08 +02:00
|
|
|
</FormRow>
|
|
|
|
<FormButtons>
|
2023-05-29 06:37:34 +02:00
|
|
|
<SubmitButton variant="primary" disabled={queryDisabled} loading={isRunning}>
|
|
|
|
{formatMessage(labels.runQuery)}
|
2023-05-20 18:02:08 +02:00
|
|
|
</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
2023-05-29 06:37:34 +02:00
|
|
|
{show && (
|
|
|
|
<Modal onClose={hideAddForm}>
|
|
|
|
<AddUrlForm onSave={handleAddUrl} onClose={hideAddForm} />
|
|
|
|
</Modal>
|
|
|
|
)}
|
2023-05-20 18:02:08 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
function AddUrlButton({ onClick }) {
|
2023-05-25 06:40:02 +02:00
|
|
|
const { formatMessage, labels } = useMessages();
|
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
return (
|
2023-05-25 19:26:00 +02:00
|
|
|
<Tooltip label={formatMessage(labels.addUrl)}>
|
2023-05-29 06:37:34 +02:00
|
|
|
<Icon onClick={onClick}>
|
|
|
|
<Icons.Plus />
|
|
|
|
</Icon>
|
2023-05-25 19:26:00 +02:00
|
|
|
</Tooltip>
|
2023-05-20 18:02:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FunnelParameters;
|