From eb7ceff10c81c1b0fbf094a45818700df23c7c7a Mon Sep 17 00:00:00 2001 From: Brian Vega Date: Thu, 24 Aug 2023 13:10:06 -0400 Subject: [PATCH] corrections to convert promises to observables --- .../core/functions/load-effect.function.ts | 1 + src/app/core/services/user.service.ts | 9 ++- src/app/scanner/scanner.component.ts | 63 ++++++++++++------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/app/core/functions/load-effect.function.ts b/src/app/core/functions/load-effect.function.ts index c57f4a0..db4be56 100644 --- a/src/app/core/functions/load-effect.function.ts +++ b/src/app/core/functions/load-effect.function.ts @@ -7,6 +7,7 @@ export const loadEffect = () => { return { subscribe: () => loadingState.startLoading(), + tap: () => loadingState.stopLoading(), finalize: () => loadingState.stopLoading(), }; }; diff --git a/src/app/core/services/user.service.ts b/src/app/core/services/user.service.ts index 4af7c9b..2543360 100644 --- a/src/app/core/services/user.service.ts +++ b/src/app/core/services/user.service.ts @@ -40,14 +40,19 @@ export class UserService { return docData.data() as AppUser | undefined; } - async addFriends(user: AppUser, friendUser: AppUser): Promise { + addFriends(user: AppUser, friendUser: AppUser): Observable { const friends = user?.friends || []; const newFriends = [...new Set([...friends, friendUser.email!])]; const points = user?.score || 0; const newPoints = points + 20; const docRef = doc(this.db, 'users', user.email!); - await updateDoc(docRef, { friends: newFriends, score: newPoints }); + return from( + updateDoc(docRef, { friends: newFriends, score: newPoints }), + ).pipe( + tap(this.loadEffectObserver), + catchError((error) => handleError(error, this.logger)), + ); } createUser({ diff --git a/src/app/scanner/scanner.component.ts b/src/app/scanner/scanner.component.ts index 092f8fd..8e604fd 100644 --- a/src/app/scanner/scanner.component.ts +++ b/src/app/scanner/scanner.component.ts @@ -1,8 +1,9 @@ -import { NgIf } from '@angular/common'; +import { AsyncPipe, NgIf } from '@angular/common'; import { Component, inject } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { ZXingScannerModule } from '@zxing/ngx-scanner'; +import { of, Subject, switchMap } from 'rxjs'; import { AppUser } from '../core/models/app-user.model'; import { CurrentUserState } from '../core/states/current-user.state'; @@ -11,8 +12,15 @@ import { UserService } from '../core/services/user.service'; @Component({ selector: 'io-scanner', standalone: true, - imports: [MatButtonModule, MatDialogModule, ZXingScannerModule, NgIf], + imports: [ + AsyncPipe, + MatButtonModule, + MatDialogModule, + ZXingScannerModule, + NgIf, + ], template: ` +

Conecta

@@ -61,40 +69,47 @@ import { UserService } from '../core/services/user.service'; }) export class ScannerComponent { private userService = inject(UserService); - private userState = inject(CurrentUserState); + private currentUser = inject(CurrentUserState).currentUser; private dialogRef = inject(MatDialogRef); errorMessage: string | null = null; + subject$ = new Subject(); + friend$ = this.subject$.pipe( + switchMap((friendEmail) => this.userService.getUserData(friendEmail)), + switchMap((friend) => { + const currentUser = this.currentUser(); + if (currentUser && friend && this.validateFriend(friend)) { + return this.userService + .addFriends(currentUser, friend) + .pipe( + switchMap(() => this.userService.addFriends(friend, currentUser)), + ); + } + return of(friend); + }), + ); + async processCode(friendEmail: string): Promise { - const user: AppUser | undefined = this.userState.currentUser(); - const userEmail = user?.email; - if (!userEmail || userEmail === friendEmail) { - this.errorMessage = 'No se puede agregar a sí mismo como amigo'; - return; - } + this.subject$.next(friendEmail); + } - const userDoc: AppUser | undefined = - await this.userService.getUserData(friendEmail); - if (!userDoc) { + validateFriend(friend: AppUser): boolean { + if (!friend) { this.errorMessage = 'El usuario no existe'; - return; + return false; } - const isFriend: boolean | undefined = userDoc.friends?.includes(userEmail); - if (isFriend) { - this.errorMessage = 'Ya es amigo'; - return; + if (this.currentUser()?.email === friend.email) { + this.errorMessage = 'No se puede agregar a sí mismo como amigo'; + return false; } - const friendsDoc: AppUser | undefined = - await this.userService.getUserData(friendEmail); - if (friendsDoc && userEmail) { - await this.userService.addFriends(user, friendsDoc); - await this.userService.addFriends(friendsDoc, user); + if (friend.friends?.includes(this.currentUser()?.email!)) { + this.errorMessage = 'Ya es amigo'; + return false; } - this.errorMessage = null; - return; + return true; } closeScanner(): void {