forked from eile/hiking-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add messages when user is online/offline
- Loading branch information
1 parent
c2c4528
commit a4ff93e
Showing
6 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
import * as dom from "dojo/dom"; | ||
import * as on from "dojo/on"; | ||
import * as domConstruct from "dojo/dom-construct"; | ||
|
||
function createMessage(message: string, online: boolean): void { | ||
|
||
// display message | ||
const messageContainer = domConstruct.create("div", { | ||
innerHTML: message, | ||
class: online ? "online connectionMessage" : "offline connectionMessage" | ||
}, document.body); | ||
|
||
// message disappears after 3 seconds | ||
window.setTimeout(function() { | ||
domConstruct.destroy(messageContainer); | ||
}, 3000); | ||
} | ||
|
||
export default class ConnectionManager { | ||
|
||
constructor(state) { | ||
|
||
window.addEventListener("load", function() { | ||
|
||
function updateOnlineStatus(event) { | ||
state.online = navigator.onLine ? true : false; | ||
} | ||
|
||
window.addEventListener("online", updateOnlineStatus); | ||
window.addEventListener("offline", updateOnlineStatus); | ||
}); | ||
|
||
state.watch("online", (value) => { | ||
console.log(value); | ||
if (!value) { | ||
this.createOfflineMessage(); | ||
} | ||
else { | ||
this.createOnlineMessage(); | ||
} | ||
}); | ||
} | ||
|
||
createOfflineMessage() { | ||
createMessage("You seem to be offline. This application has limited functionality.", false); | ||
} | ||
|
||
createOnlineMessage() { | ||
createMessage("You are back online.", true); | ||
} | ||
|
||
} |