1
1
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' ;
3
4
4
5
@Component ( {
5
6
selector : 'network-status' ,
@@ -14,17 +15,16 @@ import { Component, Input, OnInit } from '@angular/core';
14
15
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">
15
16
<div class="p-4">
16
17
<div class="flex items-start">
17
- <div class="shrink-0">
18
+ <div class="flex-shrink-0">
19
+ <!-- Heroicon name: outline/check-circle -->
18
20
<svg
19
21
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 "
22
24
fill="none"
23
25
viewBox="0 0 24 24"
24
26
stroke-width="1.5"
25
27
stroke="currentColor"
26
- xmlns="http://www.w3.org/2000/svg"
27
- viewBox="0 0 24 24"
28
28
aria-hidden="true">
29
29
<path
30
30
*ngIf="networkStatus === 'online'"
@@ -38,7 +38,15 @@ import { Component, Input, OnInit } from '@angular/core';
38
38
stroke-linejoin="round" />
39
39
</svg>
40
40
</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">
42
50
<p class="text-sm font-medium text-slate-900 capitalize">
43
51
{{ networkStatus }}
44
52
</p>
@@ -55,22 +63,58 @@ import { Component, Input, OnInit } from '@angular/core';
55
63
animations : [
56
64
trigger ( 'showHideNotification' , [
57
65
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 } ) )
60
68
] ) ,
61
- transition ( '* => void' , [ animate ( 100 , style ( { opacity : 0 } ) ) ] ) ,
69
+ transition ( '* => void' , [
70
+ animate ( 100 , style ( { opacity : 0 } ) )
71
+ ] )
62
72
] ) ,
63
73
] ,
64
74
} )
65
75
export class NetworkStatusComponent implements OnInit {
66
- @ Input ( ) networkStatusMessage ! : string ;
67
- @ Input ( ) networkStatus ! : string ;
76
+ networkStatusMessage ! : string ;
77
+ networkStatus ! : string ;
68
78
69
- showNetworkStatus : boolean = true ;
79
+ onlineEvent ! : Observable < Event > ;
80
+ offlineEvent ! : Observable < Event > ;
81
+ subscriptions : Subscription [ ] = [ ] ;
82
+
83
+ showNetworkStatus ! : boolean ;
70
84
71
85
ngOnInit ( ) {
86
+ this . networkStatusChecker ( ) ;
87
+ }
88
+
89
+ showHideNetworkStatus ( ) {
90
+ this . showNetworkStatus = true ;
72
91
setTimeout ( ( ) => {
73
92
this . showNetworkStatus = false ;
74
93
} , 3000 ) ;
75
94
}
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
+ }
76
120
}
0 commit comments