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

106 lines
2.9 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-06-03 08:10:59 +02:00
PopupTrigger,
Popup,
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';
2023-06-03 08:10:59 +02:00
import UrlAddForm from './UrlAddForm';
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
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
const handleSubmit = values => {
runReport(values);
2023-05-20 18:02:08 +02:00
};
const handleAddUrl = url => {
2023-06-03 08:10:59 +02:00
updateReport({ parameters: { urls: parameters.urls.concat(url) } });
};
const handleRemoveUrl = (index, e) => {
e.stopPropagation();
const urls = [...parameters.urls];
urls.splice(index, 1);
2023-06-03 08:10:59 +02:00
updateReport({ parameters: { urls } });
};
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 }) {
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;