2023-05-29 04:37:34 +00:00
|
|
|
import { useContext } from 'react';
|
2023-10-16 03:24:28 +00:00
|
|
|
import { Icon, LoadingButton, InlineEditField, useToasts } from 'react-basics';
|
2024-02-05 02:03:26 +00:00
|
|
|
import { useMessages, useApi, useNavigation, useTeamUrl } from 'components/hooks';
|
2024-02-06 08:38:33 +00:00
|
|
|
import { ReportContext } from './Report';
|
2023-06-05 10:08:43 +00:00
|
|
|
import styles from './ReportHeader.module.css';
|
2023-10-15 09:08:29 +00:00
|
|
|
import { REPORT_TYPES } from 'lib/constants';
|
2023-05-18 06:20:06 +00:00
|
|
|
|
2023-05-29 04:37:34 +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();
|
2024-02-03 01:49:17 +00:00
|
|
|
const { router } = useNavigation();
|
2024-02-05 02:03:26 +00:00
|
|
|
const { renderTeamUrl } = useTeamUrl();
|
2024-02-03 01:49:17 +00:00
|
|
|
|
2023-05-20 16:02:08 +00:00
|
|
|
const { post, useMutation } = useApi();
|
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 || {};
|
2023-09-01 09:45:59 +00:00
|
|
|
const defaultName = formatMessage(labels.untitled);
|
2023-05-18 06:20:06 +00:00
|
|
|
|
2023-05-20 16:02:08 +00:00
|
|
|
const handleSave = async () => {
|
2023-05-29 04:37:34 +00:00
|
|
|
if (!report.id) {
|
|
|
|
|
create(report, {
|
|
|
|
|
onSuccess: async ({ id }) => {
|
|
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
2024-02-03 01:49:17 +00:00
|
|
|
router.push(renderTeamUrl(`/reports/${id}`));
|
2023-05-29 04:37:34 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
update(report, {
|
|
|
|
|
onSuccess: async () => {
|
|
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
const handleNameChange = (name: string) => {
|
2023-09-01 09:45:59 +00:00
|
|
|
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>
|
2023-10-15 09:08:29 +00:00
|
|
|
<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>
|
2023-10-15 09:08:29 +00:00
|
|
|
</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;
|