@@ -2,8 +2,9 @@ import { AsyncPipe, NgIf } from '@angular/common';
2
2
import { Component , inject } from '@angular/core' ;
3
3
import { MatButtonModule } from '@angular/material/button' ;
4
4
import { MatDialogModule , MatDialogRef } from '@angular/material/dialog' ;
5
+ import { MatSnackBar } from '@angular/material/snack-bar' ;
5
6
import { ZXingScannerModule } from '@zxing/ngx-scanner' ;
6
- import { of , Subject , switchMap } from 'rxjs' ;
7
+ import { finalize , of , Subject , switchMap } from 'rxjs' ;
7
8
8
9
import { AppUser } from '../core/models/app-user.model' ;
9
10
import { CurrentUserState } from '../core/states/current-user.state' ;
@@ -35,7 +36,6 @@ import { UserService } from '../core/services/user.service';
35
36
></zxing-scanner>
36
37
</div>
37
38
</div>
38
- <span class="error-message" *ngIf="errorMessage">{{ errorMessage }}</span>
39
39
40
40
<div mat-dialog-actions>
41
41
<button mat-button (click)="closeScanner()"> Cerrar </button>
@@ -71,48 +71,65 @@ export class ScannerComponent {
71
71
private userService = inject ( UserService ) ;
72
72
private currentUser = inject ( CurrentUserState ) . currentUser ;
73
73
private dialogRef = inject ( MatDialogRef < ScannerComponent > ) ;
74
- errorMessage : string | null = null ;
74
+ private snackBar = inject ( MatSnackBar ) ;
75
+ statusMessage : string | null = null ;
75
76
76
77
subject$ = new Subject < string > ( ) ;
77
78
friend$ = this . subject$ . pipe (
78
- switchMap ( ( friendEmail ) => this . userService . getUserData ( friendEmail ) ) ,
79
+ switchMap ( ( code ) => this . userService . getUserData ( code ) ) ,
79
80
switchMap ( ( friend ) => {
80
81
const currentUser = this . currentUser ( ) ;
81
82
if ( currentUser && friend && this . validateFriend ( friend ) ) {
82
- return this . userService
83
- . addFriends ( currentUser , friend )
84
- . pipe (
85
- switchMap ( ( ) => this . userService . addFriends ( friend , currentUser ) ) ,
86
- ) ;
83
+ return this . userService . addFriends ( currentUser , friend ) . pipe (
84
+ switchMap ( ( ) => this . userService . addFriends ( friend , currentUser ) ) ,
85
+ finalize ( ( ) => {
86
+ this . statusMessage = 'Lectura de QR exitosa' ;
87
+ this . finishProcessCode ( this . statusMessage ) ;
88
+ } ) ,
89
+ ) ;
90
+ } else {
91
+ if ( this . statusMessage == null ) {
92
+ this . statusMessage =
93
+ 'Error al leer el QR. Verifica su validez y vuelve a intentarlo' ;
94
+ }
95
+ this . finishProcessCode ( this . statusMessage ) ;
87
96
}
88
97
return of ( friend ) ;
89
98
} ) ,
90
99
) ;
91
100
92
- async processCode ( friendEmail : string ) : Promise < void > {
93
- this . subject$ . next ( friendEmail ) ;
101
+ processCode ( code : string ) : void {
102
+ this . subject$ . next ( code ) ;
94
103
}
95
104
96
105
validateFriend ( friend : AppUser ) : boolean {
97
106
if ( ! friend ) {
98
- this . errorMessage = 'El usuario no existe' ;
107
+ this . statusMessage = 'El usuario no existe' ;
99
108
return false ;
100
109
}
101
110
102
111
if ( this . currentUser ( ) ?. email === friend . email ) {
103
- this . errorMessage = 'No se puede agregar a sí mismo como amigo' ;
112
+ this . statusMessage = 'No se puede agregar a sí mismo como amigo' ;
104
113
return false ;
105
114
}
106
115
107
116
if ( friend . friends ?. includes ( this . currentUser ( ) ?. email ! ) ) {
108
- this . errorMessage = 'Ya es amigo' ;
117
+ this . statusMessage = 'Ya es amigo' ;
109
118
return false ;
110
119
}
111
-
112
120
return true ;
113
121
}
114
122
115
123
closeScanner ( ) : void {
116
124
this . dialogRef . close ( ) ;
117
125
}
126
+
127
+ finishProcessCode ( message : string ) {
128
+ this . closeScanner ( ) ;
129
+ if ( message ) {
130
+ this . snackBar . open ( message , 'Aceptar' , {
131
+ duration : 3000 ,
132
+ } ) ;
133
+ }
134
+ }
118
135
}
0 commit comments