-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [DHIS2-16305] Enrollment Overview Plugins (#3515)
- Loading branch information
1 parent
925dfc4
commit 8e06dbc
Showing
7 changed files
with
183 additions
and
76 deletions.
There are no files selected for viewing
20 changes: 6 additions & 14 deletions
20
...es/common/TEIAndEnrollment/useMetadataForRegistrationForm/hooks/useDataEntryFormConfig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
.../Pages/common/EnrollmentOverviewDomain/EnrollmentPageLayout/renderPageComponents/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export { renderWidgets } from './renderPageComponents'; |
110 changes: 110 additions & 0 deletions
110
...nrollmentOverviewDomain/EnrollmentPageLayout/renderPageComponents/renderPageComponents.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// @flow | ||
import React from 'react'; | ||
import log from 'loglevel'; | ||
import type { | ||
ColumnConfig, | ||
DefaultWidgetColumnConfig, | ||
PluginWidgetColumnConfig, WidgetConfig, | ||
} from '../DefaultEnrollmentLayout.types'; | ||
import { errorCreator } from '../../../../../../../capture-core-utils'; | ||
import { EnrollmentPlugin } from '../../../EnrollmentPlugin'; | ||
import { WidgetTypes } from '../DefaultEnrollmentLayout.constants'; | ||
|
||
const MemoizedWidgets: { [key: string]: React$ComponentType<any> } = {}; | ||
const UnsupportedWidgets: { [key: string]: boolean } = {}; | ||
|
||
const renderComponent = ( | ||
widget: ColumnConfig, | ||
availableWidgets: $ReadOnly<{ [key: string]: WidgetConfig }>, | ||
props: Object, | ||
) => { | ||
// Manually casting the type to DefaultWidgetColumnConfig | ||
const { name, settings = {} } = ((widget: any): DefaultWidgetColumnConfig); | ||
const widgetConfig = availableWidgets[name]; | ||
|
||
if (!widgetConfig) { | ||
if (!UnsupportedWidgets[name]) { | ||
log.error(errorCreator(`Widget ${name} is not supported`)({ name })); | ||
UnsupportedWidgets[name] = true; | ||
} | ||
return null; | ||
} | ||
|
||
const { getProps, shouldHideWidget, getCustomSettings } = widgetConfig; | ||
|
||
const hideWidget = shouldHideWidget && shouldHideWidget(props); | ||
if (hideWidget) return null; | ||
let widgetProps = {}; | ||
|
||
// In case the widget is not supported, we don't want to crash the app | ||
try { | ||
widgetProps = getProps(props); | ||
} catch (error) { | ||
log.error(errorCreator(`Error while getting widget props for widget ${name}`)({ error, props })); | ||
return null; | ||
} | ||
const customSettings = getCustomSettings && getCustomSettings(settings); | ||
|
||
let Widget = MemoizedWidgets[name]; | ||
|
||
if (!Widget) { | ||
Widget = widgetConfig.Component; | ||
MemoizedWidgets[name] = React.memo(Widget); | ||
} | ||
|
||
return ( | ||
<Widget | ||
{...widgetProps} | ||
{...customSettings} | ||
key={name} | ||
/> | ||
); | ||
}; | ||
|
||
const getPropsForPlugin = ({ program, enrollmentId, teiId, orgUnitId }) => ({ | ||
programId: program.id, | ||
enrollmentId, | ||
teiId, | ||
orgUnitId, | ||
}); | ||
|
||
const renderPlugin = ( | ||
widget: ColumnConfig, | ||
availableWidgets: $ReadOnly<{ [key: string]: WidgetConfig }>, | ||
props: Object, | ||
) => { | ||
// Manually casting the type to PluginWidgetColumnConfig | ||
const { source } = ((widget: any): PluginWidgetColumnConfig); | ||
let PluginWidget = MemoizedWidgets[source]; | ||
|
||
if (!PluginWidget) { | ||
PluginWidget = EnrollmentPlugin; | ||
MemoizedWidgets[source] = (PluginWidget); | ||
} | ||
const widgetProps = getPropsForPlugin(props); | ||
|
||
return ( | ||
<PluginWidget | ||
key={source} | ||
pluginSource={source} | ||
{...widgetProps} | ||
/> | ||
); | ||
}; | ||
|
||
export const renderWidgets = ( | ||
widget: ColumnConfig, | ||
availableWidgets: $ReadOnly<{ [key: string]: WidgetConfig }>, | ||
props: Object, | ||
) => { | ||
const { type } = widget; | ||
|
||
if (type.toLowerCase() === WidgetTypes.COMPONENT) { | ||
return renderComponent(widget, availableWidgets, props); | ||
} else if (type.toLowerCase() === WidgetTypes.PLUGIN) { | ||
return renderPlugin(widget, availableWidgets, props); | ||
} | ||
|
||
log.error(errorCreator(`Widget type ${type} is not supported`)({ type })); | ||
return null; | ||
}; |
46 changes: 46 additions & 0 deletions
46
src/core_modules/capture-core/components/Pages/common/EnrollmentPlugin/EnrollmentPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// @flow | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { Plugin } from '@dhis2/app-runtime/build/es/experimental'; | ||
import { useHistory } from 'react-router-dom'; | ||
|
||
type EnrollmentPluginProps = {| | ||
enrollmentId: string, | ||
programId?: string, | ||
teiId: string, | ||
orgUnitId: string, | ||
pluginSource: string, | ||
|}; | ||
|
||
export const EnrollmentPlugin = ({ pluginSource, ...passOnProps }: EnrollmentPluginProps) => { | ||
const [pluginWidth, setPluginWidth] = useState(undefined); | ||
const history = useHistory(); | ||
const containerRef = useRef<?HTMLDivElement>(); | ||
|
||
useEffect(() => { | ||
const { current: container } = containerRef; | ||
if (!container) return () => {}; | ||
|
||
const resizeObserver = new ResizeObserver((entries) => { | ||
entries.forEach(entry => setPluginWidth(entry.contentRect.width)); | ||
}); | ||
|
||
resizeObserver.observe(container); | ||
|
||
// Cleanup function | ||
return () => { | ||
resizeObserver.unobserve(container); | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [containerRef]); | ||
|
||
return ( | ||
<div ref={containerRef}> | ||
<Plugin | ||
pluginSource={pluginSource} | ||
width={pluginWidth} | ||
navigate={history.push} | ||
{...passOnProps} | ||
/> | ||
</div> | ||
); | ||
}; |
2 changes: 2 additions & 0 deletions
2
src/core_modules/capture-core/components/Pages/common/EnrollmentPlugin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export { EnrollmentPlugin } from './EnrollmentPlugin'; |