Skip to content

Commit

Permalink
fix: local offline handling [DHIS2-12937] (#2230)
Browse files Browse the repository at this point in the history
* fix: migrate to useDhis2ConnectionStatus

* chore: update @dhis2/analytics for offline support

---------

Co-authored-by: Edoardo Sabadelli <[email protected]>
  • Loading branch information
2 people authored and jenniferarnesen committed Mar 22, 2023
1 parent 964e007 commit bdcf374
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 112 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"private": true,
"license": "BSD-3-Clause",
"dependencies": {
"@dhis2/analytics": "^24.2.5",
"@dhis2/app-runtime": "^3.7.0",
"@dhis2/analytics": "^24.9.2",
"@dhis2/app-runtime": "^3.9.0",
"@dhis2/app-runtime-adapter-d2": "^1.1.0",
"@dhis2/d2-i18n": "^1.1.1",
"@dhis2/d2-ui-core": "^7.4.1",
Expand Down Expand Up @@ -51,7 +51,7 @@
"cy:capture": "cypress_dhis2_api_stub_mode=CAPTURE yarn d2-utils-cypress run --appStart 'yarn cypress:start'"
},
"devDependencies": {
"@dhis2/cli-app-scripts": "^10.2.3",
"@dhis2/cli-app-scripts": "^10.3.0",
"@dhis2/cli-style": "^10.4.1",
"@dhis2/cli-utils-cypress": "^7.0.1",
"@dhis2/cypress-commands": "^7.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/components/DashboardsBar/Chip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import { Chip as UiChip, colors, IconStarFilled24 } from '@dhis2/ui'
import cx from 'classnames'
import debounce from 'lodash/debounce.js'
Expand All @@ -12,7 +12,7 @@ import classes from './styles/Chip.module.css'

const Chip = ({ starred, selected, label, dashboardId, onClick }) => {
const { lastUpdated } = useCacheableSection(dashboardId)
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()
const chipProps = {
selected,
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/DashboardsBar/Content.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { Button, ComponentCover, Tooltip, IconAdd24 } from '@dhis2/ui'
import cx from 'classnames'
Expand All @@ -24,7 +24,7 @@ const Content = ({
onSearchClicked,
}) => {
const [redirectUrl, setRedirectUrl] = useState(null)
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()

const onSelectDashboard = () => {
const id = getFilteredDashboards(dashboards, filterText)[0]?.id
Expand Down
2 changes: 1 addition & 1 deletion src/components/DashboardsBar/__tests__/Chip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jest.mock('@dhis2/ui', () => {
/* eslint-enable react/prop-types */

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: () => ({ online: true }),
useDhis2ConnectionStatus: () => ({ isConnected: true }),
useCacheableSection: jest.fn(),
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const dashboards = {
}

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: () => ({ online: true }),
useDhis2ConnectionStatus: () => ({ isConnected: true }),
useCacheableSection: jest.fn(() => ({
isCached: false,
recordingState: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ jest.mock('../../../../SystemSettingsProvider', () => {
})

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: jest.fn(() => ({ online: false, offline: true })),
useDhis2ConnectionStatus: jest.fn(() => ({
isConnected: false,
isDisconnected: true,
})),
useConfig: jest.fn(() => ({ baseUrl: 'dhis2' })),
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ jest.mock('../../../../SystemSettingsProvider', () => {
})

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: jest.fn(() => ({ online: true, offline: false })),
useDhis2ConnectionStatus: jest.fn(() => ({
isConnected: true,
isDisconnected: false,
})),
useConfig: jest.fn(() => ({ baseUrl: 'dhis2' })),
}))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import { render } from '@testing-library/react'
import React from 'react'
import {
Expand All @@ -11,25 +11,28 @@ import {
import ViewAsMenuItems from '../ViewAsMenuItems.js'

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: jest.fn(() => ({ online: true, offline: false })),
useDhis2ConnectionStatus: jest.fn(() => ({
isConnected: true,
isDisconnected: false,
})),
}))

const offline = {
online: false,
offline: true,
isConnected: false,
isDisconnected: true,
}

const online = {
online: true,
offline: false,
isConnected: true,
isDisconnected: false,
}

const defaultProps = {
onActiveTypeChanged: jest.fn(),
}

test('renders menu for active type MAP and type CHART', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: CHART,
activeType: MAP,
Expand All @@ -41,7 +44,7 @@ test('renders menu for active type MAP and type CHART', async () => {
})

test('renders disabled menu items when offline', () => {
useOnlineStatus.mockImplementation(jest.fn(() => offline))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => offline))

const props = Object.assign({}, defaultProps, {
type: CHART,
Expand All @@ -53,7 +56,7 @@ test('renders disabled menu items when offline', () => {
})

test('renders menu for active type CHART and type MAP', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: MAP,
activeType: CHART,
Expand All @@ -68,7 +71,7 @@ test('renders menu for active type CHART and type MAP', async () => {
})

test('renders menu for active type MAP and type MAP without Thematic layer', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: MAP,
activeType: MAP,
Expand All @@ -83,7 +86,7 @@ test('renders menu for active type MAP and type MAP without Thematic layer', asy
})

test('renders menu for active type MAP and type MAP without Thematic layer when offline', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => offline))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => offline))
const props = Object.assign({}, defaultProps, {
type: MAP,
activeType: MAP,
Expand All @@ -98,7 +101,7 @@ test('renders menu for active type MAP and type MAP without Thematic layer when
})

test('renders menu for active type REPORT_TABLE and type CHART', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: CHART,
activeType: REPORT_TABLE,
Expand All @@ -111,7 +114,7 @@ test('renders menu for active type REPORT_TABLE and type CHART', async () => {
})

test('renders menu for active type CHART and type REPORT_TABLE', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: REPORT_TABLE,
activeType: CHART,
Expand All @@ -124,7 +127,7 @@ test('renders menu for active type CHART and type REPORT_TABLE', async () => {
})

test('renders menu for active type EVENT_REPORT and type EVENT_CHART', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: EVENT_CHART,
activeType: EVENT_REPORT,
Expand All @@ -137,7 +140,7 @@ test('renders menu for active type EVENT_REPORT and type EVENT_CHART', async ()
})

test('renders menu for active type EVENT_CHART and type EVENT_REPORT', async () => {
useOnlineStatus.mockImplementation(jest.fn(() => online))
useDhis2ConnectionStatus.mockImplementation(jest.fn(() => online))
const props = Object.assign({}, defaultProps, {
type: EVENT_REPORT,
activeType: EVENT_CHART,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Item/VisualizationItem/ItemFooter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
import i18n from '@dhis2/d2-i18n'
import InterpretationsComponent from '@dhis2/d2-ui-interpretations'
Expand All @@ -10,7 +10,7 @@ import classes from './styles/ItemFooter.module.css'

const ItemFooter = (props) => {
const { d2 } = useD2()
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()

return (
<div className={classes.itemFooter} data-test="dashboarditem-footer">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import PropTypes from 'prop-types'
import React from 'react'
Expand All @@ -8,7 +8,7 @@ import NoVisualizationMessage from './NoVisualizationMessage.js'
const mapViewIsEELayer = (mapView) => mapView.layer.includes('earthEngine')

const MapPlugin = ({ visualization, style, ...pluginProps }) => {
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()

if (offline && visualization.mapViews?.find(mapViewIsEELayer)) {
return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/MenuItemWithTooltip.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OfflineTooltip } from '@dhis2/analytics'
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { MenuItem } from '@dhis2/ui'
import PropTypes from 'prop-types'
Expand All @@ -12,7 +12,7 @@ const MenuItemWithTooltip = ({
disabled,
...rest
}) => {
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()

const tooltipContent =
disabledWhenOffline && offline
Expand Down
5 changes: 4 additions & 1 deletion src/components/__tests__/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { useSystemSettings } from '../SystemSettingsProvider.js'

jest.mock('@dhis2/analytics')
jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: jest.fn(() => ({ online: true, offline: false })),
useDhis2ConnectionStatus: jest.fn(() => ({
isConnected: true,
isDisconnected: false,
})),
useDataEngine: jest.fn(() => ({ query: Function.prototype })),
useCacheableSection: jest.fn,
}))
Expand Down
8 changes: 6 additions & 2 deletions src/pages/edit/ActionsBar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { OfflineTooltip, TranslationDialog } from '@dhis2/analytics'
import { useOnlineStatus, useDataEngine, useAlert } from '@dhis2/app-runtime'
import {
useDhis2ConnectionStatus,
useDataEngine,
useAlert,
} from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { Button, ButtonStrip } from '@dhis2/ui'
import PropTypes from 'prop-types'
Expand Down Expand Up @@ -43,7 +47,7 @@ const fieldsToTranslate = ['name', 'description']

const EditBar = ({ dashboard, ...props }) => {
const dataEngine = useDataEngine()
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()
const [translationDlgIsOpen, setTranslationDlgIsOpen] = useState(false)
const [filterSettingsDlgIsOpen, setFilterSettingsDlgIsOpen] =
useState(false)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/edit/ItemSelector/ItemSearchField.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { Input, Tooltip } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import classes from './styles/ItemSearchField.module.css'

const ItemSearchField = (props) => {
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()

const getInput = () => (
// autoComplete not supported in ui@6, remove this form after upgrade
Expand Down
4 changes: 2 additions & 2 deletions src/pages/edit/LayoutModal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OfflineTooltip } from '@dhis2/analytics'
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import {
Modal,
Expand Down Expand Up @@ -27,7 +27,7 @@ const getColsSaveValue = (value) =>
value === '' ? DEFAULT_COLUMNS : parseInt(value, 10)

export const LayoutModal = ({ columns, onSaveLayout, onClose }) => {
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()
const [cols, setCols] = useState(columns)

useEffect(() => setCols(columns), [])
Expand Down
4 changes: 2 additions & 2 deletions src/pages/edit/TitleBar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OfflineTooltip } from '@dhis2/analytics'
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { InputField, TextAreaField, Radio, Button } from '@dhis2/ui'
import PropTypes from 'prop-types'
Expand Down Expand Up @@ -37,7 +37,7 @@ const EditTitleBar = ({
onChangeDescription,
onSaveLayout,
}) => {
const { offline } = useOnlineStatus()
const { isDisconnected: offline } = useDhis2ConnectionStatus()

const updateTitle = (_, e) => {
onChangeTitle(e.target.value)
Expand Down
5 changes: 4 additions & 1 deletion src/pages/edit/__tests__/ActionsBar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ useD2.mockReturnValue({
})

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: jest.fn(() => ({ online: true, offline: false })),
useDhis2ConnectionStatus: jest.fn(() => ({
isConnected: true,
isDisconnected: false,
})),
useDataEngine: jest.fn(() => ({ dataEngine: {} })),
useAlert: jest.fn(() => ({})),
}))
Expand Down
2 changes: 1 addition & 1 deletion src/pages/edit/__tests__/FilterSettingsDialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import FilterSettingsDialog from '../FilterSettingsDialog.js'

jest.mock('@dhis2/app-runtime', () => ({
useOnlineStatus: () => ({ online: true }),
useDhis2ConnectionStatus: () => ({ isConnected: true }),
}))

/* eslint-disable react/prop-types */
Expand Down
4 changes: 2 additions & 2 deletions src/pages/start/StartScreen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useDataEngine, useOnlineStatus } from '@dhis2/app-runtime'
import { useDataEngine, useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
Expand All @@ -9,7 +9,7 @@ import styles from './styles/StartScreen.module.css'
const StartScreen = ({ username }) => {
const [mostViewedDashboards, setMostViewedDashboards] = useState([])
const dataEngine = useDataEngine()
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()

useEffect(() => {
async function populateMostViewedDashboards(dataEngine) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/view/FilterBar/FilterBadge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { Tooltip } from '@dhis2/ui'
import cx from 'classnames'
Expand All @@ -11,7 +11,7 @@ import { sGetSelectedId } from '../../../reducers/selected.js'
import classes from './styles/FilterBadge.module.css'

const FilterBadge = ({ dashboardId, filter, openFilterModal, onRemove }) => {
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()
const { isCached } = useCacheableSection(dashboardId)

const notAllowed = !isCached && !online
Expand Down
4 changes: 2 additions & 2 deletions src/pages/view/FilterBar/FilterBar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnlineStatus } from '@dhis2/app-runtime'
import { useDhis2ConnectionStatus } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import PropTypes from 'prop-types'
import React, { useState } from 'react'
Expand All @@ -13,7 +13,7 @@ import FilterBadge from './FilterBadge.js'
import classes from './styles/FilterBar.module.css'

const FilterBar = ({ filters, removeFilter, removeAllFilters }) => {
const { online } = useOnlineStatus()
const { isConnected: online } = useDhis2ConnectionStatus()
const [dialogIsOpen, setDialogIsOpen] = useState(false)

const onRemoveFilter = (filterId) => {
Expand Down
Loading

0 comments on commit bdcf374

Please sign in to comment.