Skip to content

Commit

Permalink
fix: remove d2
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Apr 18, 2024
1 parent aafa267 commit 823a99b
Show file tree
Hide file tree
Showing 19 changed files with 491 additions and 521 deletions.
116 changes: 74 additions & 42 deletions src/AppWrapper.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
import { useConfig, useDataEngine } from '@dhis2/app-runtime'
import { D2Shim } from '@dhis2/app-runtime-adapter-d2'
import { CachedDataQueryProvider } from '@dhis2/analytics'
import { useDataEngine } from '@dhis2/app-runtime'
import { DataStoreProvider } from '@dhis2/app-service-datastore'
import React from 'react'
import { Provider as ReduxProvider } from 'react-redux'
import thunk from 'redux-thunk'
import { App } from './components/App.js'
import UserSettingsProvider, {
UserSettingsCtx,
} from './components/UserSettingsProvider.js'
import App from './components/App.js'
import configureStore from './configureStore.js'
import metadataMiddleware from './middleware/metadata.js'
import { USER_DATASTORE_NAMESPACE } from './modules/currentAnalyticalObject.js'
import history from './modules/history.js'
import { systemSettingsKeys } from './modules/systemSettings.js'
import {
USER_SETTINGS_DISPLAY_PROPERTY,
DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY,
} from './modules/userSettings.js'
import './locales/index.js'

const query = {
currentUser: {
resource: 'me',
params: {
fields: 'id,username,displayName~rename(name),settings',
},
},
systemSettings: {
resource: 'systemSettings',
params: {
key: systemSettingsKeys,
},
},
rootOrgUnits: {
resource: 'organisationUnits',
params: {
fields: 'id,displayName,name',
userDataViewFallback: true,
paging: false,
},
},
orgUnitLevels: {
resource: 'organisationUnitLevels',
// TODO how to handle passing params like this?
params: ({ displayNameProp = 'displayName' } = {}) => ({
fields: `id,level,${displayNameProp}~rename(displayName),name`,
paging: false,
}),
},
}

const providerDataTransformation = ({
currentUser,
systemSettings,
rootOrgUnits,
orgUnitLevels,
}) => {
const displayNameProperty =
currentUser.settings[USER_SETTINGS_DISPLAY_PROPERTY] === 'name'
? 'displayName'
: 'displayShortName'

return {
currentUser: {
...currentUser,
settings: {
uiLocale: currentUser.settings.keyUiLocale,
displayProperty:
currentUser.settings[USER_SETTINGS_DISPLAY_PROPERTY],
displayNameProperty,
[DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY]:
displayNameProperty,
},
},
systemSettings,
rootOrgUnits: rootOrgUnits.organisationUnits,
orgUnitLevels: orgUnitLevels.organisationUnitLevels,
}
}

const AppWrapper = () => {
const { baseUrl } = useConfig()
const engine = useDataEngine()
const store = configureStore([
thunk.withExtraArgument(engine),
Expand All @@ -26,43 +86,15 @@ const AppWrapper = () => {
window.store = store
}

const schemas = ['visualization', 'organisationUnit', 'userGroup']
const d2Config = {
schemas,
}

return (
<ReduxProvider store={store}>
<DataStoreProvider namespace={USER_DATASTORE_NAMESPACE}>
<UserSettingsProvider>
<UserSettingsCtx.Consumer>
{({ userSettings }) => {
return userSettings?.uiLocale ? (
<D2Shim
d2Config={d2Config}
locale={userSettings.uiLocale}
>
{({ d2 }) => {
if (!d2) {
// TODO: Handle errors in d2 initialization
return null
} else {
return (
<App
d2={d2}
location={history.location}
baseUrl={baseUrl}
dataEngine={engine}
userSettings={userSettings}
/>
)
}
}}
</D2Shim>
) : null
}}
</UserSettingsCtx.Consumer>
</UserSettingsProvider>
<CachedDataQueryProvider
query={query}
dataTransformation={providerDataTransformation}
>
<App />
</CachedDataQueryProvider>
</DataStoreProvider>
</ReduxProvider>
)
Expand Down
2 changes: 1 addition & 1 deletion src/actions/current.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const acSetCurrent = (value) => ({
value,
})

export const acClear = () => ({
export const acClearCurrent = () => ({
type: CLEAR_CURRENT,
})

Expand Down
6 changes: 3 additions & 3 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ export const clearAll =
dispatch(fromLoader.acClearLoadError())
}

dispatch(fromVisualization.acClear())
dispatch(fromCurrent.acClear())
dispatch(fromVisualization.acClearVisualization())
dispatch(fromCurrent.acClearCurrent())

const rootOrganisationUnits = sGetRootOrgUnits(getState())
const relativePeriod = sGetRelativePeriod(getState())
const digitGroupSeparator = sGetSettingsDigitGroupSeparator(getState())

dispatch(
fromUi.acClear({
fromUi.acClearUi({
rootOrganisationUnits,
relativePeriod,
digitGroupSeparator,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const acSetUi = (value) => ({
value,
})

export const acClear = (value) => ({
export const acClearUi = (value) => ({
type: CLEAR_UI,
value,
})
Expand Down
2 changes: 1 addition & 1 deletion src/actions/visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const acSetVisualization = (visualization) => {
}
}

export const acClear = () => ({
export const acClearVisualization = () => ({
type: CLEAR_VISUALIZATION,
})
Loading

0 comments on commit 823a99b

Please sign in to comment.