`;
exports[`renders a VisualizationPlugin for REPORT_TABLE 1`] = `
-
-
-
+
+
-
-
-
+
+
Date: Thu, 18 Feb 2021 01:09:56 +0100
Subject: [PATCH 3/6] fix: wait for in-flight fetches of the same plugin type
---
.../VisualizationItem/Visualization/plugin.js | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/src/components/Item/VisualizationItem/Visualization/plugin.js b/src/components/Item/VisualizationItem/Visualization/plugin.js
index c60f158b6..bfcefda26 100644
--- a/src/components/Item/VisualizationItem/Visualization/plugin.js
+++ b/src/components/Item/VisualizationItem/Visualization/plugin.js
@@ -35,6 +35,11 @@ const getPlugin = async type => {
}
const fetchPlugin = async (type, baseUrl) => {
+ const globalName = itemTypeToGlobalVariable[type]
+ if (global[globalName]) {
+ return global[globalName] // Will be a promise if fetch is in progress
+ }
+
const scripts = []
if (type === EVENT_REPORT || type === EVENT_CHART) {
@@ -43,16 +48,19 @@ const fetchPlugin = async (type, baseUrl) => {
scripts.push('./vendor/jquery-3.3.1.min.js')
scripts.push('./vendor/jquery-migrate-3.0.1.min.js')
} else {
- scripts.push('./vendor/babel-polyfill-6.26.0.min.js')
- scripts.push('./vendor/jquery-3.3.1.min.js')
- scripts.push('./vendor/jquery-migrate-3.0.1.min.js')
+ scripts.push('./vendor/babel-polyfill-6.26.0.js')
+ scripts.push('./vendor/jquery-3.3.1.js')
+ scripts.push('./vendor/jquery-migrate-3.0.1.js')
}
}
scripts.push(baseUrl + itemTypeToScriptPath[type])
- await Promise.all(scripts.map(loadExternalScript))
- return getPlugin(type)
+ const scriptsPromise = Promise.all(scripts.map(loadExternalScript)).then(
+ () => global[globalName] // At this point, has been replaced with the real thing
+ )
+ global[globalName] = scriptsPromise
+ return await scriptsPromise
}
export const pluginIsAvailable = type =>
From 2a723fb4852df52305da239b8dfc82149668ad09 Mon Sep 17 00:00:00 2001
From: Jen Jones Arnesen
Date: Thu, 11 Mar 2021 10:49:53 +0100
Subject: [PATCH 4/6] fix: move visualizer props to DataVisualizerPlugin
---
.../Visualization/DataVisualizerPlugin.js | 29 ++-
.../Visualization/Visualization.js | 29 +--
.../__tests__/Visualization.spec.js | 183 ++++++++----------
.../__snapshots__/Visualization.spec.js.snap | 74 +------
4 files changed, 111 insertions(+), 204 deletions(-)
diff --git a/src/components/Item/VisualizationItem/Visualization/DataVisualizerPlugin.js b/src/components/Item/VisualizationItem/Visualization/DataVisualizerPlugin.js
index 446eabe09..db7866c7f 100644
--- a/src/components/Item/VisualizationItem/Visualization/DataVisualizerPlugin.js
+++ b/src/components/Item/VisualizationItem/Visualization/DataVisualizerPlugin.js
@@ -1,5 +1,8 @@
-import React, { Suspense } from 'react'
+import React, { Suspense, useState } from 'react'
+import PropTypes from 'prop-types'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
+import { useUserSettings } from '../../../UserSettingsProvider'
+import LoadingMask from './LoadingMask'
const VisualizationPlugin = React.lazy(() =>
import(
@@ -7,11 +10,29 @@ const VisualizationPlugin = React.lazy(() =>
)
)
-export const DataVisualizerPlugin = props => {
- const d2 = useD2({})
+const DataVisualizerPlugin = props => {
+ const d2 = useD2()
+ const { userSettings } = useUserSettings()
+ const [pluginLoaded, setPluginLoaded] = useState(false)
+
return (
}>
-
+ {!pluginLoaded && }
+ setPluginLoaded(true)}
+ {...props}
+ />
)
}
+
+DataVisualizerPlugin.propTypes = {
+ style: PropTypes.object,
+}
+
+export default DataVisualizerPlugin
diff --git a/src/components/Item/VisualizationItem/Visualization/Visualization.js b/src/components/Item/VisualizationItem/Visualization/Visualization.js
index dba469ca9..6546037d1 100644
--- a/src/components/Item/VisualizationItem/Visualization/Visualization.js
+++ b/src/components/Item/VisualizationItem/Visualization/Visualization.js
@@ -5,7 +5,7 @@ import i18n from '@dhis2/d2-i18n'
import DefaultPlugin from './DefaultPlugin'
import MapPlugin from './MapPlugin'
-import LoadingMask from './LoadingMask'
+import DataVisualizerPlugin from './DataVisualizerPlugin'
import NoVisualizationMessage from './NoVisualizationMessage'
import getFilteredVisualization from './getFilteredVisualization'
@@ -19,9 +19,7 @@ import {
import { getVisualizationId } from '../../../../modules/item'
import memoizeOne from '../../../../modules/memoizeOne'
import { sGetVisualization } from '../../../../reducers/visualizations'
-import { UserSettingsCtx } from '../../../UserSettingsProvider'
import { pluginIsAvailable } from './plugin'
-import { DataVisualizerPlugin } from './DataVisualizerPlugin'
class Visualization extends React.Component {
state = {
@@ -81,24 +79,13 @@ class Visualization extends React.Component {
case CHART:
case REPORT_TABLE: {
return (
- <>
- {!this.state.pluginLoaded && (
-
+
- >
+ style={pluginProps.style}
+ />
)
}
case MAP: {
@@ -131,8 +118,6 @@ class Visualization extends React.Component {
}
}
-Visualization.contextType = UserSettingsCtx
-
Visualization.propTypes = {
activeType: PropTypes.string,
availableHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
diff --git a/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js b/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js
index 1afad314c..9d986529e 100644
--- a/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js
+++ b/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js
@@ -2,16 +2,11 @@ import React from 'react'
import { render } from '@testing-library/react'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
-import UserSettingsProvider from '../../../../UserSettingsProvider'
import Visualization from '../Visualization'
-jest.mock('@dhis2/app-runtime-adapter-d2', () => ({
- useD2: () => ({}),
-}))
-
jest.mock(
- '@dhis2/data-visualizer-plugin',
+ '../DataVisualizerPlugin',
() =>
function MockVisualizationPlugin() {
return
@@ -38,29 +33,19 @@ const DEFAULT_STORE_WITH_ONE_ITEM = {
visualizations: { rainbowVis: { rows: [], columns: [], filters: [] } },
}
-global.eventChartPlugin = {}
-global.eventReportPlugin = {}
-global.fetch = jest.fn(() =>
- Promise.resolve({
- userSettings: { keyAnalysisDisplayPropert: 'name' },
- })
-)
-
test('renders a MapPlugin when activeType is MAP', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -69,18 +54,16 @@ test('renders a MapPlugin when activeType is MAP', () => {
test('renders a VisualizationPlugin for CHART', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -89,18 +72,16 @@ test('renders a VisualizationPlugin for CHART', () => {
test('renders a VisualizationPlugin for REPORT_TABLE', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -109,18 +90,16 @@ test('renders a VisualizationPlugin for REPORT_TABLE', () => {
test('renders active type MAP rather than original type REPORT_TABLE', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -129,18 +108,16 @@ test('renders active type MAP rather than original type REPORT_TABLE', () => {
test('renders active type REPORT_TABLE rather than original type MAP', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -149,18 +126,16 @@ test('renders active type REPORT_TABLE rather than original type MAP', () => {
test('renders a DefaultPlugin when activeType is EVENT_CHART', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -169,18 +144,16 @@ test('renders a DefaultPlugin when activeType is EVENT_CHART', () => {
test('renders a DefaultPlugin when activeType is EVENT_REPORT', () => {
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
@@ -192,14 +165,12 @@ test('renders NoVisMessage when no visualization', () => {
}
const { container } = render(
-
-
-
+
)
expect(container).toMatchSnapshot()
diff --git a/src/components/Item/VisualizationItem/Visualization/__tests__/__snapshots__/Visualization.spec.js.snap b/src/components/Item/VisualizationItem/Visualization/__tests__/__snapshots__/Visualization.spec.js.snap
index a626504cf..07c9ea066 100644
--- a/src/components/Item/VisualizationItem/Visualization/__tests__/__snapshots__/Visualization.spec.js.snap
+++ b/src/components/Item/VisualizationItem/Visualization/__tests__/__snapshots__/Visualization.spec.js.snap
@@ -37,59 +37,13 @@ exports[`renders a MapPlugin when activeType is MAP 1`] = `
exports[`renders a VisualizationPlugin for CHART 1`] = `
-
-
-
-
-
+ class="visualization-plugin"
+ />
`;
exports[`renders a VisualizationPlugin for REPORT_TABLE 1`] = `
-
-
-
-
-
@@ -106,30 +60,6 @@ exports[`renders active type MAP rather than original type REPORT_TABLE 1`] = `
exports[`renders active type REPORT_TABLE rather than original type MAP 1`] = `