Skip to content

Commit 8ef7d93

Browse files
committed
Remove extra API calls
1 parent 1c39633 commit 8ef7d93

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

packages/google-analytics-embed-react/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "google-analytics-embed-react",
33
"description": "Bringing the Google Analytics Embed Components for your ReactJS application",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"type": "module",
66
"main": "dist/index.js",
77
"module": "dist/index.esm.js",
@@ -10,6 +10,7 @@
1010
"type": "git",
1111
"url": "git+https://github.com/arimacdev/google-analytics-embed-react.git"
1212
},
13+
"license": "MIT",
1314
"author": {
1415
"name": "WhizSid",
1516
"url": "https://github.com/whizsid",
@@ -49,5 +50,8 @@
4950
"peerDependencies": {
5051
"react": "*",
5152
"react-dom": "*"
53+
},
54+
"dependencies": {
55+
"react-fast-compare": "^3.2"
5256
}
5357
}

packages/google-analytics-embed-react/rollup.config.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ export default {
2222
],
2323
plugins: [
2424
typescript(),
25-
babel({
26-
babelHelpers: 'bundled',
27-
exclude: 'node_modules/**',
28-
extensions: [".js", ".jsx", ".ts", ".tsx"],
29-
plugins: [],
30-
presets: ["@babel/preset-react", "@babel/preset-env"]
31-
}),
3225
peerDepsExternal(),
3326
resolve({
3427
browser: true
3528
}),
36-
commonjs()
29+
commonjs(),
30+
babel({
31+
babelHelpers: 'bundled',
32+
exclude: 'node_modules/**',
33+
extensions: ['.js', '.jsx', '.ts', '.tsx'],
34+
plugins: [],
35+
presets: ['@babel/preset-react', '@babel/preset-env']
36+
}),
3737
]
3838
};

packages/google-analytics-embed-react/src/DataChart.tsx

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import GoogleAnalyticsContext, { GoogleAnalyticsState } from './GoogleAnalyticsContext';
3+
import equal from 'react-fast-compare';
34

45
export type DataChartProps<T> = {
56
/** Query */
@@ -33,25 +34,40 @@ class DataChart<O> extends React.Component<DataChartProps<O>> {
3334
}
3435

3536
componentDidMount(): void {
36-
this.componentDidUpdate();
37+
this.componentDidUpdate({ query: null } as any);
3738
}
3839

39-
componentDidUpdate(): void {
40+
componentDidUpdate(prevProps: DataChartProps<O>): void {
4041
const gaState = this.context as GoogleAnalyticsState;
4142
const { query, children, style, className, onSuccess, onError, ...chartOptions } = this.props;
4243
// Rendering the component only if a user authenticated
4344
if (gaState === 'AUTH_SUCCESS') {
4445
// Updating the existing chart with new options if already rendered
4546
if (this.googleDataChart != null) {
46-
this.googleDataChart.set({
47-
query,
48-
chart: {
49-
container: this.elementRef.current as HTMLElement,
50-
type: this.chartType as any,
51-
options: chartOptions as any
52-
}
53-
});
54-
this.googleDataChart.execute();
47+
const {
48+
query: prevQuery,
49+
children: _children,
50+
style: _style,
51+
className: _className,
52+
onSuccess: _onSuccess,
53+
onError: _onError,
54+
...prevChartOptions
55+
} = prevProps;
56+
57+
if (
58+
prevQuery != null &&
59+
(!equal(prevQuery, query) || !equal(prevChartOptions, chartOptions))
60+
) {
61+
this.googleDataChart.set({
62+
query,
63+
chart: {
64+
container: this.elementRef.current as HTMLElement,
65+
type: this.chartType as any,
66+
options: chartOptions as any
67+
}
68+
});
69+
this.googleDataChart.execute();
70+
}
5571
} else {
5672
// Creating a chart if a chart instance not created
5773
this.googleDataChart = new gapi.analytics.googleCharts.DataChart({

sites/react-backend-auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dependencies": {
66
"@types/react": "*",
77
"@types/react-dom": "*",
8-
"google-analytics-embed-react": "0.0.2",
8+
"google-analytics-embed-react": "0.0.3",
99
"react": "*",
1010
"react-dom": "*",
1111
"axios": "*"

sites/react-frontend-auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dependencies": {
66
"@types/react": "*",
77
"@types/react-dom": "*",
8-
"google-analytics-embed-react": "0.0.2",
8+
"google-analytics-embed-react": "0.0.3",
99
"react": "*",
1010
"react-dom": "*"
1111
},

0 commit comments

Comments
 (0)