-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pwa): track online status [LIBS-315] (#718)
* fix: migrate to IDB to store dhis2 base url * refactor: init function * fix: add complete env to dev SW * refactor: get base url api * feat(pwa): use SW to track dhis2 server connection (most strategies) * refactor(sw): convert recording mode to strategy & use status plugin * refactor: rename online status to dhis2 connection status for clarity * fix(deps): use latest app-runtime * chore(pwa-app): add request tester * feat(offline-interface): add subscription to connection status from SW * chore(pwa-app): temporarily remove dependency * fix: fallback to localStorage for baseUrl to avoid breaking change * chore: format * chore: formatting * chore: add some tools for testing connection status * fix: exclude pings from cache * fix: treat 401 as disconnected * fix: don't check auth * refactor: make reusable getAllClientsInScope fn * refactor: clean up * fix: throttle status broadcasts * fix: use shorter isConnected name * chore: rename set-up-service-worker * fix: change subscription callback name * chore: some more dev utils * chore: satisfy linter * chore: update comments * refactor: clarify systemInfo and baseUrl states * chore: clarify todos * chore: update comment * fix: wait for offline interface to be ready for connection status * chore: fix typo * chore: update temp test scripts * chore: remove console logs * chore: clear up test scripts * fix(sw): use NetworkOnly for new ping endpoint * feat: use connection status from app runtime
- Loading branch information
1 parent
cba768a
commit 1dfd1e6
Showing
17 changed files
with
455 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useDataEngine, useDhis2ConnectionStatus } from '@dhis2/app-runtime' | ||
import { Box, Button, ButtonStrip, Help } from '@dhis2/ui' | ||
import React from 'react' | ||
|
||
const query = { | ||
me: { | ||
resource: 'me', | ||
params: { | ||
fields: ['id', 'name'], | ||
}, | ||
}, | ||
} | ||
|
||
export default function RequestTester() { | ||
const engine = useDataEngine() | ||
const { isConnected, lastConnected } = useDhis2ConnectionStatus() | ||
|
||
const internalRequest = () => { | ||
console.log('Request tester: internal request') | ||
engine.query(query) | ||
} | ||
const externalRequest = () => { | ||
console.log('Request tester: external request') | ||
fetch('https://random.dog/woof.json') | ||
} | ||
|
||
return ( | ||
<div> | ||
<div> | ||
Connection to DHIS2 server:{' '} | ||
{isConnected ? ( | ||
<span style={{ color: 'green' }}>Connected</span> | ||
) : ( | ||
<span style={{ color: 'red' }}>NOT CONNECTED</span> | ||
)} | ||
</div> | ||
<div> | ||
Last connected: {lastConnected?.toLocaleTimeString() || 'null'} | ||
</div> | ||
<Help>Based on useDhis2ConnectionStatus()</Help> | ||
<Box marginTop={'12px'}> | ||
<ButtonStrip> | ||
<Button onClick={internalRequest}> | ||
Query DHIS2 server | ||
</Button> | ||
<Button onClick={externalRequest}> | ||
Query external server | ||
</Button> | ||
</ButtonStrip> | ||
</Box> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { openDB /* deleteDB */ } from 'idb' | ||
|
||
export const BASE_URL_DB = 'dhis2-base-url-db' | ||
export const BASE_URL_STORE = 'dhis2-base-url-store' | ||
|
||
const DB_VERSION = 1 | ||
|
||
/** | ||
* Opens indexed DB and object store for baser urls by app name. Should be used any | ||
* time the DB is accessed to make sure object stores are set up correctly and | ||
* avoid DB-access race condition on first installation. | ||
* | ||
* @returns {Promise} dbPromise. Usage: `const db = await dbPromise` | ||
*/ | ||
function openBaseUrlsDB() { | ||
return openDB(BASE_URL_DB, DB_VERSION, { | ||
upgrade(db, oldVersion /* newVersion, transaction */) { | ||
// DB versioning trick that can iteratively apply upgrades | ||
// https://developers.google.com/web/ilt/pwa/working-with-indexeddb#using_database_versioning | ||
switch (oldVersion) { | ||
case 0: { | ||
db.createObjectStore(BASE_URL_STORE, { | ||
keyPath: 'appName', | ||
}) | ||
} | ||
// falls through (this comment satisfies eslint) | ||
default: { | ||
console.debug('[sections-db] Done upgrading DB') | ||
} | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
/** Deletes the DB (probably not needed) */ | ||
// function deleteBaseUrlsDB() { | ||
// return deleteDB(BASE_URL_DB) | ||
// } | ||
|
||
export async function setBaseUrlByAppName({ appName, baseUrl }) { | ||
const db = await openBaseUrlsDB() | ||
return db.put(BASE_URL_STORE, { appName, baseUrl }) | ||
} | ||
|
||
export async function getBaseUrlByAppName(appName) { | ||
const db = await openBaseUrlsDB() | ||
return db.get(BASE_URL_STORE, appName).then((entry) => entry?.baseUrl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.