Skip to content

Commit

Permalink
fix: handle low cli-app-scripts version [LIBS-501] (#1349)
Browse files Browse the repository at this point in the history
* fix: handle low cli-app-scripts version

* fix: validate isConnected
  • Loading branch information
KaiVandivier authored Dec 14, 2023
1 parent 96bceca commit d15bce1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ describe('initialization to the right values based on offline interface', () =>
wrapper: customWrapper,
})

expect(result.current.isConnected).toBe(false)
expect(result.current.isDisconnected).toBe(true)
expect(result.current.lastConnected).toEqual(testCurrentDate)
expect(result.current.isConnected).toBe(true)
expect(result.current.isDisconnected).toBe(false)
expect(result.current.lastConnected).toBe(null)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ export const Dhis2ConnectionStatusProvider = ({
}, [pingAndHandleStatus, serverVersion])

useEffect(() => {
if (!offlineInterface.subscribeToDhis2ConnectionStatus) {
// Missing this functionality from the offline interface --
// use a ping on startup to get the status
smartIntervalRef.current?.invokeCallbackImmediately()
console.warn(
'Please upgrade to @dhis2/cli-app-scripts@>10.3.8 for full connection status features'
)
return
}

const unsubscribe = offlineInterface.subscribeToDhis2ConnectionStatus({
onUpdate,
})
Expand All @@ -190,23 +200,23 @@ export const Dhis2ConnectionStatusProvider = ({
}, [offlineInterface, onUpdate])

// Memoize this value to prevent unnecessary rerenders of context provider
const contextValue = useMemo(
() => ({
// in the unlikely circumstance that offlineInterface.latestIsConnected
// is `null` when this initializes, fail safe by defaulting to
// `isConnected: false`
isConnected: Boolean(isConnected),
isDisconnected: !isConnected,
lastConnected: isConnected
const contextValue = useMemo(() => {
// in the unlikely circumstance that offlineInterface.latestIsConnected
// is `null` or `undefined` when this initializes, fail safe by defaulting to
// `isConnected: true`. A ping or SW update should update the status shortly.
const validatedIsConnected = isConnected ?? true
return {
isConnected: validatedIsConnected,
isDisconnected: !validatedIsConnected,
lastConnected: validatedIsConnected
? null
: // Only evaluate if disconnected, since local storage
// is synchronous and disk-based.
// If lastConnected is not set in localStorage though, set it.
// (relevant on startup)
getLastConnected(appName) || updateLastConnected(appName),
}),
[isConnected, appName]
)
}
}, [isConnected, appName])

return (
<Dhis2ConnectionStatusContext.Provider value={contextValue}>
Expand Down

0 comments on commit d15bce1

Please sign in to comment.