Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lazily load plugin scripts and dependencies when needed (DHIS2-10518) #1546

Merged
merged 10 commits into from
Mar 11, 2021
1 change: 1 addition & 0 deletions d2.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const config = {
name: 'dashboard',
type: 'app',
coreApp: true,
title: 'Dashboard',
Expand Down
90 changes: 0 additions & 90 deletions public/index.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { Suspense } from 'react'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'

const VisualizationPlugin = React.lazy(() =>
import(
/* webpackChunkName: "data-visualizer-plugin" */ /* webpackPrefetch: true */ '@dhis2/data-visualizer-plugin'
)
)

export const DataVisualizerPlugin = props => {
const d2 = useD2({})
return (
<Suspense fallback={<div />}>
<VisualizationPlugin d2={d2} {...props} />
</Suspense>
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import React from 'react'
import PropTypes from 'prop-types'
import { CircularLoader } from '@dhis2/ui'

import classes from './styles/LoadingMask.module.css'

const LoadingMask = () => {
const LoadingMask = ({ style }) => {
return (
<div className={classes.center}>
<div className={classes.center} style={style}>
<CircularLoader />
</div>
)
}

LoadingMask.propTypes = {
style: PropTypes.object,
}

export default LoadingMask
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import VisualizationPlugin from '@dhis2/data-visualizer-plugin'
import i18n from '@dhis2/d2-i18n'
import { D2Shim } from '@dhis2/app-runtime-adapter-d2'

import DefaultPlugin from './DefaultPlugin'
import MapPlugin from './MapPlugin'
Expand All @@ -23,6 +21,7 @@ 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 = {
Expand Down Expand Up @@ -84,29 +83,21 @@ class Visualization extends React.Component {
return (
<>
{!this.state.pluginLoaded && (
<div style={pluginProps.style}>
<LoadingMask />
</div>
<LoadingMask style={pluginProps.style} />
)}
<D2Shim d2Config={{}}>
{({ d2 }) => (
<VisualizationPlugin
d2={d2}
visualization={this.memoizedGetFilteredVisualization(
pluginProps.visualization,
pluginProps.itemFilters
)}
onLoadingComplete={this.onLoadingComplete}
forDashboard={true}
style={pluginProps.style}
userSettings={{
displayProperty: this.context
.userSettings
.keyAnalysisDisplayProperty,
}}
/>
<DataVisualizerPlugin
visualization={this.memoizedGetFilteredVisualization(
pluginProps.visualization,
pluginProps.itemFilters
)}
</D2Shim>
onLoadingComplete={this.onLoadingComplete}
forDashboard={true}
style={pluginProps.style}
userSettings={{
displayProperty: this.context.userSettings
.keyAnalysisDisplayProperty,
}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forDashboard, userSettings and onLoadingComplete could be moved to DataVisualizerPlugin. Should be pretty straightforward (onLoadingComplete just sets a state var that is only relevant for DataVisualizerPlugin). But if you don't have time for this, I can do it. If its not done by noon (either by me or you), then let's just go ahead and merge as is, and I can do this later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes committed

/>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UserSettingsProvider from '../../../../UserSettingsProvider'
import Visualization from '../Visualization'

jest.mock('@dhis2/app-runtime-adapter-d2', () => ({
D2Shim: ({ children }) => children({ d2: {} }),
useD2: () => ({}),
}))

jest.mock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,57 @@ exports[`renders a MapPlugin when activeType is MAP 1`] = `
exports[`renders a VisualizationPlugin for CHART 1`] = `
<div>
<div
class="center"
style="height: 500px;"
>
<div
class="center"
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
>
<div
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
</div>
<div
class="visualization-plugin"
/>
<div />
</div>
`;

exports[`renders a VisualizationPlugin for REPORT_TABLE 1`] = `
<div>
<div
class="center"
style="height: 500px;"
>
<div
class="center"
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
>
<div
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
</div>
<div
Expand All @@ -115,30 +107,27 @@ exports[`renders active type MAP rather than original type REPORT_TABLE 1`] = `
exports[`renders active type REPORT_TABLE rather than original type MAP 1`] = `
<div>
<div
class="center"
style="height: 500px;"
>
<div
class="center"
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
>
<div
class="jsx-19995197 "
data-test="dhis2-uicore-circularloader"
role="progressbar"
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<svg
class="jsx-19995197"
viewBox="22 22 44 44"
>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
<circle
class="jsx-19995197 circle"
cx="44"
cy="44"
fill="none"
r="20.2"
stroke-width="3.6"
/>
</svg>
</div>
</div>
<div
Expand Down
Loading