Skip to content

Commit 0960229

Browse files
committed
Lazy load chart
1 parent e89e422 commit 0960229

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

examples/demo/src/dashboard/Dashboard.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import React, { useMemo, CSSProperties } from 'react';
2-
import { useGetList } from 'react-admin';
3-
import { useMediaQuery, Theme } from '@mui/material';
1+
import React, { useMemo, CSSProperties, Suspense } from 'react';
2+
import { Translate, useGetList } from 'react-admin';
3+
import {
4+
useMediaQuery,
5+
Theme,
6+
Skeleton,
7+
Card,
8+
CardHeader,
9+
CardContent,
10+
} from '@mui/material';
411
import { subDays, startOfDay } from 'date-fns';
512

613
import Welcome from './Welcome';
@@ -9,7 +16,6 @@ import NbNewOrders from './NbNewOrders';
916
import PendingOrders from './PendingOrders';
1017
import PendingReviews from './PendingReviews';
1118
import NewCustomers from './NewCustomers';
12-
import OrderChart from './OrderChart';
1319

1420
import { Order } from '../types';
1521

@@ -37,6 +43,8 @@ const styles = {
3743
const Spacer = () => <span style={{ width: '1em' }} />;
3844
const VerticalSpacer = () => <span style={{ height: '1em' }} />;
3945

46+
const OrderChart = React.lazy(() => import('./OrderChart'));
47+
4048
const Dashboard = () => {
4149
const isXSmall = useMediaQuery((theme: Theme) =>
4250
theme.breakpoints.down('sm')
@@ -109,7 +117,18 @@ const Dashboard = () => {
109117
<NbNewOrders value={nbNewOrders} />
110118
</div>
111119
<div style={styles.singleCol}>
112-
<OrderChart orders={recentOrders} />
120+
<Card>
121+
<CardHeader
122+
title={
123+
<Translate i18nKey="pos.dashboard.month_history" />
124+
}
125+
/>
126+
<CardContent>
127+
<Suspense fallback={<Skeleton height={300} />}>
128+
<OrderChart orders={recentOrders} />
129+
</Suspense>
130+
</CardContent>
131+
</Card>
113132
</div>
114133
<div style={styles.singleCol}>
115134
<PendingOrders orders={pendingOrders} />
@@ -126,7 +145,18 @@ const Dashboard = () => {
126145
<NbNewOrders value={nbNewOrders} />
127146
</div>
128147
<div style={styles.singleCol}>
129-
<OrderChart orders={recentOrders} />
148+
<Card>
149+
<CardHeader
150+
title={
151+
<Translate i18nKey="pos.dashboard.month_history" />
152+
}
153+
/>
154+
<CardContent>
155+
<Suspense fallback={<Skeleton height={300} />}>
156+
<OrderChart orders={recentOrders} />
157+
</Suspense>
158+
</CardContent>
159+
</Card>
130160
</div>
131161
<div style={styles.singleCol}>
132162
<PendingOrders orders={pendingOrders} />

examples/demo/src/dashboard/OrderChart.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import { Card, CardHeader, CardContent } from '@mui/material';
32
import * as echarts from 'echarts/core';
43
// Import bar charts, all suffixed with Chart
54
import { LineChart } from 'echarts/charts';
@@ -12,7 +11,6 @@ import {
1211
DatasetComponent,
1312
TransformComponent,
1413
} from 'echarts/components';
15-
import { useTranslate } from 'react-admin';
1614
// Features like Universal Transition and Label Layout
1715
import { LabelLayout, UniversalTransition } from 'echarts/features';
1816

@@ -84,7 +82,6 @@ const getRevenuePerDay = (orders: Order[]): TotalByDay[] => {
8482

8583
const OrderChart = (props: { orders?: Order[] }) => {
8684
const { orders } = props;
87-
const translate = useTranslate();
8885
const chartRef = React.useRef<HTMLDivElement>(null);
8986
const chartInstance = React.useRef<echarts.ECharts | null>(null);
9087

@@ -201,14 +198,7 @@ const OrderChart = (props: { orders?: Order[] }) => {
201198
};
202199
}, [orders]);
203200

204-
return (
205-
<Card>
206-
<CardHeader title={translate('pos.dashboard.month_history')} />
207-
<CardContent>
208-
<div ref={chartRef} style={{ width: '100%', height: 300 }} />
209-
</CardContent>
210-
</Card>
211-
);
201+
return <div ref={chartRef} style={{ width: '100%', height: 300 }} />;
212202
};
213203

214204
interface TotalByDay {

0 commit comments

Comments
 (0)