mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-23 18:41:36 +01:00
85 lines
1.8 KiB
JavaScript
85 lines
1.8 KiB
JavaScript
import { produce } from 'immer';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import useApi from './useApi';
|
|
|
|
const baseParameters = {
|
|
name: 'Untitled',
|
|
description: '',
|
|
parameters: {},
|
|
};
|
|
|
|
export function useReport(reportId, defaultParameters) {
|
|
const [report, setReport] = useState(null);
|
|
const [isRunning, setIsRunning] = useState(false);
|
|
const { get, post } = useApi();
|
|
|
|
const loadReport = async id => {
|
|
const data = await get(`/reports/${id}`);
|
|
|
|
const { dateRange } = data?.parameters || {};
|
|
const { startDate, endDate } = dateRange || {};
|
|
|
|
if (startDate && endDate) {
|
|
dateRange.startDate = new Date(startDate);
|
|
dateRange.endDate = new Date(endDate);
|
|
}
|
|
|
|
setReport(data);
|
|
};
|
|
|
|
const runReport = useCallback(
|
|
async parameters => {
|
|
setIsRunning(true);
|
|
|
|
const { type } = report;
|
|
|
|
const data = await post(`/reports/${type}`, parameters);
|
|
|
|
setReport(
|
|
produce(state => {
|
|
state.parameters = parameters;
|
|
state.data = data;
|
|
|
|
return state;
|
|
}),
|
|
);
|
|
|
|
setIsRunning(false);
|
|
},
|
|
[report],
|
|
);
|
|
|
|
const updateReport = useCallback(
|
|
async data => {
|
|
setReport(
|
|
produce(state => {
|
|
const { parameters, ...rest } = data;
|
|
|
|
if (parameters) {
|
|
state.parameters = { ...state.parameters, ...parameters };
|
|
}
|
|
|
|
for (const key in rest) {
|
|
state[key] = rest[key];
|
|
}
|
|
|
|
return state;
|
|
}),
|
|
);
|
|
},
|
|
[report],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!reportId) {
|
|
setReport({ ...baseParameters, ...defaultParameters });
|
|
} else {
|
|
loadReport(reportId);
|
|
}
|
|
}, []);
|
|
|
|
return { report, runReport, updateReport, isRunning };
|
|
}
|
|
|
|
export default useReport;
|