Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dhis2/app-runtime-adapter-d2

Package Overview
Dependencies
Maintainers
14
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhis2/app-runtime-adapter-d2

Support [`d2`](https://github.com/dhis2/d2) and [`d2-ui`](https://github.com/dhis2/d2-ui) components in [DHIS2 Platform](https://platform.dhis2.nu) applications

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
473
decreased by-46.37%
Maintainers
14
Weekly downloads
 
Created
Source

app-runtime-adapter-d2

Support d2 and d2-ui components in DHIS2 Platform applications

Requirements

Ths library has two peer dependencies:

Initializing D2

There are two entrypoints to support initialization and access to the d2 singleton from within an application.

  • useD2, a React Hook
import React, { useState } from 'react'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
import { getUserSettings } from 'd2'

const d2Config = {
    schemas: ['dataElement', 'program', 'userGroup'],
}

const MyApp = () => {
    const [userSettings, setUserSettings] = useState(null)

    const onInitialized = d2 => {
        getUserSettings().then(setUserSettings)

        /* ... do other things with d2 ... */
    }
    const { d2, d2Error } = useD2({ d2Config, onInitialized })

    /* ... optionally render error or loading state ... */

    if (d2 && !d2Error && userSettings) {
        /* ... render the application ... */
    }
}

export default MyApp
  • D2Shim, a React Component that wraps the useD2 hook. It accepts a single child function
import React, { useState } from 'react'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
import { getUserSettings } from 'd2'
import { ScreenCover, CircularLoader } from '@dhis2/ui-core'
import Root from './components/Root' // This is the business logic of the application

const d2Config = {
    schemas: ['dataElement', 'program', 'userGroup'],
}

const onD2Initialized = async d2 => {
    const userSettings = await getUserSettings()

    /* do something with userSettings or d2 - spawn Redux initialization actions, for instance */
}

const MyApp = () => (
    <D2Shim d2Config={d2Config} onInitialized={onD2Initialized}>
        {({ d2, d2Error }) => (
            <>
                {d2Error && (
                    <div
                        style={{
                            display: 'flex',
                            height: '100%',
                            width: '100%',
                            alignItems: 'center',
                            justifyContent: 'center',
                        }}
                    >
                        {`D2 initialization error: ${d2Error}`}
                    </div>
                )}
                {!d2 && !d2Error && (
                    <ScreenCover>
                        <CircularLoader />
                    </ScreenCover>
                )}
                {d2 && <Root d2={d2} store={store} />}
            </>
        )}
    </D2Shim>
)

export default MyApp

Both accept three options:

  • d2Config - an object passed to the init function when initializing d2
  • onInitialized - a callback function executed when d2 has been initialized. It will be awaited, so returning a promise will delay resolution of the d2 return value.
  • i18nRoot - an optional string that indicates the app's directory containing "legacy" i18n files, e.g., i18n_module_en.properties. In data-visualier, for example, the string would be "i18n_old". When this string is set, the properties file of the current language will be loaded.

Referencing D2

The same two entrypoints can be used multiple places within an application to reference the d2 singleton. Once d2 has been initialized it will not be re-initialized, so you can safely call useD2 or render D2Shim without any arguments further down the VDom tree after rendering D2Shim with initialization parameters at the application entrypoint.

Report an issue

The issue tracker can be found in DHIS2 JIRA under the LIBS project.

Deep links:

FAQs

Package last updated on 26 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc