Updated loading for reports.

This commit is contained in:
Mike Cao 2023-12-12 20:05:45 -08:00
parent 9735769413
commit e1c65cdf2a
4 changed files with 24 additions and 7 deletions

View File

@ -1,5 +1,6 @@
'use client'; 'use client';
import { createContext, ReactNode } from 'react'; import { createContext, ReactNode } from 'react';
import { Loading } from 'react-basics';
import { useReport } from 'components/hooks'; import { useReport } from 'components/hooks';
import styles from './Report.module.css'; import styles from './Report.module.css';
import classNames from 'classnames'; import classNames from 'classnames';
@ -17,11 +18,11 @@ export function Report({ reportId, defaultParameters, children, className }: Rep
const report = useReport(reportId, defaultParameters); const report = useReport(reportId, defaultParameters);
if (!report) { if (!report) {
return null; return reportId ? <Loading position="page" /> : null;
} }
return ( return (
<ReportContext.Provider value={{ ...report }}> <ReportContext.Provider value={report}>
<div className={classNames(styles.container, className)}>{children}</div> <div className={classNames(styles.container, className)}>{children}</div>
</ReportContext.Provider> </ReportContext.Provider>
); );

View File

@ -1,6 +1,14 @@
import styles from './ReportBody.module.css'; import styles from './ReportBody.module.css';
import { useContext } from 'react';
import { ReportContext } from './Report';
export function ReportBody({ children }) { export function ReportBody({ children }) {
const { report } = useContext(ReportContext);
if (!report) {
return null;
}
return <div className={styles.body}>{children}</div>; return <div className={styles.body}>{children}</div>;
} }

View File

@ -1,6 +1,14 @@
import styles from './ReportMenu.module.css'; import styles from './ReportMenu.module.css';
import { useContext } from 'react';
import { ReportContext } from './Report';
export function ReportMenu({ children }) { export function ReportMenu({ children }) {
const { report } = useContext(ReportContext);
if (!report) {
return null;
}
return <div className={styles.menu}>{children}</div>; return <div className={styles.menu}>{children}</div>;
} }

View File

@ -4,7 +4,7 @@ import { useTimezone } from './useTimezone';
import useApi from './useApi'; import useApi from './useApi';
import useMessages from './useMessages'; import useMessages from './useMessages';
export function useReport(reportId, defaultParameters) { export function useReport(reportId: string, defaultParameters: { [key: string]: any }) {
const [report, setReport] = useState(null); const [report, setReport] = useState(null);
const [isRunning, setIsRunning] = useState(false); const [isRunning, setIsRunning] = useState(false);
const { get, post } = useApi(); const { get, post } = useApi();
@ -17,7 +17,7 @@ export function useReport(reportId, defaultParameters) {
parameters: {}, parameters: {},
}; };
const loadReport = async id => { const loadReport = async (id: string) => {
const data: any = await get(`/reports/${id}`); const data: any = await get(`/reports/${id}`);
const { dateRange } = data?.parameters || {}; const { dateRange } = data?.parameters || {};
@ -32,7 +32,7 @@ export function useReport(reportId, defaultParameters) {
}; };
const runReport = useCallback( const runReport = useCallback(
async parameters => { async (parameters: { [key: string]: any }) => {
setIsRunning(true); setIsRunning(true);
const { type } = report; const { type } = report;
@ -50,11 +50,11 @@ export function useReport(reportId, defaultParameters) {
setIsRunning(false); setIsRunning(false);
}, },
[report], [report, timezone],
); );
const updateReport = useCallback( const updateReport = useCallback(
async data => { async (data: { [x: string]: any; parameters: any }) => {
setReport( setReport(
produce((state: any) => { produce((state: any) => {
const { parameters, ...rest } = data; const { parameters, ...rest } = data;