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: implement plugin wrappers #1332

Merged
merged 11 commits into from
Mar 13, 2023
Merged
3 changes: 2 additions & 1 deletion examples/cra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"@dhis2/app-service-alerts": "file:../../services/alerts",
"@dhis2/app-service-config": "file:../../services/config",
"@dhis2/app-service-data": "file:../../services/data",
"@dhis2/app-service-offline": "file:../../services/offline"
"@dhis2/app-service-offline": "file:../../services/offline",
"@dhis2/app-service-plugin": "file:../../services/plugin"
},
"scripts": {
"start": "react-scripts start",
Expand Down
49 changes: 36 additions & 13 deletions examples/cra/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1054,29 +1054,42 @@
integrity sha512-ij4wRiunFfaJxjB0BdrYHIH8FxBJpOwNPhhAcunlmPdXudL1WQV1qoP9un6JsEBAgQH+7UXyyjh0g7jTxXK6tg==

"@dhis2/app-runtime@file:../../runtime":
version "3.5.0"
version "3.7.0"
dependencies:
"@dhis2/app-service-alerts" "3.5.0"
"@dhis2/app-service-config" "3.5.0"
"@dhis2/app-service-data" "3.5.0"
"@dhis2/app-service-offline" "3.5.0"
"@dhis2/app-service-alerts" "3.7.0"
"@dhis2/app-service-config" "3.7.0"
"@dhis2/app-service-data" "3.7.0"
"@dhis2/app-service-offline" "3.7.0"
"@dhis2/app-service-plugin" "3.7.0"
"@dhis2/d2-i18n" "^1.1.0"

"@dhis2/app-service-alerts@3.5.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.5.0"
"@dhis2/app-service-alerts@3.7.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.7.0"

"@dhis2/app-service-config@3.5.0", "@dhis2/app-service-config@file:../../services/config":
version "3.5.0"
"@dhis2/app-service-config@3.7.0", "@dhis2/app-service-config@file:../../services/config":
version "3.7.0"

"@dhis2/app-service-data@3.5.0", "@dhis2/app-service-data@file:../../services/data":
version "3.5.0"
"@dhis2/app-service-data@3.7.0", "@dhis2/app-service-data@file:../../services/data":
version "3.7.0"
dependencies:
react-query "^3.13.11"

"@dhis2/app-service-offline@3.5.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.5.0"
"@dhis2/app-service-offline@3.7.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.7.0"
dependencies:
lodash "^4.17.21"

"@dhis2/app-service-plugin@3.7.0", "@dhis2/app-service-plugin@file:../../services/plugin":
version "3.7.0"

"@dhis2/d2-i18n@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.0.tgz#ec777c5091f747e4c5aa4f9801c62ba4d1ef3d16"
integrity sha512-x3u58goDQsMfBzy50koxNrJjofJTtjRZOfz6f6Py/wMMJfp/T6vZjWMQgcfWH0JrV6d04K1RTt6bI05wqsVQvg==
dependencies:
i18next "^10.3"
moment "^2.24.0"

"@hapi/address@2.x.x":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5"
Expand Down Expand Up @@ -5149,6 +5162,11 @@ https-browserify@^1.0.0:
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"
integrity sha1-7AbBDgo0wPL68Zn3/X/Hj//QPHM=

i18next@^10.3:
version "10.6.0"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-10.6.0.tgz#90ffd9f9bc617f34b9a12e037260f524445f7684"
integrity sha512-ycRlN145kQf8EsyDAzMfjqv1ZT1Jwp7P2H/07bP8JLWm+7cSLD4XqlJOvq4mKVS2y2mMIy10lX9ZeYUdQ0qSRw==

iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -6878,6 +6896,11 @@ mkdirp@^0.5.1, mkdirp@^0.5.3, mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"

moment@^2.24.0:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==

move-concurrently@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
Expand Down
3 changes: 2 additions & 1 deletion examples/query-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@dhis2/app-service-alerts": "file:../../services/alerts",
"@dhis2/app-service-config": "file:../../services/config",
"@dhis2/app-service-data": "file:../../services/data",
"@dhis2/app-service-offline": "file:../../services/offline"
"@dhis2/app-service-offline": "file:../../services/offline",
"@dhis2/app-service-plugin": "file:../../services/plugin"
}
}
51 changes: 37 additions & 14 deletions examples/query-playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1789,30 +1789,45 @@
dependencies:
moment "^2.24.0"

"@dhis2/app-runtime@*", "@dhis2/app-runtime@^2.2.2", "@dhis2/app-runtime@file:../../runtime":
version "3.5.0"
"@dhis2/app-runtime@*":
version "3.8.0"
resolved "https://registry.yarnpkg.com/@dhis2/app-runtime/-/app-runtime-3.8.0.tgz#4ec7fc4ec6647dc8428e3c0d2e14b2d188a993b9"
integrity sha512-f5M1RfUJb4yZaPDywTfogVXjzWcuYGJ7JQzny6iWXrJu1+qrRKYbfFutYNhB+4bXD4bB59DelHWqVaHtrGvbVA==
dependencies:
"@dhis2/app-service-alerts" "3.5.0"
"@dhis2/app-service-config" "3.5.0"
"@dhis2/app-service-data" "3.5.0"
"@dhis2/app-service-offline" "3.5.0"
"@dhis2/app-service-alerts" "3.8.0"
"@dhis2/app-service-config" "3.8.0"
"@dhis2/app-service-data" "3.8.0"
"@dhis2/app-service-offline" "3.8.0"

"@dhis2/app-service-alerts@3.5.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.5.0"
"@dhis2/app-runtime@^2.2.2", "@dhis2/app-runtime@file:../../runtime":
version "3.7.0"
dependencies:
"@dhis2/app-service-alerts" "3.7.0"
"@dhis2/app-service-config" "3.7.0"
"@dhis2/app-service-data" "3.7.0"
"@dhis2/app-service-offline" "3.7.0"
"@dhis2/app-service-plugin" "3.7.0"
"@dhis2/d2-i18n" "^1.1.0"

"@dhis2/app-service-config@3.5.0", "@dhis2/app-service-config@file:../../services/config":
version "3.5.0"
"@dhis2/app-service-alerts@3.7.0", "@dhis2/app-service-alerts@3.8.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.7.0"

"@dhis2/app-service-data@3.5.0", "@dhis2/app-service-data@file:../../services/data":
version "3.5.0"
"@dhis2/app-service-config@3.7.0", "@dhis2/app-service-config@3.8.0", "@dhis2/app-service-config@file:../../services/config":
version "3.7.0"

"@dhis2/app-service-data@3.7.0", "@dhis2/app-service-data@3.8.0", "@dhis2/app-service-data@file:../../services/data":
version "3.7.0"
dependencies:
react-query "^3.13.11"

"@dhis2/app-service-offline@3.5.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.5.0"
"@dhis2/app-service-offline@3.7.0", "@dhis2/app-service-offline@3.8.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.7.0"
dependencies:
lodash "^4.17.21"

"@dhis2/app-service-plugin@3.7.0", "@dhis2/app-service-plugin@file:../../services/plugin":
version "3.7.0"

"@dhis2/app-shell@5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@dhis2/app-shell/-/app-shell-5.2.0.tgz#19fc3c6b18ea18048d3cdd1680ce535417edb6b3"
Expand Down Expand Up @@ -1900,6 +1915,14 @@
i18next "^10.3"
moment "^2.24.0"

"@dhis2/d2-i18n@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.0.tgz#ec777c5091f747e4c5aa4f9801c62ba4d1ef3d16"
integrity sha512-x3u58goDQsMfBzy50koxNrJjofJTtjRZOfz6f6Py/wMMJfp/T6vZjWMQgcfWH0JrV6d04K1RTt6bI05wqsVQvg==
dependencies:
i18next "^10.3"
moment "^2.24.0"

"@dhis2/prop-types@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@dhis2/prop-types/-/prop-types-1.6.4.tgz#ec4d256c9440d4d00071524422a727c61ddaa6f6"
Expand Down
6 changes: 4 additions & 2 deletions runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
"build/**"
],
"dependencies": {
"@dhis2/app-service-alerts": "3.7.0",
"@dhis2/app-service-config": "3.7.0",
"@dhis2/app-service-data": "3.7.0",
"@dhis2/app-service-alerts": "3.7.0",
"@dhis2/app-service-offline": "3.7.0"
"@dhis2/app-service-offline": "3.7.0",
"@dhis2/app-service-plugin": "3.7.0",
"@dhis2/d2-i18n": "^1.1.0"
},
"peerDependencies": {
"prop-types": "^15.7.2",
Expand Down
12 changes: 11 additions & 1 deletion runtime/src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ type ProviderInput = {
config: Config
children: React.ReactNode
offlineInterface?: any // temporary until offline service has types
plugin: boolean
parentAlertsAdd: any
showAlertsInPlugin: boolean
}
export const Provider = ({
config,
children,
offlineInterface,
plugin,
parentAlertsAdd,
showAlertsInPlugin,
}: ProviderInput) => (
<ConfigProvider config={config}>
<AlertsProvider>
<AlertsProvider
plugin={plugin}
parentAlertsAdd={parentAlertsAdd}
showAlertsInPlugin={showAlertsInPlugin}
>
<DataProvider>
<OfflineProvider offlineInterface={offlineInterface}>
{children}
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export {
clearSensitiveCaches,
} from '@dhis2/app-service-offline'

export { PluginSender } from '@dhis2/app-service-plugin'

export { Provider } from './Provider'
3 changes: 3 additions & 0 deletions services/alerts/src/AlertsManagerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const placeholder = () => {

const defaultAlertsManager: AlertsManager = {
add: placeholder,
plugin: false,
parentAlertsAdd: null,
showAlertsInPlugin: false,
}

export const AlertsManagerContext =
Expand Down
12 changes: 10 additions & 2 deletions services/alerts/src/AlertsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { makeAlertsManager } from './makeAlertsManager'
import type { Alert, AlertsManager } from './types'

export const AlertsProvider = ({
plugin,
parentAlertsAdd,
showAlertsInPlugin,
children,
}: {
plugin: boolean
parentAlertsAdd: any
showAlertsInPlugin: boolean
children: React.ReactNode
}): ReactElement => {
const [alerts, setAlerts] = useState<Alert[]>([])
const [alertsManager] = useState<AlertsManager>(() =>
makeAlertsManager(setAlerts)
makeAlertsManager(setAlerts, plugin)
)

return (
<AlertsManagerContext.Provider value={alertsManager}>
<AlertsManagerContext.Provider
value={{ ...alertsManager, parentAlertsAdd, showAlertsInPlugin }}
>
<AlertsContext.Provider value={alerts}>
{children}
</AlertsContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions services/alerts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { AlertsProvider } from './AlertsProvider'
export { useAlerts } from './useAlerts'
export { useAlert } from './useAlert'
export { AlertsManagerContext } from './AlertsManagerContext'
Copy link
Member Author

Choose a reason for hiding this comment

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

this is now being exported so that the PluginSender can access add from the parent (app) and pass it to the plugin, so that the alerts can then be added to the app's AlertsManager rather than the plugin's

4 changes: 3 additions & 1 deletion services/alerts/src/makeAlertsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const toVisibleAlertsArray = (alertsMap: AlertsMap) =>
Array.from(alertsMap.values())

export const makeAlertsManager = (
setAlerts: React.Dispatch<React.SetStateAction<Alert[]>>
setAlerts: React.Dispatch<React.SetStateAction<Alert[]>>,
plugin: boolean
): AlertsManager => {
const alertsMap: AlertsMap = new Map()
let id = 0
Expand All @@ -29,5 +30,6 @@ export const makeAlertsManager = (

return {
add,
plugin,
}
}
3 changes: 3 additions & 0 deletions services/alerts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ export type AlertsMap = Map<number, Alert>

export type AlertsManager = {
add: (alert: Alert, alertRef: AlertRef) => Alert
plugin: boolean
parentAlertsAdd?: any
showAlertsInPlugin?: boolean
}
29 changes: 20 additions & 9 deletions services/alerts/src/useAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const useAlert = (
message: string | ((props: any) => string),
options: AlertOptions | ((props: any) => AlertOptions) = {}
): { show: (props?: any) => void; hide: () => void } => {
const { add }: AlertsManager = useContext(AlertsManagerContext)
const { add, plugin, parentAlertsAdd, showAlertsInPlugin }: AlertsManager =
useContext(AlertsManagerContext)
const alertRef = useRef(<Alert | null>null)

const show = useCallback(
Expand All @@ -17,15 +18,25 @@ export const useAlert = (
const resolvedOptions =
typeof options === 'function' ? options(props) : options

alertRef.current = add(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
if (plugin && parentAlertsAdd && !showAlertsInPlugin) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I passed this showAlertsInPlugin down to useAlert as I thought it made the logic clearer here, but it could be incorporated into logic further up and then not passed down.

alertRef.current = parentAlertsAdd(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
} else {
alertRef.current = add(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
}
},
[add, message, options]
[add, parentAlertsAdd, message, options, plugin, showAlertsInPlugin]
)

const hide = useCallback(() => {
Expand Down
5 changes: 5 additions & 0 deletions services/plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DHIS2 Platform
node_modules
.d2
src/locales
build
11 changes: 11 additions & 0 deletions services/plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# DHIS2 App Data Service

Application configuration for [DHIS2](https://dhis2.org) applications

This library is intended for use with the [DHIS2 Application Platform](https://github.com/dhis2/app-platform).

## Installation

This package is internal to `@dhis2/app-runtime` and generally should not be installed independently.

See [the docs](https://runtime.dhis2.nu) for more.
9 changes: 9 additions & 0 deletions services/plugin/d2.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = {
type: 'lib',

entryPoints: {
lib: './src/index.ts',
},
}

module.exports = config
10 changes: 10 additions & 0 deletions services/plugin/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
collectCoverageFrom: [
'src/**/*.(js|jsx|ts|tsx)',
'!src/index.ts',
'!src/types.ts',
],

// Setup react-testing-library
setupFilesAfterEnv: ['<rootDir>/src/setupRTL.ts'],
}
Loading