Skip to content

Commit c41b137

Browse files
committed
Implement QR Scan Success and Failure Handling
1 parent c0ac6a1 commit c41b137

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/app/core/services/user.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ export class UserService {
4141
}
4242

4343
addFriends(user: AppUser, friendUser: AppUser): Observable<void> {
44+
const pointsPerQRRead = 20;
4445
const friends = user?.friends || [];
4546
const newFriends = [...new Set([...friends, friendUser.email!])];
4647
const points = user?.score || 0;
47-
const newPoints = points + 20;
48+
const newPoints = points + pointsPerQRRead;
4849

4950
const docRef = doc(this.db, 'users', user.email!);
5051
return from(

src/app/scanner/scanner.component.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { AsyncPipe, NgIf } from '@angular/common';
22
import { Component, inject } from '@angular/core';
33
import { MatButtonModule } from '@angular/material/button';
44
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
5+
import { MatSnackBar } from '@angular/material/snack-bar';
56
import { ZXingScannerModule } from '@zxing/ngx-scanner';
6-
import { of, Subject, switchMap } from 'rxjs';
7+
import { finalize, of, Subject, switchMap } from 'rxjs';
78

89
import { AppUser } from '../core/models/app-user.model';
910
import { CurrentUserState } from '../core/states/current-user.state';
@@ -35,7 +36,6 @@ import { UserService } from '../core/services/user.service';
3536
></zxing-scanner>
3637
</div>
3738
</div>
38-
<span class="error-message" *ngIf="errorMessage">{{ errorMessage }}</span>
3939
4040
<div mat-dialog-actions>
4141
<button mat-button (click)="closeScanner()"> Cerrar </button>
@@ -71,48 +71,65 @@ export class ScannerComponent {
7171
private userService = inject(UserService);
7272
private currentUser = inject(CurrentUserState).currentUser;
7373
private dialogRef = inject(MatDialogRef<ScannerComponent>);
74-
errorMessage: string | null = null;
74+
private snackBar = inject(MatSnackBar);
75+
statusMessage: string | null = null;
7576

7677
subject$ = new Subject<string>();
7778
friend$ = this.subject$.pipe(
78-
switchMap((friendEmail) => this.userService.getUserData(friendEmail)),
79+
switchMap((code) => this.userService.getUserData(code)),
7980
switchMap((friend) => {
8081
const currentUser = this.currentUser();
8182
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);
8796
}
8897
return of(friend);
8998
}),
9099
);
91100

92-
async processCode(friendEmail: string): Promise<void> {
93-
this.subject$.next(friendEmail);
101+
processCode(code: string): void {
102+
this.subject$.next(code);
94103
}
95104

96105
validateFriend(friend: AppUser): boolean {
97106
if (!friend) {
98-
this.errorMessage = 'El usuario no existe';
107+
this.statusMessage = 'El usuario no existe';
99108
return false;
100109
}
101110

102111
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';
104113
return false;
105114
}
106115

107116
if (friend.friends?.includes(this.currentUser()?.email!)) {
108-
this.errorMessage = 'Ya es amigo';
117+
this.statusMessage = 'Ya es amigo';
109118
return false;
110119
}
111-
112120
return true;
113121
}
114122

115123
closeScanner(): void {
116124
this.dialogRef.close();
117125
}
126+
127+
finishProcessCode(message: string) {
128+
this.closeScanner();
129+
if (message) {
130+
this.snackBar.open(message, 'Aceptar', {
131+
duration: 3000,
132+
});
133+
}
134+
}
118135
}

0 commit comments

Comments
 (0)