umami/src/app/(main)/reports/[reportId]/ReportHeader.tsx

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import { useContext } from 'react';
2023-09-29 12:29:22 +00:00
import { useRouter } from 'next/navigation';
2023-10-16 03:24:28 +00:00
import { Icon, LoadingButton, InlineEditField, useToasts } from 'react-basics';
import { useMessages, useApi } from 'components/hooks';
import { ReportContext } from './Report';
2023-06-05 10:08:43 +00:00
import styles from './ReportHeader.module.css';
import { REPORT_TYPES } from 'lib/constants';
2023-05-18 06:20:06 +00:00
export function ReportHeader({ icon }) {
const { report, updateReport } = useContext(ReportContext);
2023-05-20 16:02:08 +00:00
const { formatMessage, labels, messages } = useMessages();
2023-06-13 18:59:12 +00:00
const { showToast } = useToasts();
2023-05-20 16:02:08 +00:00
const { post, useMutation } = useApi();
const router = useRouter();
2023-12-04 05:35:20 +00:00
const { mutate: create, isPending: isCreating } = useMutation({
mutationFn: (data: any) => post(`/reports`, data),
});
const { mutate: update, isPending: isUpdating } = useMutation({
mutationFn: (data: any) => post(`/reports/${data.id}`, data),
});
2023-05-18 06:20:06 +00:00
2023-06-05 10:08:43 +00:00
const { name, description, parameters } = report || {};
2023-05-30 23:49:22 +00:00
const { websiteId, dateRange } = parameters || {};
const defaultName = formatMessage(labels.untitled);
2023-05-18 06:20:06 +00:00
2023-05-20 16:02:08 +00:00
const handleSave = async () => {
if (!report.id) {
create(report, {
onSuccess: async ({ id }) => {
showToast({ message: formatMessage(messages.saved), variant: 'success' });
2023-12-03 11:07:03 +00:00
router.push(`/reports/${id}`);
},
});
} else {
update(report, {
onSuccess: async () => {
showToast({ message: formatMessage(messages.saved), variant: 'success' });
},
});
}
};
2023-12-03 11:07:03 +00:00
const handleNameChange = (name: string) => {
updateReport({ name: name || defaultName });
2023-05-20 16:02:08 +00:00
};
2023-05-18 06:20:06 +00:00
2023-12-03 11:07:03 +00:00
const handleDescriptionChange = (description: string) => {
2023-06-05 10:08:43 +00:00
updateReport({ description });
};
2023-10-16 03:24:28 +00:00
if (!report) {
return null;
}
return (
<div className={styles.header}>
<div>
<div className={styles.type}>
{formatMessage(
labels[Object.keys(REPORT_TYPES).find(key => REPORT_TYPES[key] === report?.type)],
)}
</div>
<div className={styles.title}>
<Icon size="lg">{icon}</Icon>
<InlineEditField
key={name}
name="name"
value={name}
placeholder={defaultName}
onCommit={handleNameChange}
/>
</div>
2023-10-16 03:24:28 +00:00
<div className={styles.description}>
<InlineEditField
key={description}
name="description"
value={description}
placeholder={`+ ${formatMessage(labels.addDescription)}`}
onCommit={handleDescriptionChange}
/>
</div>
</div>
2023-10-16 03:24:28 +00:00
<div className={styles.actions}>
2023-06-05 10:08:43 +00:00
<LoadingButton
variant="primary"
2023-08-25 20:32:24 +00:00
isLoading={isCreating || isUpdating}
2023-06-05 10:08:43 +00:00
disabled={!websiteId || !dateRange?.value || !name}
onClick={handleSave}
>
{formatMessage(labels.save)}
</LoadingButton>
</div>
</div>
2023-05-18 06:20:06 +00:00
);
}
2023-05-20 16:02:08 +00:00
export default ReportHeader;