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

123 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-05-20 18:02:08 +02:00
import { useMessages } from 'hooks';
import {
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,
Text,
2023-05-20 18:02:08 +02:00
TextField,
Tooltip,
2023-05-20 18:02:08 +02:00
} from 'react-basics';
import Icons from 'components/icons';
import { updateReport } from 'store/reports';
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 || {};
const queryDisabled = !websiteId || parameters?.urls?.length < 2;
2023-05-20 18:02:08 +02:00
const handleSubmit = values => {
updateReport(id, { parameters: values, isLoading: false, update: Date.now() });
2023-05-20 18:02:08 +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}>
<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>
<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>
<SubmitButton variant="primary" disabled={queryDisabled} loading={isLoading}>
2023-05-20 18:02:08 +02:00
{formatMessage(labels.query)}
</SubmitButton>
</FormButtons>
</Form>
</>
);
}
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-20 18:02:08 +02:00
return (
<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;