umami/src/app/(main)/reports/funnel/FunnelParameters.tsx

93 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-12-03 12:07:03 +01:00
import { useContext } from 'react';
import { useMessages } from 'components/hooks';
2023-05-20 18:02:08 +02:00
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,
TextField,
} from 'react-basics';
import Icons from 'components/icons';
2023-06-03 08:10:59 +02:00
import UrlAddForm from './UrlAddForm';
2024-02-06 09:38:33 +01:00
import { ReportContext } from '../[reportId]/Report';
2024-01-29 23:47:52 +01:00
import BaseParameters from '../[reportId]/BaseParameters';
import ParameterList from '../[reportId]/ParameterList';
import PopupForm from '../[reportId]/PopupForm';
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();
2023-06-03 08:10:59 +02:00
const { id, parameters } = report || {};
2023-06-03 08:10:59 +02:00
const { websiteId, dateRange, urls } = parameters || {};
const queryDisabled = !websiteId || !dateRange || urls?.length < 2;
2023-05-20 18:02:08 +02:00
2023-12-03 12:07:03 +01:00
const handleSubmit = (data: any, e: any) => {
2023-07-02 07:02:49 +02:00
e.stopPropagation();
e.preventDefault();
2023-06-05 12:08:43 +02:00
if (!queryDisabled) {
runReport(data);
}
2023-05-20 18:02:08 +02:00
};
2023-12-03 12:07:03 +01:00
const handleAddUrl = (url: string) => {
2023-06-03 08:10:59 +02:00
updateReport({ parameters: { urls: parameters.urls.concat(url) } });
};
const handleRemoveUrl = (url: string) => {
const urls = [...parameters.urls];
2024-04-02 18:19:45 +02:00
updateReport({ parameters: { urls: urls.filter(n => n !== url) } });
};
2023-05-20 18:02:08 +02:00
2023-07-02 07:02:49 +02:00
const AddUrlButton = () => {
return (
<PopupTrigger>
<Icon>
<Icons.Plus />
</Icon>
2023-10-17 05:44:22 +02:00
<Popup position="right" alignment="start">
<PopupForm>
<UrlAddForm onAdd={handleAddUrl} />
</PopupForm>
2023-07-02 07:02:49 +02:00
</Popup>
</PopupTrigger>
);
};
2023-05-20 18:02:08 +02:00
return (
2023-12-03 12:07:03 +01:00
<Form values={parameters} onSubmit={handleSubmit} preventSubmit={true}>
<BaseParameters allowWebsiteSelect={!id} />
2023-06-03 08:10:59 +02:00
<FormRow label={formatMessage(labels.window)}>
<FormInput
name="window"
rules={{ required: formatMessage(labels.required), pattern: /[0-9]+/ }}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
2023-07-02 07:02:49 +02:00
<FormRow label={formatMessage(labels.urls)} action={<AddUrlButton />}>
<ParameterList>
{urls.map(url => {
return (
<ParameterList.Item key={url} onRemove={() => handleRemoveUrl(url)}>
{url}
</ParameterList.Item>
);
})}
</ParameterList>
2023-06-03 08:10:59 +02:00
</FormRow>
<FormButtons>
2023-09-05 22:53:58 +02:00
<SubmitButton variant="primary" disabled={queryDisabled} isLoading={isRunning}>
2023-06-03 08:10:59 +02:00
{formatMessage(labels.runQuery)}
</SubmitButton>
</FormButtons>
</Form>
2023-05-20 18:02:08 +02:00
);
}
export default FunnelParameters;