Refactor online status tracker.
This commit is contained in:
parent
5fcee3dcf9
commit
beca2bf7f8
@ -9,3 +9,4 @@ export * from '@app/_helpers/mock-backend';
|
|||||||
export * from '@app/_helpers/read-csv';
|
export * from '@app/_helpers/read-csv';
|
||||||
export * from '@app/_helpers/schema-validation';
|
export * from '@app/_helpers/schema-validation';
|
||||||
export * from '@app/_helpers/sync';
|
export * from '@app/_helpers/sync';
|
||||||
|
export * from '@app/_helpers/online-status';
|
||||||
|
17
src/app/_helpers/online-status.ts
Normal file
17
src/app/_helpers/online-status.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const apiUrls = [
|
||||||
|
'https://api.coindesk.com/v1/bpi/currentprice.json',
|
||||||
|
'https://dog.ceo/api/breeds/image/random',
|
||||||
|
'https://ipinfo.io/161.185.160.93/geo',
|
||||||
|
'https://randomuser.me/api/',
|
||||||
|
];
|
||||||
|
|
||||||
|
async function checkOnlineStatus(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const online = await fetch(apiUrls[Math.floor(Math.random() * apiUrls.length)]);
|
||||||
|
return online.status >= 200 && online.status < 300;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { checkOnlineStatus };
|
@ -1,6 +1,6 @@
|
|||||||
<nav class="navbar navbar-dark background-dark">
|
<nav class="navbar navbar-dark background-dark">
|
||||||
<h1 class="navbar-brand">
|
<h1 class="navbar-brand">
|
||||||
<div *ngIf="noInternetConnection; then offlineBlock; else onlineBlock"></div>
|
<div *ngIf="online; then onlineBlock; else offlineBlock"></div>
|
||||||
<ng-template #offlineBlock>
|
<ng-template #offlineBlock>
|
||||||
<strong style="color: red">OFFLINE </strong>
|
<strong style="color: red">OFFLINE </strong>
|
||||||
<img width="20rem" src="assets/images/no-wifi.svg" alt="Internet Disconnected" />
|
<img width="20rem" src="assets/images/no-wifi.svg" alt="Internet Disconnected" />
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
|
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
|
||||||
|
import { checkOnlineStatus } from '@src/app/_helpers';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-network-status',
|
selector: 'app-network-status',
|
||||||
@ -7,21 +8,30 @@ import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@
|
|||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class NetworkStatusComponent implements OnInit {
|
export class NetworkStatusComponent implements OnInit {
|
||||||
noInternetConnection: boolean = !navigator.onLine;
|
online: boolean = navigator.onLine;
|
||||||
|
|
||||||
constructor(private cdr: ChangeDetectorRef) {
|
constructor(private cdr: ChangeDetectorRef) {
|
||||||
this.handleNetworkChange();
|
this.handleNetworkChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {}
|
ngOnInit(): void {
|
||||||
|
window.addEventListener('online', (event: any) => {
|
||||||
|
this.online = true;
|
||||||
|
this.cdr.detectChanges();
|
||||||
|
});
|
||||||
|
window.addEventListener('offline', (event: any) => {
|
||||||
|
this.online = false;
|
||||||
|
this.cdr.detectChanges();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
handleNetworkChange(): void {
|
handleNetworkChange(): void {
|
||||||
setTimeout(() => {
|
setTimeout(async () => {
|
||||||
if (!navigator.onLine !== this.noInternetConnection) {
|
if (this.online !== (await checkOnlineStatus())) {
|
||||||
this.noInternetConnection = !navigator.onLine;
|
this.online = await checkOnlineStatus();
|
||||||
this.cdr.detectChanges();
|
this.cdr.detectChanges();
|
||||||
}
|
}
|
||||||
this.handleNetworkChange();
|
this.handleNetworkChange();
|
||||||
}, 5000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user