Skip to content

Commit f3814ef

Browse files
committed
wip
1 parent 8e3aa50 commit f3814ef

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import { Component, OnDestroy, OnInit } from '@angular/core';
2-
import { fromEvent, Observable, Subscription } from 'rxjs';
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,25 +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-
5930
updateTheme(savedTheme: string | null = null): string {
6031
let theme = 'system';
6132
try {
@@ -87,8 +58,4 @@ export class AppComponent implements OnInit, OnDestroy {
8758
document.documentElement.classList.remove('[&_*]:!transition-none');
8859
}, 0);
8960
}
90-
91-
ngOnDestroy(): void {
92-
this.subscriptions.forEach(subscription => subscription.unsubscribe());
93-
}
9461
}

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

Lines changed: 58 additions & 15 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',
@@ -17,39 +18,47 @@ import { Component, Input, OnInit } from '@angular/core';
1718
<div class="flex-shrink-0">
1819
<!-- Heroicon name: outline/check-circle -->
1920
<svg
20-
class="h-6 w-6 text-green-400"
21+
class="h-6 w-6"
22+
[ngClass]="{'text-green-400': networkStatus === 'online', 'text-red-400': networkStatus === 'offline'}"
2123
xmlns="http://www.w3.org/2000/svg"
2224
fill="none"
2325
viewBox="0 0 24 24"
2426
stroke-width="1.5"
2527
stroke="currentColor"
2628
aria-hidden="true">
2729
<path
30+
*ngIf="networkStatus === 'online'"
2831
stroke-linecap="round"
2932
stroke-linejoin="round"
3033
d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
34+
<path
35+
*ngIf="networkStatus === 'offline'"
36+
d="M15.312 10c1.368.426 2.65 1.12 3.768 2.05m3.5-3.55a16 16 0 0 0-12.383-3.896M8.53 15.61a6 6 0 0 1 6.95 0M12 19.5h.01M1.193 8.7A16.014 16.014 0 0 1 5.76 5.764m-1.027 6.48a10.966 10.966 0 0 1 5-2.51m5.966 6.042A5.974 5.974 0 0 0 12 14.5c-1.416 0-2.718.49-3.745 1.312M3 3l18 18"
37+
stroke-linecap="round"
38+
stroke-linejoin="round" />
3139
</svg>
3240
</div>
33-
<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">
3450
<p class="text-sm font-medium text-slate-900 capitalize">
35-
Online
51+
{{ networkStatus }}
3652
</p>
3753
<p class="mt-1 text-sm text-slate-500">
38-
Anyone with a link can now view this file.
54+
{{ networkStatusMessage }}
3955
</p>
4056
</div>
4157
</div>
4258
</div>
4359
</div>
4460
</div>
4561
</div>
46-
<div *ngIf="networkStatus === 'online'" class="online">
47-
<span>{{ networkStatusMessage }}</span>
48-
</div>
49-
50-
<div *ngIf="networkStatus === 'offline'" class="offline">
51-
<span>{{ networkStatusMessage }}</span>
52-
</div>
5362
`,
5463
animations: [
5564
trigger('showHideNotification', [
@@ -64,14 +73,48 @@ import { Component, Input, OnInit } from '@angular/core';
6473
],
6574
})
6675
export class NetworkStatusComponent implements OnInit {
67-
@Input() networkStatusMessage!: string;
68-
@Input() networkStatus!: string;
76+
networkStatusMessage!: string;
77+
networkStatus!: string;
6978

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

7285
ngOnInit() {
86+
this.networkStatusChecker();
87+
}
88+
89+
showHideNetworkStatus() {
90+
this.showNetworkStatus = true;
7391
setTimeout(() => {
7492
this.showNetworkStatus = false;
7593
}, 3000);
7694
}
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+
}
77120
}

0 commit comments

Comments
 (0)