11import { 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} )
6675export 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