Skip to content

Commit 4b73216

Browse files
committed
Add network network Status
1 parent e684c14 commit 4b73216

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

src/app/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ import { NgxPermissionsModule } from 'ngx-permissions';
2020

2121
import { environment } from 'environments/environment';
2222
import { AppRoutingModule } from './core/routes/app-routing.module';
23-
import { AppComponent } from './core/components/app/app.component';
2423
import { ROOT_REDUCERS } from './core/store/app.store';
2524
import { AuthInterceptor } from './modules/authentication/interceptors/auth.interceptor';
2625
import { HttpLoadingInterceptor } from './shared/interceptors/http-loading.interceptor';
2726
import { AuthenticationModule } from './modules/authentication/authentication.module';
2827
import { ErrorsInterceptor } from './shared/interceptors/errors.interceptor';
28+
import { AppComponent } from './core/components/app/app.component';
29+
import { NetworkStatusComponent } from './core/components/network-status/network-status.component';
2930

3031
registerLocaleData(localeFr);
3132

3233
@NgModule({
33-
declarations: [AppComponent],
34+
declarations: [AppComponent, NetworkStatusComponent],
3435
imports: [
3536
AppRoutingModule,
3637
AuthenticationModule,

src/app/core/components/app/app.component.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import { Component, OnInit } from '@angular/core';
2-
3-
import { SeoService } from '@app/shared/services/seo.service';
1+
import { Component, OnDestroy, OnInit } from '@angular/core';
2+
import { fromEvent, Observable, Subscription } from 'rxjs';
43

54
@Component({
65
selector: 'admin-root',
7-
template: '<router-outlet></router-outlet>',
6+
template: `
7+
<router-outlet></router-outlet>
8+
<network-status
9+
[networkStatus]="connectionStatus"
10+
[networkStatusMessage]="connectionStatusMessage"></network-status>
11+
`,
812
})
9-
export class AppComponent implements OnInit {
13+
export class AppComponent implements OnInit, OnDestroy {
1014
mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
15+
onlineEvent!: Observable<Event>;
16+
offlineEvent!: Observable<Event>;
17+
subscriptions: Subscription[] = [];
1118

12-
public constructor(private seoService: SeoService) {}
19+
connectionStatusMessage!: string;
20+
connectionStatus!: string;
1321

1422
ngOnInit(): void {
23+
this.networkStatus();
1524
document.documentElement.setAttribute('data-theme', this.updateTheme());
1625

1726
new MutationObserver(([{ oldValue }]) => {
@@ -28,6 +37,25 @@ export class AppComponent implements OnInit {
2837
});
2938
}
3039

40+
networkStatus(): void {
41+
this.onlineEvent = fromEvent(window, 'online');
42+
this.offlineEvent = fromEvent(window, 'offline');
43+
44+
this.subscriptions.push(
45+
this.onlineEvent.subscribe(() => {
46+
this.connectionStatus = 'online';
47+
this.connectionStatusMessage = $localize`Vous êtes de nouveau en ligne.`;
48+
})
49+
);
50+
51+
this.subscriptions.push(
52+
this.offlineEvent.subscribe(() => {
53+
this.connectionStatus = 'offline';
54+
this.connectionStatusMessage = $localize`Connexion perdue ! Vous n'êtes pas connecté à l'Internet`;
55+
})
56+
);
57+
}
58+
3159
updateTheme(savedTheme: string | null = null): string {
3260
let theme = 'system';
3361
try {
@@ -59,4 +87,8 @@ export class AppComponent implements OnInit {
5987
document.documentElement.classList.remove('[&_*]:!transition-none');
6088
}, 0);
6189
}
90+
91+
ngOnDestroy(): void {
92+
this.subscriptions.forEach(subscription => subscription.unsubscribe());
93+
}
6294
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'network-status',
5+
template: `
6+
<div
7+
aria-live="assertive"
8+
class="pointer-events-none fixed inset-0 flex items-end px-4 py-6 sm:p-6">
9+
<div class="flex w-full flex-col items-end space-y-4">
10+
<div
11+
class="pointer-events-auto w-full max-w-sm overflow-hidden rounded-lg bg-white dark:bg-gray-800 shadow-lg ring-1 ring-black ring-opacity-5">
12+
<div class="p-4">
13+
<div class="flex items-start">
14+
<div class="flex-shrink-0">
15+
<!-- Heroicon name: outline/check-circle -->
16+
<svg
17+
class="h-6 w-6 text-green-400"
18+
xmlns="http://www.w3.org/2000/svg"
19+
fill="none"
20+
viewBox="0 0 24 24"
21+
stroke-width="1.5"
22+
stroke="currentColor"
23+
aria-hidden="true">
24+
<path
25+
stroke-linecap="round"
26+
stroke-linejoin="round"
27+
d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
28+
</svg>
29+
</div>
30+
<div class="ml-3 w-0 flex-1 pt-0.5">
31+
<p class="text-sm font-medium text-slate-900 capitalize">
32+
Online
33+
</p>
34+
<p class="mt-1 text-sm text-slate-500">
35+
Anyone with a link can now view this file.
36+
</p>
37+
</div>
38+
</div>
39+
</div>
40+
</div>
41+
</div>
42+
</div>
43+
<div *ngIf="networkStatus === 'online'" class="online">
44+
<span>{{ networkStatusMessage }}</span>
45+
</div>
46+
47+
<div *ngIf="networkStatus === 'offline'" class="offline">
48+
<span>{{ networkStatusMessage }}</span>
49+
</div>
50+
`,
51+
})
52+
export class NetworkStatusComponent {
53+
@Input() networkStatusMessage!: string;
54+
@Input() networkStatus!: string;
55+
}

0 commit comments

Comments
 (0)