2023-05-20 18:02:08 +02:00
|
|
|
import { Flexbox, Icon, LoadingButton, Text, useToast } from 'react-basics';
|
2023-05-18 08:20:06 +02:00
|
|
|
import WebsiteSelect from 'components/input/WebsiteSelect';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
|
|
|
import DateFilter from 'components/input/DateFilter';
|
|
|
|
import { parseDateRange } from 'lib/date';
|
2023-05-20 18:02:08 +02:00
|
|
|
import { updateReport } from 'store/reports';
|
|
|
|
import { useMessages, useApi } from 'hooks';
|
|
|
|
import styles from './reports.module.css';
|
2023-05-18 08:20:06 +02:00
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
export function ReportHeader({ report, icon }) {
|
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
|
|
|
const { toast, showToast } = useToast();
|
|
|
|
const { post, useMutation } = useApi();
|
|
|
|
const { mutate, isLoading } = useMutation(data => post(`/reports`, data));
|
2023-05-18 08:20:06 +02:00
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
const { id, websiteId, name, parameters } = report || {};
|
|
|
|
const { value, startDate, endDate } = parameters?.dateRange || {};
|
|
|
|
|
|
|
|
const handleSelect = websiteId => {
|
|
|
|
updateReport(id, { websiteId });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDateChange = value => {
|
|
|
|
updateReport(id, { parameters: { dateRange: { ...parseDateRange(value) } } });
|
2023-05-18 08:20:06 +02:00
|
|
|
};
|
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
const handleSave = async () => {
|
|
|
|
mutate(report, {
|
|
|
|
onSuccess: async () => {
|
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2023-05-18 08:20:06 +02:00
|
|
|
|
|
|
|
const Title = () => {
|
|
|
|
return (
|
|
|
|
<>
|
2023-05-20 18:02:08 +02:00
|
|
|
<Icon size="lg">{icon}</Icon>
|
|
|
|
<Text>{name}</Text>
|
2023-05-18 08:20:06 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-05-20 18:02:08 +02:00
|
|
|
<PageHeader title={<Title />} className={styles.header}>
|
2023-05-18 08:20:06 +02:00
|
|
|
<Flexbox gap={20}>
|
|
|
|
<DateFilter
|
|
|
|
value={value}
|
|
|
|
startDate={startDate}
|
|
|
|
endDate={endDate}
|
|
|
|
onChange={handleDateChange}
|
|
|
|
showAllTime
|
|
|
|
/>
|
|
|
|
<WebsiteSelect websiteId={websiteId} onSelect={handleSelect} />
|
2023-05-20 18:02:08 +02:00
|
|
|
<LoadingButton
|
|
|
|
variant="primary"
|
|
|
|
loading={isLoading}
|
|
|
|
disabled={!websiteId || !value}
|
|
|
|
onClick={handleSave}
|
|
|
|
>
|
|
|
|
{formatMessage(labels.save)}
|
|
|
|
</LoadingButton>
|
2023-05-18 08:20:06 +02:00
|
|
|
</Flexbox>
|
2023-05-20 18:02:08 +02:00
|
|
|
{toast}
|
2023-05-18 08:20:06 +02:00
|
|
|
</PageHeader>
|
|
|
|
);
|
|
|
|
}
|
2023-05-20 18:02:08 +02:00
|
|
|
|
|
|
|
export default ReportHeader;
|