From 36eec715efdbfd55eae392928cdad91c2acf8a29 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Wed, 17 Mar 2021 15:14:05 +0100 Subject: [PATCH] fix: only request dimensions when needed (#1654) This fix decreases the initial api requests being made when the dashboard first loads. There's no need to request dimensions unless they are needed for filtering the dashboard or setting filter settings while editing a dashboard. --- src/components/App.js | 28 ------------ src/components/ControlBar/EditBar.js | 4 -- .../ControlBar/__tests__/EditBar.spec.js | 8 ++++ .../__snapshots__/EditBar.spec.js.snap | 3 ++ src/components/ItemFilter/FilterSelector.js | 15 +++---- .../ItemFilter/FilterSettingsDialog.js | 4 +- .../__tests__/FilterSettingsDialog.spec.js | 18 +++++--- src/modules/useDimensions.js | 44 +++++++++++++++++++ src/reducers/dimensions.js | 8 ++-- 9 files changed, 79 insertions(+), 53 deletions(-) create mode 100644 src/modules/useDimensions.js diff --git a/src/components/App.js b/src/components/App.js index 782af3d68..536ddcc86 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -4,19 +4,14 @@ import { connect } from 'react-redux' import PropTypes from 'prop-types' import { CssVariables } from '@dhis2/ui' import { useD2 } from '@dhis2/app-runtime-adapter-d2' -import { apiFetchDimensions } from '@dhis2/analytics' -import { useDataEngine } from '@dhis2/app-runtime' import Dashboard from './Dashboard/Dashboard' import AlertBar from './AlertBar/AlertBar' -import { useUserSettings } from './UserSettingsProvider' import { acReceivedUser } from '../actions/user' import { tFetchDashboards } from '../actions/dashboards' import { tSetControlBarRows } from '../actions/controlBar' import { tSetShowDescription } from '../actions/selected' -import { acSetDimensions } from '../actions/dimensions' -import getFilteredDimensions from '../modules/getFilteredDimensions' import { EDIT, @@ -30,8 +25,6 @@ import './App.css' const App = props => { const { d2 } = useD2() - const dataEngine = useDataEngine() - const { userSettings } = useUserSettings() useEffect(() => { props.setCurrentUser(d2.currentUser) @@ -50,25 +43,6 @@ const App = props => { ) }, []) - useEffect(() => { - const fetchDimensions = async () => { - try { - const dimensions = await apiFetchDimensions( - dataEngine, - userSettings.keyAnalysisDisplayProperty - ) - - props.setDimensions(getFilteredDimensions(dimensions)) - } catch (e) { - console.error(e) - } - } - - if (userSettings.keyAnalysisDisplayProperty) { - fetchDimensions() - } - }, [userSettings]) - return ( <> @@ -117,7 +91,6 @@ App.propTypes = { fetchDashboards: PropTypes.func.isRequired, setControlBarRows: PropTypes.func.isRequired, setCurrentUser: PropTypes.func.isRequired, - setDimensions: PropTypes.func.isRequired, setShowDescription: PropTypes.func.isRequired, } @@ -125,7 +98,6 @@ const mapDispatchToProps = { fetchDashboards: tFetchDashboards, setControlBarRows: tSetControlBarRows, setCurrentUser: acReceivedUser, - setDimensions: acSetDimensions, setShowDescription: tSetShowDescription, } diff --git a/src/components/ControlBar/EditBar.js b/src/components/ControlBar/EditBar.js index a46782497..55fb81077 100644 --- a/src/components/ControlBar/EditBar.js +++ b/src/components/ControlBar/EditBar.js @@ -19,7 +19,6 @@ import { } from '../../actions/editDashboard' import { acClearPrintDashboard } from '../../actions/printDashboard' import { tDeleteDashboard } from '../../actions/dashboards' -import { sGetDimensions } from '../../reducers/dimensions' import { sGetEditDashboardRoot, sGetIsNewDashboard, @@ -152,7 +151,6 @@ const EditBar = props => { const filterSettingsDialog = () => dashboard || props.newDashboard ? ( { dashboardId: dashboard.id, dashboardName: dashboard.name, deleteAccess, - dimensions: sGetDimensions(state), newDashboard, restrictFilters: dashboard.restrictFilters, isPrintPreviewView: sGetIsPrintPreviewView(state), diff --git a/src/components/ControlBar/__tests__/EditBar.spec.js b/src/components/ControlBar/__tests__/EditBar.spec.js index 1a9a7e9af..69c1a01a7 100644 --- a/src/components/ControlBar/__tests__/EditBar.spec.js +++ b/src/components/ControlBar/__tests__/EditBar.spec.js @@ -18,6 +18,14 @@ jest.mock('@dhis2/app-runtime-adapter-d2') jest.mock('@dhis2/app-runtime') jest.mock('../../../api/fetchDashboard') +jest.mock( + '../../ItemFilter/FilterSettingsDialog', + () => + function MockFilterSettingsDialog() { + return
+ } +) + jest.mock( '@dhis2/d2-ui-translation-dialog', () => diff --git a/src/components/ControlBar/__tests__/__snapshots__/EditBar.spec.js.snap b/src/components/ControlBar/__tests__/__snapshots__/EditBar.spec.js.snap index 222544ed3..962b3e7a0 100644 --- a/src/components/ControlBar/__tests__/__snapshots__/EditBar.spec.js.snap +++ b/src/components/ControlBar/__tests__/__snapshots__/EditBar.spec.js.snap @@ -62,6 +62,9 @@ exports[`renders Save and Discard buttons but no dialogs when no dashboard id 1`
+
`; diff --git a/src/components/ItemFilter/FilterSelector.js b/src/components/ItemFilter/FilterSelector.js index 0f44a7a02..55264cd41 100644 --- a/src/components/ItemFilter/FilterSelector.js +++ b/src/components/ItemFilter/FilterSelector.js @@ -3,25 +3,24 @@ import PropTypes from 'prop-types' import { connect } from 'react-redux' import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown' import isEmpty from 'lodash/isEmpty' - import i18n from '@dhis2/d2-i18n' import { DimensionsPanel } from '@dhis2/analytics' - import { Button, Popover } from '@dhis2/ui' import FilterDialog from './FilterDialog' import { sGetActiveModalDimension } from '../../reducers/activeModalDimension' -import { sGetDimensions } from '../../reducers/dimensions' import { sGetItemFiltersRoot } from '../../reducers/itemFilters' import { acClearActiveModalDimension, acSetActiveModalDimension, } from '../../actions/activeModalDimension' +import useDimensions from '../../modules/useDimensions' import classes from './styles/FilterSelector.module.css' const FilterSelector = props => { const [showPopover, setShowPopover] = useState(false) + const dimensions = useDimensions(showPopover) const ref = useRef(null) @@ -33,21 +32,21 @@ const FilterSelector = props => { const selectDimension = id => { props.setActiveModalDimension( - props.dimensions.find(dimension => dimension.id === id) + dimensions.find(dimension => dimension.id === id) ) } const filterDimensions = () => { if (!props.restrictFilters) { - return props.dimensions + return dimensions } else { - return props.dimensions.filter(d => + return dimensions.filter(d => [...props.allowedFilters].includes(d.id) ) } } - return isEmpty(filterDimensions()) ? null : ( + return props.restrictFilters && !props.allowedFilters ? null : ( <>