2023-05-29 06:37:34 +02:00
|
|
|
import { useContext } from 'react';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { Flexbox, Icon, LoadingButton, InlineEditField, 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 { useMessages, useApi } from 'hooks';
|
2023-05-29 06:37:34 +02:00
|
|
|
import { ReportContext } from './Report';
|
2023-05-20 18:02:08 +02:00
|
|
|
import styles from './reports.module.css';
|
2023-05-18 08:20:06 +02:00
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
export function ReportHeader({ icon }) {
|
|
|
|
const { report, updateReport } = useContext(ReportContext);
|
2023-05-20 18:02:08 +02:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
|
|
|
const { toast, showToast } = useToast();
|
|
|
|
const { post, useMutation } = useApi();
|
2023-05-29 06:37:34 +02:00
|
|
|
const router = useRouter();
|
|
|
|
const { mutate: create, isLoading: isCreating } = useMutation(data => post(`/reports`, data));
|
|
|
|
const { mutate: update, isLoading: isUpdating } = useMutation(data =>
|
|
|
|
post(`/reports/${data.id}`, data),
|
|
|
|
);
|
2023-05-18 08:20:06 +02:00
|
|
|
|
2023-05-31 01:49:22 +02:00
|
|
|
const { name, parameters } = report || {};
|
|
|
|
const { websiteId, dateRange } = parameters || {};
|
2023-05-29 06:37:34 +02:00
|
|
|
const { value, startDate, endDate } = dateRange || {};
|
2023-05-20 18:02:08 +02:00
|
|
|
|
2023-05-31 01:49:22 +02:00
|
|
|
const handleWebsiteSelect = websiteId => {
|
|
|
|
updateReport({ parameters: { websiteId } });
|
2023-05-20 18:02:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleDateChange = value => {
|
2023-05-31 01:49:22 +02:00
|
|
|
updateReport({ parameters: { dateRange: { ...parseDateRange(value) } } });
|
2023-05-18 08:20:06 +02:00
|
|
|
};
|
|
|
|
|
2023-05-20 18:02:08 +02:00
|
|
|
const handleSave = async () => {
|
2023-05-29 06:37:34 +02:00
|
|
|
if (!report.id) {
|
|
|
|
create(report, {
|
|
|
|
onSuccess: async ({ id }) => {
|
|
|
|
router.push(`/reports/${id}`, null, { shallow: true });
|
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
update(report, {
|
|
|
|
onSuccess: async () => {
|
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleNameChange = name => {
|
|
|
|
updateReport({ name });
|
2023-05-20 18:02:08 +02:00
|
|
|
};
|
2023-05-18 08:20:06 +02:00
|
|
|
|
|
|
|
const Title = () => {
|
|
|
|
return (
|
|
|
|
<>
|
2023-05-20 18:02:08 +02:00
|
|
|
<Icon size="lg">{icon}</Icon>
|
2023-05-29 06:37:34 +02:00
|
|
|
<InlineEditField value={name} onCommit={handleNameChange} />
|
2023-05-18 08:20:06 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-05-20 18:02:08 +02:00
|
|
|
<PageHeader title={<Title />} className={styles.header}>
|
2023-06-03 08:10:59 +02:00
|
|
|
<LoadingButton
|
|
|
|
variant="primary"
|
|
|
|
loading={isCreating || isUpdating}
|
|
|
|
disabled={!websiteId || !value}
|
|
|
|
onClick={handleSave}
|
|
|
|
>
|
|
|
|
{formatMessage(labels.save)}
|
|
|
|
</LoadingButton>
|
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;
|