-
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.
- Loading branch information
1 parent
271c74d
commit f3a3757
Showing
5 changed files
with
118 additions
and
92 deletions.
There are no files selected for viewing
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; | ||
}; |
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