2023-01-06 04:39:29 +01:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
2022-10-22 06:33:23 +02:00
|
|
|
import classNames from 'classnames';
|
2023-02-15 11:27:18 +01:00
|
|
|
import DateFilter from 'components/input/DateFilter';
|
2022-10-22 06:33:23 +02:00
|
|
|
import DataTable from 'components/metrics/DataTable';
|
|
|
|
import FilterTags from 'components/metrics/FilterTags';
|
|
|
|
import useApi from 'hooks/useApi';
|
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2023-01-06 04:39:29 +01:00
|
|
|
import useTimezone from 'hooks/useTimezone';
|
|
|
|
import { useEffect, useState, useRef } from 'react';
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
Flexbox,
|
|
|
|
Form,
|
|
|
|
FormButtons,
|
|
|
|
FormInput,
|
|
|
|
FormRow,
|
|
|
|
Item,
|
|
|
|
TextField,
|
|
|
|
} from 'react-basics';
|
2022-10-22 06:33:23 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2023-01-10 08:59:26 +01:00
|
|
|
import { FormMessage } from '../layout/FormLayout';
|
2022-10-22 06:33:23 +02:00
|
|
|
import styles from './EventDataForm.module.css';
|
|
|
|
|
|
|
|
export const filterOptions = [
|
|
|
|
{ label: 'Count', value: 'count' },
|
|
|
|
{ label: 'Average', value: 'avg' },
|
|
|
|
{ label: 'Minimum', value: 'min' },
|
2022-10-25 19:27:43 +02:00
|
|
|
{ label: 'Maximum', value: 'max' },
|
2022-10-22 06:33:23 +02:00
|
|
|
{ label: 'Sum', value: 'sum' },
|
|
|
|
];
|
|
|
|
|
|
|
|
export const dateOptions = [
|
|
|
|
{ label: <FormattedMessage id="label.today" defaultMessage="Today" />, value: '1day' },
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<FormattedMessage id="label.last-hours" defaultMessage="Last {x} hours" values={{ x: 24 }} />
|
|
|
|
),
|
|
|
|
value: '24hour',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: <FormattedMessage id="label.yesterday" defaultMessage="Yesterday" />,
|
|
|
|
value: '-1day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: <FormattedMessage id="label.this-week" defaultMessage="This week" />,
|
|
|
|
value: '1week',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 7 }} />
|
|
|
|
),
|
|
|
|
value: '7day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: <FormattedMessage id="label.this-month" defaultMessage="This month" />,
|
|
|
|
value: '1month',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 30 }} />
|
|
|
|
),
|
|
|
|
value: '30day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 90 }} />
|
|
|
|
),
|
|
|
|
value: '90day',
|
|
|
|
},
|
|
|
|
{ label: <FormattedMessage id="label.this-year" defaultMessage="This year" />, value: '1year' },
|
|
|
|
{
|
|
|
|
label: <FormattedMessage id="label.custom-range" defaultMessage="Custom range" />,
|
|
|
|
value: 'custom',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export default function EventDataForm({ websiteId, onClose, className }) {
|
|
|
|
const { post } = useApi();
|
|
|
|
const [message, setMessage] = useState();
|
2023-01-06 04:39:29 +01:00
|
|
|
const { mutate } = useMutation(data => post(`/websites/${websiteId}/eventdata`, data));
|
2022-10-22 07:19:51 +02:00
|
|
|
const [columns, setColumns] = useState({});
|
|
|
|
const [filters, setFilters] = useState({});
|
2023-01-06 04:39:29 +01:00
|
|
|
const [type, setType] = useState('');
|
2022-10-22 06:33:23 +02:00
|
|
|
const [data, setData] = useState([]);
|
|
|
|
const [dateRange, setDateRange] = useDateRange('report');
|
|
|
|
const { startDate, endDate, value } = dateRange;
|
|
|
|
const [timezone] = useTimezone();
|
|
|
|
const [isValid, setIsValid] = useState(false);
|
2023-01-06 04:39:29 +01:00
|
|
|
const columnRef = useRef(null);
|
|
|
|
const filterRef = useRef(null);
|
2022-10-22 06:33:23 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (Object.keys(columns).length > 0) {
|
|
|
|
setIsValid(true);
|
|
|
|
} else {
|
|
|
|
setIsValid(false);
|
|
|
|
}
|
|
|
|
}, [columns]);
|
|
|
|
|
2023-01-06 04:39:29 +01:00
|
|
|
const handleAddTag = (value, list, setState, ref, clearDropdown) => {
|
2022-10-22 06:33:23 +02:00
|
|
|
setState({ ...list, [`${value.field}`]: value.value });
|
2023-01-06 04:39:29 +01:00
|
|
|
|
|
|
|
ref.current.reset({ field: '', value: '' });
|
|
|
|
|
|
|
|
if (clearDropdown) {
|
|
|
|
setType('');
|
|
|
|
}
|
2022-10-22 06:33:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveTag = (value, list, setState) => {
|
2022-10-22 07:19:51 +02:00
|
|
|
const newList = { ...list };
|
2022-10-22 06:33:23 +02:00
|
|
|
|
2022-10-22 07:19:51 +02:00
|
|
|
delete newList[`${value}`];
|
2022-10-22 06:33:23 +02:00
|
|
|
|
2022-10-22 07:19:51 +02:00
|
|
|
setState(newList);
|
2022-10-22 06:33:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
const params = {
|
|
|
|
website_id: websiteId,
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
2022-10-22 06:33:23 +02:00
|
|
|
timezone,
|
|
|
|
columns,
|
|
|
|
filters,
|
|
|
|
};
|
|
|
|
|
2023-01-06 04:39:29 +01:00
|
|
|
mutate(params, {
|
|
|
|
onSuccess: async data => {
|
|
|
|
setData(data);
|
|
|
|
setMessage(null);
|
|
|
|
},
|
|
|
|
onError: async () => {
|
|
|
|
setMessage(
|
|
|
|
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
|
|
|
);
|
|
|
|
setData([]);
|
|
|
|
},
|
|
|
|
});
|
2022-10-22 06:33:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-01-06 04:39:29 +01:00
|
|
|
<Flexbox className={styles.message} alignItems="center" direction="column">
|
|
|
|
<FormMessage>{message}</FormMessage>
|
|
|
|
</Flexbox>
|
2022-10-22 06:33:23 +02:00
|
|
|
<div className={classNames(styles.container, className)}>
|
|
|
|
<div className={styles.form}>
|
2023-01-06 04:39:29 +01:00
|
|
|
<div className={styles.filters}>
|
|
|
|
<FormRow label={<FormattedMessage id="label.date-range" defaultMessage="Date Range" />}>
|
|
|
|
<DateFilter
|
|
|
|
value={value}
|
|
|
|
startDate={startDate}
|
|
|
|
endDate={endDate}
|
|
|
|
onChange={setDateRange}
|
|
|
|
options={dateOptions}
|
|
|
|
/>
|
|
|
|
</FormRow>
|
|
|
|
</div>
|
|
|
|
<div className={styles.filters}>
|
|
|
|
<Form
|
|
|
|
ref={columnRef}
|
|
|
|
onSubmit={value =>
|
|
|
|
handleAddTag({ ...value, value: type }, columns, setColumns, columnRef, true)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<FormRow
|
2023-01-06 07:56:36 +01:00
|
|
|
label={<FormattedMessage id="label.field-name" defaultMessage="Field Name" />}
|
2023-01-06 04:39:29 +01:00
|
|
|
>
|
2023-01-06 07:56:36 +01:00
|
|
|
<FormInput name="field" rules={{ required: 'Required' }}>
|
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
|
|
|
<FormRow label={<FormattedMessage id="label.type" defaultMessage="Type" />}>
|
|
|
|
<FormInput name="value">
|
|
|
|
<Dropdown items={filterOptions} value={type} onChange={setType}>
|
|
|
|
{({ value, label }) => <Item key={value}>{label}</Item>}
|
|
|
|
</Dropdown>
|
|
|
|
</FormInput>
|
2022-10-22 06:33:23 +02:00
|
|
|
</FormRow>
|
2023-01-06 04:39:29 +01:00
|
|
|
<FormButtons className={styles.formButtons}>
|
|
|
|
<Button variant="action" type="submit">
|
|
|
|
<FormattedMessage id="label.add-column" defaultMessage="Add Column" />
|
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
<FilterTags
|
|
|
|
className={styles.filterTag}
|
|
|
|
params={columns}
|
|
|
|
onClick={value => handleRemoveTag(value, columns, setColumns)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.filters}>
|
|
|
|
<Form
|
|
|
|
ref={filterRef}
|
|
|
|
onSubmit={value => handleAddTag(value, filters, setFilters, filterRef)}
|
|
|
|
>
|
2023-01-06 07:56:36 +01:00
|
|
|
<FormRow
|
2023-01-06 04:39:29 +01:00
|
|
|
label={<FormattedMessage id="label.field-name" defaultMessage="Field Name" />}
|
2022-10-22 06:33:23 +02:00
|
|
|
>
|
2023-01-06 07:56:36 +01:00
|
|
|
<FormInput name="field">
|
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
|
|
|
<FormRow label={<FormattedMessage id="label.value" defaultMessage="Value" />}>
|
|
|
|
<FormInput name="value">
|
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
2023-01-06 04:39:29 +01:00
|
|
|
<FormButtons className={styles.formButtons}>
|
|
|
|
<Button variant="action" type="submit">
|
|
|
|
<FormattedMessage id="label.add-filter" defaultMessage="Add Filter" />
|
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
<FilterTags
|
|
|
|
className={styles.filterTag}
|
|
|
|
params={filters}
|
|
|
|
onClick={value => handleRemoveTag(value, filters, setFilters)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-22 06:33:23 +02:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<DataTable className={styles.table} data={data} title="Results" showPercentage={false} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<FormButtons>
|
|
|
|
<Button variant="action" onClick={handleSubmit} disabled={!isValid}>
|
|
|
|
<FormattedMessage id="label.search" defaultMessage="Search" />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage id="label.cancel" defaultMessage="Cancel" />
|
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|