From 46fe1f29759c0add3b9ff4529f99c966e08254f7 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Wed, 7 Aug 2024 15:32:03 +0200 Subject: [PATCH] chore: remove d2 --- src/AppWrapper.js | 32 ++++++---- src/components/App.js | 10 ++-- .../DashboardsBar/__tests__/Chip.spec.js | 12 ++-- .../__tests__/DashboardsBar.spec.js | 12 ++-- src/components/Item/AppItem/Item.js | 8 +-- .../Item/AppItem/__tests__/Item.spec.js | 59 ++++++++++--------- src/components/Item/Item.js | 7 +-- src/components/Item/VisualizationItem/Item.js | 4 +- .../Item/VisualizationItem/ItemFooter.js | 13 ++-- .../Visualization/IframePlugin.js | 10 ++-- .../Visualization/LegacyPlugin.js | 1 - .../Visualization/Visualization.js | 6 +- .../__tests__/Visualization.spec.js | 18 +++--- .../VisualizationItem/Visualization/plugin.js | 15 +++-- src/components/__tests__/App.spec.js | 19 +++--- src/modules/useCacheableSection.js | 6 +- src/pages/edit/__tests__/ActionsBar.spec.js | 14 ++--- src/pages/view/CacheableViewDashboard.js | 8 +-- .../FilterBar/__tests__/FilterBadge.spec.js | 12 ++-- .../view/__tests__/ViewDashboard.spec.js | 13 ++-- 20 files changed, 137 insertions(+), 142 deletions(-) diff --git a/src/AppWrapper.js b/src/AppWrapper.js index 5a299321c..1039676c9 100644 --- a/src/AppWrapper.js +++ b/src/AppWrapper.js @@ -13,15 +13,15 @@ import './locales/index.js' const d2Config = { schemas: [ - 'visualization', - 'map', - 'report', - 'eventChart', - 'eventReport', - 'eventVisualization', - 'dashboard', - 'organisationUnit', - 'userGroup', + // 'visualization', + // 'map', + // 'report', + // 'eventChart', + // 'eventReport', + // 'eventVisualization', + // 'dashboard', + // 'organisationUnit', + // 'userGroup', ], } @@ -44,13 +44,25 @@ const query = { apps: { resource: 'apps', }, + currentUser: { + resource: 'me', + params: { + fields: 'id,username,displayName~rename(name),authorities,settings[keyAnalysisDisplayProperty]', + }, + }, } -const providerDataTransformation = ({ rootOrgUnits, apps }) => { +const providerDataTransformation = ({ rootOrgUnits, apps, currentUser }) => { const lineListingApp = apps.find((app) => app.key === 'line-listing') || {} return { rootOrgUnits: rootOrgUnits.organisationUnits, lineListingAppVersion: lineListingApp.version || '0.0.0', + currentUser, + apps, + nameProperty: + currentUser.settings.keyAnalysisDisplayProperty === 'name' + ? 'displayName' + : 'displayShortName', } } diff --git a/src/components/App.js b/src/components/App.js index 38f1c4c6e..28820c851 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,4 +1,4 @@ -import { useD2 } from '@dhis2/app-runtime-adapter-d2' +import { useCachedDataQuery } from '@dhis2/analytics' import { CssVariables } from '@dhis2/ui' import PropTypes from 'prop-types' import React, { useEffect } from 'react' @@ -26,8 +26,8 @@ import 'react-resizable/css/styles.css' import './styles/ItemGrid.css' const App = (props) => { - const { d2 } = useD2() const { systemSettings } = useSystemSettings() + const { currentUser } = useCachedDataQuery() useEffect(() => { props.fetchDashboards() @@ -60,7 +60,7 @@ const App = (props) => { ) : ( ) } @@ -70,7 +70,7 @@ const App = (props) => { path={ROUTE_START_PATH} render={() => ( )} @@ -86,7 +86,7 @@ const App = (props) => { render={(props) => ( )} /> diff --git a/src/components/DashboardsBar/__tests__/Chip.spec.js b/src/components/DashboardsBar/__tests__/Chip.spec.js index 1fd8accc8..4c0dbc9b2 100644 --- a/src/components/DashboardsBar/__tests__/Chip.spec.js +++ b/src/components/DashboardsBar/__tests__/Chip.spec.js @@ -33,13 +33,11 @@ jest.mock('@dhis2/app-runtime', () => ({ useCacheableSection: jest.fn(), })) -jest.mock('@dhis2/app-runtime-adapter-d2', () => ({ - useD2: () => ({ - d2: { - currentUser: { - username: 'rainbowDash', - id: 'r3nb0d5h', - }, +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', }, }), })) diff --git a/src/components/DashboardsBar/__tests__/DashboardsBar.spec.js b/src/components/DashboardsBar/__tests__/DashboardsBar.spec.js index f353b58d3..74ee88d72 100644 --- a/src/components/DashboardsBar/__tests__/DashboardsBar.spec.js +++ b/src/components/DashboardsBar/__tests__/DashboardsBar.spec.js @@ -11,13 +11,11 @@ import DashboardsBar, { MAX_ROW_COUNT, } from '../DashboardsBar.js' -jest.mock('@dhis2/app-runtime-adapter-d2', () => ({ - useD2: () => ({ - d2: { - currentUser: { - username: 'rainbowDash', - id: 'r3nb0d5h', - }, +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', }, }), })) diff --git a/src/components/Item/AppItem/Item.js b/src/components/Item/AppItem/Item.js index 00b0ee3d4..442b38368 100644 --- a/src/components/Item/AppItem/Item.js +++ b/src/components/Item/AppItem/Item.js @@ -1,4 +1,3 @@ -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import { Divider, colors, spacers, IconQuestion24 } from '@dhis2/ui' import PropTypes from 'prop-types' import React from 'react' @@ -11,15 +10,13 @@ import { import ItemHeader from '../ItemHeader/ItemHeader.js' import { getIframeSrc } from './getIframeSrc.js' -const AppItem = ({ dashboardMode, item, itemFilters }) => { - const { d2 } = useD2() - +const AppItem = ({ dashboardMode, item, itemFilters, apps }) => { let appDetails const appKey = item.appKey if (appKey) { - appDetails = d2.system.installedApps.find((app) => app.key === appKey) + appDetails = apps.find((app) => app.key === appKey) } const hideTitle = @@ -70,6 +67,7 @@ const AppItem = ({ dashboardMode, item, itemFilters }) => { } AppItem.propTypes = { + apps: PropTypes.array, dashboardMode: PropTypes.string, item: PropTypes.object, itemFilters: PropTypes.object, diff --git a/src/components/Item/AppItem/__tests__/Item.spec.js b/src/components/Item/AppItem/__tests__/Item.spec.js index 57dbb4990..9beff4a6f 100644 --- a/src/components/Item/AppItem/__tests__/Item.spec.js +++ b/src/components/Item/AppItem/__tests__/Item.spec.js @@ -1,11 +1,18 @@ -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import { render } from '@testing-library/react' import React from 'react' import { Provider } from 'react-redux' import configureMockStore from 'redux-mock-store' import Item from '../Item.js' -jest.mock('@dhis2/app-runtime-adapter-d2') +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', + }, + }), + getDimensionById: jest.fn(), +})) jest.mock('@dhis2/ui', () => { const originalModule = jest.requireActual('@dhis2/ui') @@ -41,29 +48,23 @@ const itemWithoutTitle = { shortened: false, } -useD2.mockReturnValue({ - d2: { - system: { - installedApps: [ - { - key: 'scorecard', - name: 'Scorecard', - launchUrl: 'launchurl', - }, - { - key: 'noTitle', - name: 'No Title', - launchUrl: 'launchurl', - settings: { - dashboardWidget: { - hideTitle: true, - }, - }, - }, - ], +const apps = [ + { + key: 'scorecard', + name: 'Scorecard', + launchUrl: 'launchurl', + }, + { + key: 'noTitle', + name: 'No Title', + launchUrl: 'launchurl', + settings: { + dashboardWidget: { + hideTitle: true, + }, }, }, -}) +] test('renders a valid App item in view mode', () => { const store = { @@ -71,7 +72,7 @@ test('renders a valid App item in view mode', () => { } const { container } = render( - + ) expect(container).toMatchSnapshot() @@ -86,7 +87,7 @@ test('renders a valid App item with filter in view mode', () => { const { container } = render( - + ) expect(container).toMatchSnapshot() @@ -101,7 +102,7 @@ test('renders a valid App item with filter in edit mode', () => { const { container } = render( - + ) expect(container).toMatchSnapshot() @@ -114,7 +115,7 @@ test('renders a valid App item without title in view mode if specified in app se const { container } = render( - + ) expect(container).toMatchSnapshot() @@ -127,7 +128,7 @@ test('renders a valid App item with title in edit mode irrespective of app setti const { container } = render( - + ) expect(container).toMatchSnapshot() @@ -148,7 +149,7 @@ test('renders an invalid App item', () => { const { container } = render( - + ) expect(container).toMatchSnapshot() diff --git a/src/components/Item/Item.js b/src/components/Item/Item.js index b4369ce29..283b68ed2 100644 --- a/src/components/Item/Item.js +++ b/src/components/Item/Item.js @@ -1,4 +1,4 @@ -import { useD2 } from '@dhis2/app-runtime-adapter-d2' +import { useCachedDataQuery } from '@dhis2/analytics' import PropTypes from 'prop-types' import React from 'react' import { @@ -61,11 +61,10 @@ const getGridItem = (type) => { } export const Item = (props) => { - const { d2 } = useD2() - + const { apps } = useCachedDataQuery() const GridItem = getGridItem(props.item.type) - return + return } Item.propTypes = { diff --git a/src/components/Item/VisualizationItem/Item.js b/src/components/Item/VisualizationItem/Item.js index f17f235bf..3ccf96ce5 100644 --- a/src/components/Item/VisualizationItem/Item.js +++ b/src/components/Item/VisualizationItem/Item.js @@ -208,7 +208,7 @@ class Item extends Component { const activeType = this.getActiveType() const actionButtons = - pluginIsAvailable(activeType || item.type, this.props.d2) && + pluginIsAvailable(activeType || item.type, this.props.apps) && isViewMode(dashboardMode) ? ( { const { baseUrl } = useConfig() const [interpretationId, setInterpretationId] = useState(null) const [replyInitialFocus, setReplyInitialFocus] = useState(false) - const { d2 } = useD2() + const { currentUser } = useCachedDataQuery() const setReplyToInterpretation = (id) => { setInterpretationId(id) @@ -48,7 +51,7 @@ const ItemFooter = ({ item }) => { /> {interpretationId ? ( { /> ) : ( { const dispatch = useDispatch() const iframePluginStatus = useSelector(sGetIframePluginStatus) - - const { d2 } = useD2() const { baseUrl } = useConfig() - const { userSettings } = useUserSettings() const iframeRef = useRef() const [error, setError] = useState(null) + const { apps } = useCachedDataQuery() // When this mounts, check if the dashboard is recording const { isCached, recordingState } = useCacheableSection(dashboardId) @@ -98,14 +96,14 @@ const IframePlugin = ({ // 2. check if there is an installed app for the pluginType // and use its plugin launch URL - const pluginLaunchUrl = getPluginLaunchUrl(pluginType, d2, baseUrl) + const pluginLaunchUrl = getPluginLaunchUrl(pluginType, apps, baseUrl) if (pluginLaunchUrl) { return pluginLaunchUrl } setError('missing-plugin') - }, [d2, baseUrl, pluginType]) + }, [apps, baseUrl, pluginType]) const iframeSrc = getIframeSrc() diff --git a/src/components/Item/VisualizationItem/Visualization/LegacyPlugin.js b/src/components/Item/VisualizationItem/Visualization/LegacyPlugin.js index dc8c93449..28a9a9dff 100644 --- a/src/components/Item/VisualizationItem/Visualization/LegacyPlugin.js +++ b/src/components/Item/VisualizationItem/Visualization/LegacyPlugin.js @@ -47,7 +47,6 @@ const LegacyPlugin = ({ auth: d2.Api.getApi().defaultHeaders.Authorization, }, activeType, - d2, options, }) } diff --git a/src/components/Item/VisualizationItem/Visualization/Visualization.js b/src/components/Item/VisualizationItem/Visualization/Visualization.js index cc840a049..33e5a6fb7 100644 --- a/src/components/Item/VisualizationItem/Visualization/Visualization.js +++ b/src/components/Item/VisualizationItem/Visualization/Visualization.js @@ -1,6 +1,5 @@ import { useCachedDataQuery } from '@dhis2/analytics' import { useDhis2ConnectionStatus } from '@dhis2/app-runtime' -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import i18n from '@dhis2/d2-i18n' import { Button, Cover, IconInfo24, IconWarning24, colors } from '@dhis2/ui' import uniqueId from 'lodash/uniqueId.js' @@ -43,10 +42,9 @@ const Visualization = ({ onClickNoFiltersOverlay, ...rest }) => { - const { d2 } = useD2() const dashboardId = useSelector(sGetSelectedId) const { isDisconnected: offline } = useDhis2ConnectionStatus() - const { lineListingAppVersion } = useCachedDataQuery() + const { lineListingAppVersion, apps } = useCachedDataQuery() // NOTE: // The following is all memoized because the IframePlugin (and potentially others) @@ -197,7 +195,7 @@ const Visualization = ({ ) } default: { - return !pluginIsAvailable(activeType || item.type, d2) ? ( + return !pluginIsAvailable(activeType || item.type, apps) ? (
diff --git a/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js b/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js index 57901ef4b..d9281decd 100644 --- a/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js +++ b/src/components/Item/VisualizationItem/Visualization/__tests__/Visualization.spec.js @@ -4,16 +4,14 @@ import { Provider } from 'react-redux' import configureMockStore from 'redux-mock-store' import Visualization from '../Visualization.js' -jest.mock('@dhis2/app-runtime-adapter-d2', () => { - return { - useD2: jest.fn(() => ({ - d2: { - currentUser: { username: 'rainbowDash' }, - system: { installedApps: {} }, - }, - })), - } -}) +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', + }, + }), +})) jest.mock( '../LegacyPlugin', diff --git a/src/components/Item/VisualizationItem/Visualization/plugin.js b/src/components/Item/VisualizationItem/Visualization/plugin.js index 28cad71b6..ee3acd610 100644 --- a/src/components/Item/VisualizationItem/Visualization/plugin.js +++ b/src/components/Item/VisualizationItem/Visualization/plugin.js @@ -24,10 +24,9 @@ const itemTypeToScriptPath = { const hasIntegratedPlugin = (type) => [CHART, REPORT_TABLE, VISUALIZATION, MAP].includes(type) -export const getPluginLaunchUrl = (type, d2, baseUrl) => { +export const getPluginLaunchUrl = (type, apps, baseUrl) => { // 1. lookup in api/apps for the "manually installed" app, this can be a new version for a core (bundled) app // 2. fallback to default hardcoded path for the core (bundled) apps - const apps = d2.system.installedApps const appKey = itemTypeMap[type].appKey const appDetails = appKey && apps.find((app) => app.key === appKey) @@ -89,13 +88,13 @@ const fetchPlugin = async (type, baseUrl) => { return await scriptsPromise } -export const pluginIsAvailable = (type, d2) => +export const pluginIsAvailable = (type, apps) => hasIntegratedPlugin(type) || - Boolean(getPluginLaunchUrl(type, d2)) || + Boolean(getPluginLaunchUrl(type, apps)) || Boolean(itemTypeToGlobalVariable[type]) -const loadPlugin = async ({ type, config, credentials, d2 }) => { - if (!pluginIsAvailable(type, d2)) { +const loadPlugin = async ({ type, config, credentials }) => { + if (!pluginIsAvailable(type)) { return } @@ -117,7 +116,7 @@ const loadPlugin = async ({ type, config, credentials, d2 }) => { export const load = async ( item, visualization, - { credentials, activeType, d2, options = {} } + { credentials, activeType, options = {} } ) => { const config = { ...visualization, @@ -126,7 +125,7 @@ export const load = async ( } const type = activeType || item.type - await loadPlugin({ type, config, credentials, d2 }) + await loadPlugin({ type, config, credentials }) } export const unmount = async (item, activeType) => { diff --git a/src/components/__tests__/App.spec.js b/src/components/__tests__/App.spec.js index 11aeee081..2850169f8 100644 --- a/src/components/__tests__/App.spec.js +++ b/src/components/__tests__/App.spec.js @@ -8,7 +8,15 @@ import { apiFetchDashboards } from '../../api/fetchAllDashboards.js' import App from '../App.js' import { useSystemSettings } from '../SystemSettingsProvider.js' -jest.mock('@dhis2/analytics') +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', + }, + }), + getDimensionById: jest.fn(), +})) jest.mock('@dhis2/app-runtime', () => ({ useDhis2ConnectionStatus: jest.fn(() => ({ isConnected: true, @@ -52,15 +60,6 @@ jest.mock( } ) -jest.mock('@dhis2/app-runtime-adapter-d2', () => { - return { - useD2: jest.fn(() => ({ - d2: { - currentUser: { username: 'rainbowDash' }, - }, - })), - } -}) jest.mock('../../pages/view', () => { return { ViewDashboard: function Mock() { diff --git a/src/modules/useCacheableSection.js b/src/modules/useCacheableSection.js index 1c4521392..f68d9884d 100644 --- a/src/modules/useCacheableSection.js +++ b/src/modules/useCacheableSection.js @@ -1,12 +1,12 @@ +import { useCachedDataQuery } from '@dhis2/analytics' import { useCacheableSection as useCacheableSectionAppRuntime } from '@dhis2/app-runtime' -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import getCacheableSectionId from './getCacheableSectionId.js' export const useCacheableSection = (dashboardId) => { - const { d2 } = useD2() + const { currentUser } = useCachedDataQuery() const cacheableSectionProps = useCacheableSectionAppRuntime( - getCacheableSectionId(d2.currentUser.id, dashboardId) + getCacheableSectionId(currentUser.id, dashboardId) ) return { ...cacheableSectionProps } } diff --git a/src/pages/edit/__tests__/ActionsBar.spec.js b/src/pages/edit/__tests__/ActionsBar.spec.js index c076036fb..b3332f910 100644 --- a/src/pages/edit/__tests__/ActionsBar.spec.js +++ b/src/pages/edit/__tests__/ActionsBar.spec.js @@ -1,4 +1,3 @@ -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import { render } from '@testing-library/react' import React from 'react' import { Provider } from 'react-redux' @@ -8,7 +7,6 @@ import ActionsBar from '../ActionsBar.js' const mockStore = configureMockStore() -jest.mock('@dhis2/app-runtime-adapter-d2') jest.mock('@dhis2/app-runtime') /* eslint-disable react/prop-types */ @@ -38,6 +36,12 @@ jest.mock('@dhis2/analytics', () => { OfflineTooltip: function Mock({ children }) { return
{children}
}, + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', + }, + }), } }) /* eslint-enable react/prop-types */ @@ -60,12 +64,6 @@ jest.mock( ) /* eslint-enable react/prop-types */ -useD2.mockReturnValue({ - d2: { - currentUser: 'rainbowDash', - }, -}) - jest.mock('@dhis2/app-runtime', () => ({ useDhis2ConnectionStatus: jest.fn(() => ({ isConnected: true, diff --git a/src/pages/view/CacheableViewDashboard.js b/src/pages/view/CacheableViewDashboard.js index f2fade1b1..27e43f322 100644 --- a/src/pages/view/CacheableViewDashboard.js +++ b/src/pages/view/CacheableViewDashboard.js @@ -1,5 +1,5 @@ +import { useCachedDataQuery } from '@dhis2/analytics' import { CacheableSection } from '@dhis2/app-runtime' -import { useD2 } from '@dhis2/app-runtime-adapter-d2' import i18n from '@dhis2/d2-i18n' import isEmpty from 'lodash/isEmpty.js' import PropTypes from 'prop-types' @@ -23,7 +23,7 @@ const CacheableViewDashboard = ({ dashboardsIsEmpty, }) => { const [dashboardsBarExpanded, setDashboardsBarExpanded] = useState(false) - const { d2 } = useD2() + const { currentUser } = useCachedDataQuery() if (!dashboardsLoaded) { return @@ -51,14 +51,14 @@ const CacheableViewDashboard = ({ ) } - const cacheSectionId = getCacheableSectionId(d2.currentUser.id, id) + const cacheSectionId = getCacheableSectionId(currentUser.id, id) return ( }> ) diff --git a/src/pages/view/FilterBar/__tests__/FilterBadge.spec.js b/src/pages/view/FilterBar/__tests__/FilterBadge.spec.js index dc55c9091..1164dca2d 100644 --- a/src/pages/view/FilterBar/__tests__/FilterBadge.spec.js +++ b/src/pages/view/FilterBar/__tests__/FilterBadge.spec.js @@ -8,13 +8,11 @@ const mockStore = configureMockStore() const store = { selected: { id: 'dashboard1' } } -jest.mock('@dhis2/app-runtime-adapter-d2', () => ({ - useD2: () => ({ - d2: { - currentUser: { - username: 'rainbowDash', - id: 'r3nb0d5h', - }, +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', }, }), })) diff --git a/src/pages/view/__tests__/ViewDashboard.spec.js b/src/pages/view/__tests__/ViewDashboard.spec.js index 5ac6c8791..2dfa67dbf 100644 --- a/src/pages/view/__tests__/ViewDashboard.spec.js +++ b/src/pages/view/__tests__/ViewDashboard.spec.js @@ -8,15 +8,14 @@ import { apiPostDataStatistics } from '../../../api/dataStatistics.js' import { apiFetchDashboard } from '../../../api/fetchDashboard.js' import ViewDashboard from '../ViewDashboard.js' -jest.mock('@dhis2/app-runtime-adapter-d2', () => ({ - useD2: () => ({ - d2: { - currentUser: { - username: 'rainbowDash', - id: 'r3nb0d5h', - }, +jest.mock('@dhis2/analytics', () => ({ + useCachedDataQuery: () => ({ + currentUser: { + username: 'rainbowDash', + id: 'r3nb0d5h', }, }), + getDimensionById: jest.fn(), })) jest.mock('@dhis2/app-runtime', () => ({