Skip to content

Commit e156657

Browse files
committed
refactor: handle recording on first load and plugin cache clear
1 parent 51b024a commit e156657

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/PluginWrapper.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useCacheableSection, CacheableSection } from '@dhis2/app-runtime'
22
import { CenteredContent, CircularLoader, Layer } from '@dhis2/ui'
3-
import postRobot from '@krakenjs/post-robot'
43
import PropTypes from 'prop-types'
54
import React, { useEffect } from 'react'
65
import { Visualization } from './components/Visualization/Visualization.js'
@@ -34,17 +33,6 @@ const CacheableSectionWrapper = ({
3433
// eslint-disable-next-line react-hooks/exhaustive-deps
3534
}, [cacheNow])
3635

37-
useEffect(() => {
38-
const listener = postRobot.on(
39-
'removeCachedData',
40-
// todo: check domain too; differs based on deployment env though
41-
{ window: window.parent },
42-
() => remove()
43-
)
44-
45-
return () => listener.cancel()
46-
}, [remove])
47-
4836
useEffect(() => {
4937
// Synchronize cache state on load or prop update
5038
// -- a back-up to imperative `removeCachedData`
@@ -69,7 +57,8 @@ CacheableSectionWrapper.propTypes = {
6957
}
7058

7159
const PluginWrapper = (props) => {
72-
const { onInstallationStatusChange, ...propsFromParent } = props
60+
const { onInstallationStatusChange, onPropsReceived, ...propsFromParent } =
61+
props
7362

7463
useEffect(() => {
7564
// Get & send PWA installation status now
@@ -78,27 +67,34 @@ const PluginWrapper = (props) => {
7867
}).then(onInstallationStatusChange)
7968
}, [onInstallationStatusChange])
8069

81-
return propsFromParent ? (
82-
<div
83-
style={{
84-
display: 'flex',
85-
height: '100%',
86-
overflow: 'hidden',
87-
}}
88-
>
89-
<CacheableSectionWrapper
90-
id={propsFromParent.cacheId}
91-
cacheNow={propsFromParent.recordOnNextLoad}
92-
isParentCached={propsFromParent.isParentCached}
70+
if (propsFromParent) {
71+
onPropsReceived()
72+
73+
return (
74+
<div
75+
style={{
76+
display: 'flex',
77+
height: '100%',
78+
overflow: 'hidden',
79+
}}
9380
>
94-
<Visualization {...propsFromParent} />
95-
</CacheableSectionWrapper>
96-
</div>
97-
) : null
81+
<CacheableSectionWrapper
82+
id={propsFromParent.cacheId}
83+
cacheNow={propsFromParent.recordOnNextLoad}
84+
isParentCached={propsFromParent.isParentCached}
85+
>
86+
<Visualization {...propsFromParent} />
87+
</CacheableSectionWrapper>
88+
</div>
89+
)
90+
} else {
91+
return null
92+
}
9893
}
9994

10095
PluginWrapper.propTypes = {
10196
onInstallationStatusChange: PropTypes.func,
97+
onPropsReceived: PropTypes.func,
10298
}
10399

104100
export default PluginWrapper

0 commit comments

Comments
 (0)