import classNames from 'classnames';
import Button from 'components/common/Button';
import DateFilter from 'components/common/DateFilter';
import DropDown from 'components/common/DropDown';
import FormLayout, {
FormButtons,
FormError,
FormMessage,
FormRow,
} from 'components/layout/FormLayout';
import DataTable from 'components/metrics/DataTable';
import FilterTags from 'components/metrics/FilterTags';
import { Field, Form, Formik } from 'formik';
import useApi from 'hooks/useApi';
import useDateRange from 'hooks/useDateRange';
import { useState, useEffect } from 'react';
import { FormattedMessage } from 'react-intl';
import styles from './EventDataForm.module.css';
import useTimezone from 'hooks/useTimezone';
export const filterOptions = [
{ label: 'Count', value: 'count' },
{ label: 'Average', value: 'avg' },
{ label: 'Minimum', value: 'min' },
{ label: 'Maxmimum', value: 'max' },
{ label: 'Sum', value: 'sum' },
];
export const dateOptions = [
{ label: , value: '1day' },
{
label: (
),
value: '24hour',
},
{
label: ,
value: '-1day',
},
{
label: ,
value: '1week',
divider: true,
},
{
label: (
),
value: '7day',
},
{
label: ,
value: '1month',
divider: true,
},
{
label: (
),
value: '30day',
},
{
label: (
),
value: '90day',
},
{ label: , value: '1year' },
{
label: ,
value: 'custom',
divider: true,
},
];
export default function EventDataForm({ websiteId, onClose, className }) {
const { post } = useApi();
const [message, setMessage] = useState();
const [columns, setColumns] = useState({});
const [filters, setFilters] = useState({});
const [data, setData] = useState([]);
const [dateRange, setDateRange] = useDateRange('report');
const { startDate, endDate, value } = dateRange;
const [timezone] = useTimezone();
const [isValid, setIsValid] = useState(false);
useEffect(() => {
if (Object.keys(columns).length > 0) {
setIsValid(true);
} else {
setIsValid(false);
}
}, [columns]);
const handleAddTag = (value, list, setState, resetForm) => {
setState({ ...list, [`${value.field}`]: value.value });
resetForm();
};
const handleRemoveTag = (value, list, setState) => {
const newList = { ...list };
delete newList[`${value}`];
setState(newList);
};
const handleSubmit = async () => {
const params = {
website_id: websiteId,
start_at: +startDate,
end_at: +endDate,
timezone,
columns,
filters,
};
const { ok, data } = await post(`/websites/${websiteId}/eventdata`, params);
if (!ok) {
setMessage();
setData([]);
} else {
setData(data);
setMessage(null);
}
};
return (
<>
{message}
handleAddTag(value, columns, setColumns, resetForm)
}
>
{({ values, setFieldValue }) => (
)}
handleRemoveTag(value, columns, setColumns)}
/>
handleAddTag(value, filters, setFilters, resetForm)
}
>
{({ values }) => (
)}
handleRemoveTag(value, filters, setFilters)}
/>
>
);
}