Skip to content

Commit a9ba0f0

Browse files
committed
replace network status checker function
1 parent 5a8b6dc commit a9ba0f0

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed
Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
import { Component, OnDestroy, OnInit } from '@angular/core';
2-
import { fromEvent, Observable, Subscription } from 'rxjs';
1+
import { Component, OnInit } from '@angular/core';
32

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

2213
ngOnInit(): void {
23-
this.networkStatus();
2414
document.documentElement.setAttribute('data-theme', this.updateTheme());
2515

2616
new MutationObserver(([{ oldValue }]) => {
@@ -37,27 +27,6 @@ export class AppComponent implements OnInit, OnDestroy {
3727
});
3828
}
3929

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-
59-
}
60-
6130
updateTheme(savedTheme: string | null = null): string {
6231
let theme = 'system';
6332
try {
@@ -89,8 +58,4 @@ export class AppComponent implements OnInit, OnDestroy {
8958
document.documentElement.classList.remove('[&_*]:!transition-none');
9059
}, 0);
9160
}
92-
93-
ngOnDestroy(): void {
94-
this.subscriptions.forEach(subscription => subscription.unsubscribe());
95-
}
9661
}

src/app/core/components/network-status/network-status.component.ts

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { animate, style, transition, trigger } from '@angular/animations';
2-
import { Component, Input, OnInit } from '@angular/core';
2+
import { Component, OnInit } from '@angular/core';
3+
import { fromEvent, Observable, Subscription } from 'rxjs';
34

45
@Component({
56
selector: 'network-status',
@@ -14,17 +15,16 @@ import { Component, Input, OnInit } from '@angular/core';
1415
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">
1516
<div class="p-4">
1617
<div class="flex items-start">
17-
<div class="shrink-0">
18+
<div class="flex-shrink-0">
19+
<!-- Heroicon name: outline/check-circle -->
1820
<svg
1921
class="h-6 w-6"
20-
[class.text-red-400]="networkStatus === 'offline'"
21-
[class.text-green-400]="networkStatus === 'online'"
22+
[ngClass]="{'text-green-400': networkStatus === 'online', 'text-red-400': networkStatus === 'offline'}"
23+
xmlns="http://www.w3.org/2000/svg"
2224
fill="none"
2325
viewBox="0 0 24 24"
2426
stroke-width="1.5"
2527
stroke="currentColor"
26-
xmlns="http://www.w3.org/2000/svg"
27-
viewBox="0 0 24 24"
2828
aria-hidden="true">
2929
<path
3030
*ngIf="networkStatus === 'online'"
@@ -38,7 +38,15 @@ import { Component, Input, OnInit } from '@angular/core';
3838
stroke-linejoin="round" />
3939
</svg>
4040
</div>
41-
<div class="ml-3 w-0 flex-1 pt-0.5">
41+
<div *ngIf="networkStatus === 'online'" class="ml-3 w-0 flex-1 pt-0.5">
42+
<p class="text-sm font-medium text-slate-900 capitalize">
43+
{{ networkStatus }}
44+
</p>
45+
<p class="mt-1 text-sm text-slate-500">
46+
{{ networkStatusMessage }}
47+
</p>
48+
</div>
49+
<div *ngIf="networkStatus === 'offline'" class="ml-3 w-0 flex-1 pt-0.5">
4250
<p class="text-sm font-medium text-slate-900 capitalize">
4351
{{ networkStatus }}
4452
</p>
@@ -55,22 +63,58 @@ import { Component, Input, OnInit } from '@angular/core';
5563
animations: [
5664
trigger('showHideNotification', [
5765
transition('void => *', [
58-
style({ transform: 'translateX(0.5rem)', opacity: 0 }),
59-
animate(300, style({ transform: 'translateX(0)', opacity: 1 })),
66+
style({ transform: "translateX(0.5rem)", opacity: 0 }),
67+
animate(300, style({ transform: "translateX(0)", opacity: 1 }))
6068
]),
61-
transition('* => void', [animate(100, style({ opacity: 0 }))]),
69+
transition('* => void', [
70+
animate(100, style({ opacity: 0 }))
71+
])
6272
]),
6373
],
6474
})
6575
export class NetworkStatusComponent implements OnInit {
66-
@Input() networkStatusMessage!: string;
67-
@Input() networkStatus!: string;
76+
networkStatusMessage!: string;
77+
networkStatus!: string;
6878

69-
showNetworkStatus: boolean = true;
79+
onlineEvent!: Observable<Event>;
80+
offlineEvent!: Observable<Event>;
81+
subscriptions: Subscription[] = [];
82+
83+
showNetworkStatus!: boolean;
7084

7185
ngOnInit() {
86+
this.networkStatusChecker();
87+
}
88+
89+
showHideNetworkStatus() {
90+
this.showNetworkStatus = true;
7291
setTimeout(() => {
7392
this.showNetworkStatus = false;
7493
}, 3000);
7594
}
95+
96+
networkStatusChecker(): void {
97+
this.onlineEvent = fromEvent(window, 'online');
98+
this.offlineEvent = fromEvent(window, 'offline');
99+
100+
this.subscriptions.push(
101+
this.onlineEvent.subscribe(() => {
102+
this.networkStatus = 'online';
103+
this.networkStatusMessage = $localize`Vous êtes de nouveau en ligne.`;
104+
this.showHideNetworkStatus();
105+
})
106+
);
107+
108+
this.subscriptions.push(
109+
this.offlineEvent.subscribe(() => {
110+
this.networkStatus = 'offline';
111+
this.networkStatusMessage = $localize`Connexion perdue ! Vous n'êtes pas connecté à l'Internet`;
112+
this.showHideNetworkStatus();
113+
})
114+
);
115+
}
116+
117+
ngOnDestroy(): void {
118+
this.subscriptions.forEach(subscription => subscription.unsubscribe());
119+
}
76120
}

0 commit comments

Comments
 (0)