Skip to content

Commit

Permalink
fix: support independent data provider, refactor example (#70)
Browse files Browse the repository at this point in the history
* fix: support independent data provider, refactor example

* chore: move config parsing outside of App component
  • Loading branch information
amcgee authored Aug 14, 2019
1 parent becae1a commit 7db27f6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 40 deletions.
46 changes: 20 additions & 26 deletions examples/cra/src/App.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import React from 'react'
import './App.css'
import { useConfig, useDataQuery } from '@dhis2/app-runtime'
import { SwitchableProvider } from './components/SwitchableProvider'
import { ConfigConsumer } from './components/ConfigConsumer'
import { IndicatorList } from './components/IndicatorList'

const config = {
baseUrl: process.env.REACT_APP_D2_BASE_URL || 'https://play.dhis2.org/dev',
apiVersion: process.env.REACT_APP_D2_API_VERSION || 33,
}

const providerType = (
process.env.REACT_APP_D2_PROVIDER_TYPE || 'runtime'
).toLowerCase()

const App = () => {
const config = useConfig()
const { loading, error, data } = useDataQuery({
indicators: {
resource: 'indicators.json',
order: 'shortName:desc',
pageSize: 10,
},
})
return (
<div className="App">
<header className="App-header">
<span>
<strong>Base url:</strong> {config.baseUrl}
</span>
<h3>Indicators (first 10)</h3>
{loading && <span>...</span>}
{error && <span>{`ERROR: ${error.message}`}</span>}
{data && (
<pre>
{data.indicators.indicators
.map(ind => ind.displayName)
.join('\n')}
</pre>
)}
</header>
</div>
<SwitchableProvider type={providerType} config={config}>
<div className="App">
<header className="App-header">
<ConfigConsumer />
<IndicatorList />
</header>
</div>
</SwitchableProvider>
)
}

Expand Down
11 changes: 11 additions & 0 deletions examples/cra/src/components/ConfigConsumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import { useConfig } from '@dhis2/app-runtime'

export const ConfigConsumer = () => {
const config = useConfig()
return (
<span>
<strong>Base url:</strong> {config.baseUrl}
</span>
)
}
26 changes: 26 additions & 0 deletions examples/cra/src/components/IndicatorList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import { useDataQuery } from '@dhis2/app-runtime'

export const IndicatorList = () => {
const { loading, error, data } = useDataQuery({
indicators: {
resource: 'indicators.json',
order: 'shortName:desc',
pageSize: 10,
},
})
return (
<div>
<h3>Indicators (first 10)</h3>
{loading && <span>...</span>}
{error && <span>{`ERROR: ${error.message}`}</span>}
{data && (
<pre>
{data.indicators.indicators
.map(ind => ind.displayName)
.join('\n')}
</pre>
)}
</div>
)
}
18 changes: 18 additions & 0 deletions examples/cra/src/components/SwitchableProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import { Provider, DataProvider } from '@dhis2/app-runtime'

export const SwitchableProvider = ({ type, config, children }) => {
if (type === 'data') {
return (
<DataProvider
baseUrl={config.baseUrl}
apiVersion={config.apiVersion}
>
{children}
</DataProvider>
)
} else if (type === 'runtime') {
return <Provider config={config}>{children}</Provider>
}
throw new Error(`Invalid provider type ${type}`)
}
13 changes: 1 addition & 12 deletions examples/cra/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,8 @@ import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
import * as serviceWorker from './serviceWorker'
import { Provider } from '@dhis2/app-runtime'

ReactDOM.render(
<Provider
config={{
baseUrl: 'https://play.dhis2.org/dev',
apiVersion: 32,
}}
>
<App />
</Provider>,
document.getElementById('root')
)
ReactDOM.render(<App />, document.getElementById('root'))

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down
2 changes: 1 addition & 1 deletion examples/cra/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@
integrity sha512-6It2EVfGskxZCQhuykrfnALg7oVeiI6KclWSmGDqB0AiInVrTGB9Jp9i4/Ad21u9Jde/voVQz6eFX/eSg/UsPA==

"@dhis2/app-runtime@file:../../runtime":
version "1.4.3"
version "1.4.2"

"@hapi/address@2.x.x":
version "2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion services/data/src/components/DataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ProviderInput {
export const DataProvider = (props: ProviderInput) => {
const config = {
...useConfig(),
props,
...props,
}

return (
Expand Down

0 comments on commit 7db27f6

Please sign in to comment.