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

105 lines
3.0 KiB
JavaScript
Raw Normal View History

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,
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 AddUrlForm from './AddUrlForm';
import { ReportContext } from 'components/pages/reports/Report';
2023-05-20 18:02:08 +02:00
import styles from './FunnelParameters.module.css';
export function FunnelParameters() {
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
2023-05-20 18:02:08 +02:00
const { formatMessage, labels } = useMessages();
const [show, setShow] = useState(false);
2023-05-20 18:02:08 +02:00
const ref = useRef(null);
const { websiteId, parameters } = report || {};
const queryDisabled = !websiteId || parameters?.urls?.length < 2;
2023-05-20 18:02:08 +02:00
const handleSubmit = values => {
runReport(values);
2023-05-20 18:02:08 +02:00
};
const handleAddUrl = url => {
updateReport({ parameters: { ...parameters, urls: parameters.urls.concat(url) } });
};
const handleRemoveUrl = (index, e) => {
e.stopPropagation();
const urls = [...parameters.urls];
urls.splice(index, 1);
updateReport({ parameters: { ...parameters, urls } });
};
2023-05-20 18:02:08 +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}>
<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 onClick={showAddForm} />}>
<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>
2023-05-20 18:02:08 +02:00
</FormRow>
<FormButtons>
<SubmitButton variant="primary" disabled={queryDisabled} loading={isRunning}>
{formatMessage(labels.runQuery)}
2023-05-20 18:02:08 +02:00
</SubmitButton>
</FormButtons>
</Form>
{show && (
<Modal onClose={hideAddForm}>
<AddUrlForm onSave={handleAddUrl} onClose={hideAddForm} />
</Modal>
)}
2023-05-20 18:02:08 +02:00
</>
);
}
function AddUrlButton({ onClick }) {
const { formatMessage, labels } = useMessages();
2023-05-20 18:02:08 +02:00
return (
<Tooltip label={formatMessage(labels.addUrl)}>
<Icon onClick={onClick}>
<Icons.Plus />
</Icon>
</Tooltip>
2023-05-20 18:02:08 +02:00
);
}
export default FunnelParameters;