Skip to content

Commit 8eb5d3a

Browse files
authored
Merge pull request #10829 from marmelab/improve-demo-bundle-size
Improve commerce demo bundle
2 parents 5cd8c5a + 0960229 commit 8eb5d3a

File tree

4 files changed

+91
-23
lines changed

4 files changed

+91
-23
lines changed

examples/crm/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default defineConfig(async () => {
3636
}),
3737
],
3838
define: {
39-
'process.env': process.env,
39+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
4040
},
4141
server: {
4242
port: 8000,

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: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
11
import * as React from 'react';
2-
import { Card, CardHeader, CardContent } from '@mui/material';
3-
import * as echarts from 'echarts';
4-
import { useTranslate } from 'react-admin';
2+
import * as echarts from 'echarts/core';
3+
// Import bar charts, all suffixed with Chart
4+
import { LineChart } from 'echarts/charts';
5+
6+
// Import the title, tooltip, rectangular coordinate system, dataset and transform components
7+
import {
8+
TitleComponent,
9+
TooltipComponent,
10+
GridComponent,
11+
DatasetComponent,
12+
TransformComponent,
13+
} from 'echarts/components';
14+
// Features like Universal Transition and Label Layout
15+
import { LabelLayout, UniversalTransition } from 'echarts/features';
16+
17+
// Import the Canvas renderer
18+
// Note that including the CanvasRenderer or SVGRenderer is a required step
19+
import { SVGRenderer } from 'echarts/renderers';
20+
521
import { format, subDays, addDays } from 'date-fns';
622

723
import { Order } from '../types';
24+
import type {
25+
DatasetComponentOption,
26+
GridComponentOption,
27+
LineSeriesOption,
28+
TitleComponentOption,
29+
TooltipComponentOption,
30+
} from 'echarts';
831

932
const lastDay = new Date();
1033
const lastMonthDays = Array.from({ length: 30 }, (_, i) => subDays(lastDay, i));
1134
const aMonthAgo = subDays(new Date(), 30);
1235

36+
// Create an Option type with only the required components and charts via ComposeOption
37+
type ECOption = echarts.ComposeOption<
38+
| LineSeriesOption
39+
| TitleComponentOption
40+
| TooltipComponentOption
41+
| GridComponentOption
42+
| DatasetComponentOption
43+
>;
44+
45+
echarts.use([
46+
TitleComponent,
47+
TooltipComponent,
48+
GridComponent,
49+
DatasetComponent,
50+
TransformComponent,
51+
LineChart,
52+
LabelLayout,
53+
UniversalTransition,
54+
SVGRenderer,
55+
]);
56+
1357
const dateFormatter = (date: number): string =>
1458
new Date(date).toLocaleDateString();
1559

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

3983
const OrderChart = (props: { orders?: Order[] }) => {
4084
const { orders } = props;
41-
const translate = useTranslate();
4285
const chartRef = React.useRef<HTMLDivElement>(null);
4386
const chartInstance = React.useRef<echarts.ECharts | null>(null);
4487

@@ -53,7 +96,7 @@ const OrderChart = (props: { orders?: Order[] }) => {
5396
const revenueData = getRevenuePerDay(orders);
5497

5598
// Configure the chart
56-
const option = {
99+
const option: ECOption = {
57100
xAxis: {
58101
type: 'time',
59102
min: addDays(aMonthAgo, 1).getTime(),
@@ -90,8 +133,7 @@ const OrderChart = (props: { orders?: Order[] }) => {
90133
axisPointer: {
91134
type: 'line',
92135
lineStyle: {
93-
type: 'dashed',
94-
dashArray: [3, 3],
136+
type: [3, 3],
95137
},
96138
},
97139
},
@@ -156,14 +198,7 @@ const OrderChart = (props: { orders?: Order[] }) => {
156198
};
157199
}, [orders]);
158200

159-
return (
160-
<Card>
161-
<CardHeader title={translate('pos.dashboard.month_history')} />
162-
<CardContent>
163-
<div ref={chartRef} style={{ width: '100%', height: 300 }} />
164-
</CardContent>
165-
</Card>
166-
);
201+
return <div ref={chartRef} style={{ width: '100%', height: 300 }} />;
167202
};
168203

169204
interface TotalByDay {

examples/demo/vite.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export default defineConfig(async ({ mode }) => {
4747
}),
4848
],
4949
define: {
50-
'process.env': process.env,
50+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
51+
'process.env.REACT_APP_DATA_PROVIDER': JSON.stringify(
52+
process.env.REACT_APP_DATA_PROVIDER
53+
),
5154
},
5255
server: {
5356
port: 8000,

0 commit comments

Comments
 (0)