Skip to content

Commit

Permalink
refactor: handle recording on first load and plugin cache clear
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Oct 9, 2023
1 parent 51b024a commit e156657
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/PluginWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCacheableSection, CacheableSection } from '@dhis2/app-runtime'
import { CenteredContent, CircularLoader, Layer } from '@dhis2/ui'
import postRobot from '@krakenjs/post-robot'
import PropTypes from 'prop-types'
import React, { useEffect } from 'react'
import { Visualization } from './components/Visualization/Visualization.js'
Expand Down Expand Up @@ -34,17 +33,6 @@ const CacheableSectionWrapper = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cacheNow])

useEffect(() => {
const listener = postRobot.on(
'removeCachedData',
// todo: check domain too; differs based on deployment env though
{ window: window.parent },
() => remove()
)

return () => listener.cancel()
}, [remove])

useEffect(() => {
// Synchronize cache state on load or prop update
// -- a back-up to imperative `removeCachedData`
Expand All @@ -69,7 +57,8 @@ CacheableSectionWrapper.propTypes = {
}

const PluginWrapper = (props) => {
const { onInstallationStatusChange, ...propsFromParent } = props
const { onInstallationStatusChange, onPropsReceived, ...propsFromParent } =
props

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

return propsFromParent ? (
<div
style={{
display: 'flex',
height: '100%',
overflow: 'hidden',
}}
>
<CacheableSectionWrapper
id={propsFromParent.cacheId}
cacheNow={propsFromParent.recordOnNextLoad}
isParentCached={propsFromParent.isParentCached}
if (propsFromParent) {
onPropsReceived()

return (
<div
style={{
display: 'flex',
height: '100%',
overflow: 'hidden',
}}
>
<Visualization {...propsFromParent} />
</CacheableSectionWrapper>
</div>
) : null
<CacheableSectionWrapper
id={propsFromParent.cacheId}
cacheNow={propsFromParent.recordOnNextLoad}
isParentCached={propsFromParent.isParentCached}
>
<Visualization {...propsFromParent} />
</CacheableSectionWrapper>
</div>
)
} else {
return null
}
}

PluginWrapper.propTypes = {
onInstallationStatusChange: PropTypes.func,
onPropsReceived: PropTypes.func,
}

export default PluginWrapper

0 comments on commit e156657

Please sign in to comment.