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

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-12-03 11:07:03 +00:00
import { useContext } from 'react';
import { useMessages } from 'components/hooks';
2023-05-20 16:02:08 +00:00
import {
Icon,
Form,
FormButtons,
FormInput,
FormRow,
2023-06-03 06:10:59 +00:00
PopupTrigger,
Popup,
2023-05-20 16:02:08 +00:00
SubmitButton,
TextField,
} from 'react-basics';
import Icons from 'components/icons';
2023-06-03 06:10:59 +00:00
import UrlAddForm from './UrlAddForm';
2023-10-08 01:55:14 +00:00
import { ReportContext } from '../[id]/Report';
import BaseParameters from '../[id]/BaseParameters';
import ParameterList from '../[id]/ParameterList';
import PopupForm from '../[id]/PopupForm';
2023-05-20 16:02:08 +00:00
export function FunnelParameters() {
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
2023-05-20 16:02:08 +00:00
const { formatMessage, labels } = useMessages();
2023-06-03 06:10:59 +00:00
const { parameters } = report || {};
const { websiteId, dateRange, urls } = parameters || {};
const queryDisabled = !websiteId || !dateRange || urls?.length < 2;
2023-05-20 16:02:08 +00:00
2023-12-03 11:07:03 +00:00
const handleSubmit = (data: any, e: any) => {
2023-07-02 05:02:49 +00:00
e.stopPropagation();
e.preventDefault();
2023-06-05 10:08:43 +00:00
if (!queryDisabled) {
runReport(data);
}
2023-05-20 16:02:08 +00:00
};
2023-12-03 11:07:03 +00:00
const handleAddUrl = (url: string) => {
2023-06-03 06:10:59 +00:00
updateReport({ parameters: { urls: parameters.urls.concat(url) } });
};
2023-12-03 11:07:03 +00:00
const handleRemoveUrl = (index: number, e: any) => {
e.stopPropagation();
const urls = [...parameters.urls];
urls.splice(index, 1);
2023-06-03 06:10:59 +00:00
updateReport({ parameters: { urls } });
};
2023-05-20 16:02:08 +00:00
2023-07-02 05:02:49 +00:00
const AddUrlButton = () => {
return (
<PopupTrigger>
<Icon>
<Icons.Plus />
</Icon>
2023-10-17 03:44:22 +00:00
<Popup position="right" alignment="start">
<PopupForm>
<UrlAddForm onAdd={handleAddUrl} />
</PopupForm>
2023-07-02 05:02:49 +00:00
</Popup>
</PopupTrigger>
);
};
2023-05-20 16:02:08 +00:00
return (
2023-12-03 11:07:03 +00:00
<Form values={parameters} onSubmit={handleSubmit} preventSubmit={true}>
2023-06-03 06:10:59 +00:00
<BaseParameters />
<FormRow label={formatMessage(labels.window)}>
<FormInput
name="window"
rules={{ required: formatMessage(labels.required), pattern: /[0-9]+/ }}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
2023-07-02 05:02:49 +00:00
<FormRow label={formatMessage(labels.urls)} action={<AddUrlButton />}>
2023-12-03 11:07:03 +00:00
<ParameterList
items={urls}
onRemove={(index: number, e: any) => handleRemoveUrl(index, e)}
/>
2023-06-03 06:10:59 +00:00
</FormRow>
<FormButtons>
2023-09-05 20:53:58 +00:00
<SubmitButton variant="primary" disabled={queryDisabled} isLoading={isRunning}>
2023-06-03 06:10:59 +00:00
{formatMessage(labels.runQuery)}
</SubmitButton>
</FormButtons>
</Form>
2023-05-20 16:02:08 +00:00
);
}
export default FunnelParameters;