2023-05-20 18:02:08 +02:00
|
|
|
import { useMessages } from 'hooks';
|
|
|
|
import {
|
2023-05-25 06:40:02 +02:00
|
|
|
Button,
|
2023-05-20 18:02:08 +02:00
|
|
|
Icon,
|
|
|
|
Form,
|
|
|
|
FormButtons,
|
|
|
|
FormInput,
|
|
|
|
FormRow,
|
2023-05-25 06:54:49 +02:00
|
|
|
ModalTrigger,
|
|
|
|
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';
|
|
|
|
import { updateReport } from 'store/reports';
|
2023-05-25 06:40:02 +02:00
|
|
|
import { useRef, useState } from 'react';
|
2023-05-20 18:02:08 +02:00
|
|
|
import styles from './FunnelParameters.module.css';
|
|
|
|
|
|
|
|
export function FunnelParameters({ report }) {
|
|
|
|
const { formatMessage, labels } = useMessages();
|
|
|
|
const ref = useRef(null);
|
|
|
|
const { id, websiteId, parameters, isLoading } = 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-25 06:40:02 +02:00
|
|
|
updateReport(id, { parameters: values, isLoading: false, update: Date.now() });
|
2023-05-20 18:02:08 +02:00
|
|
|
};
|
|
|
|
|
2023-05-25 06:40:02 +02:00
|
|
|
const handleAdd = url => {
|
|
|
|
updateReport(id, { parameters: { ...parameters, urls: parameters.urls.concat(url) } });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemove = index => {
|
|
|
|
const urls = [...parameters.urls];
|
|
|
|
urls.splice(index, 1);
|
|
|
|
updateReport(id, { parameters: { ...parameters, urls } });
|
|
|
|
};
|
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-25 06:40:02 +02:00
|
|
|
<FormRow label={formatMessage(labels.urls)} action={<AddURLButton onAdd={handleAdd} />}>
|
|
|
|
<div className={styles.urls}>
|
|
|
|
{parameters?.urls.map((url, index) => {
|
|
|
|
return (
|
|
|
|
<div key={index} className={styles.url}>
|
|
|
|
<Text>{url}</Text>
|
|
|
|
<Icon onClick={() => handleRemove(index)}>
|
|
|
|
<Icons.Close />
|
|
|
|
</Icon>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2023-05-20 18:02:08 +02:00
|
|
|
</FormRow>
|
|
|
|
<FormButtons>
|
2023-05-25 06:40:02 +02:00
|
|
|
<SubmitButton variant="primary" disabled={queryDisabled} loading={isLoading}>
|
2023-05-20 18:02:08 +02:00
|
|
|
{formatMessage(labels.query)}
|
|
|
|
</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-25 06:40:02 +02:00
|
|
|
function AddURLButton({ onAdd }) {
|
|
|
|
const [url, setUrl] = useState('');
|
|
|
|
const { formatMessage, labels } = useMessages();
|
|
|
|
|
|
|
|
const handleAdd = close => {
|
|
|
|
onAdd?.(url);
|
|
|
|
setUrl('');
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = e => {
|
|
|
|
setUrl(e.target.value);
|
|
|
|
};
|
2023-05-25 06:54:49 +02:00
|
|
|
const handleClose = close => {
|
|
|
|
setUrl('');
|
|
|
|
close();
|
|
|
|
};
|
2023-05-25 06:40:02 +02:00
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
return (
|
2023-05-25 19:26:00 +02:00
|
|
|
<Tooltip label={formatMessage(labels.addUrl)}>
|
|
|
|
<ModalTrigger>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Plus />
|
|
|
|
</Icon>
|
|
|
|
<Modal>
|
|
|
|
{close => {
|
|
|
|
return (
|
|
|
|
<Form>
|
|
|
|
<FormRow label={formatMessage(labels.url)}>
|
|
|
|
<TextField name="url" value={url} onChange={handleChange} autoComplete="off" />
|
|
|
|
</FormRow>
|
|
|
|
<FormButtons align="center" flex>
|
|
|
|
<Button variant="primary" onClick={() => handleAdd(close)}>
|
|
|
|
{formatMessage(labels.add)}
|
|
|
|
</Button>
|
|
|
|
<Button onClick={() => handleClose(close)}>{formatMessage(labels.cancel)}</Button>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Modal>
|
|
|
|
</ModalTrigger>
|
|
|
|
</Tooltip>
|
2023-05-20 18:02:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FunnelParameters;
|