Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-friends #8

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/core/functions/load-effect.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const loadEffect = () => {

return {
subscribe: () => loadingState.startLoading(),
tap: () => loadingState.stopLoading(),
finalize: () => loadingState.stopLoading(),
};
};
9 changes: 7 additions & 2 deletions src/app/core/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ export class UserService {
return docData.data() as AppUser | undefined;
}

async addFriends(user: AppUser, friendUser: AppUser): Promise<void> {
addFriends(user: AppUser, friendUser: AppUser): Observable<void> {
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({
Expand Down
63 changes: 39 additions & 24 deletions src/app/scanner/scanner.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: `
<ng-container *ngIf="friend$ | async as friend" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

estar parte no es necesaria as friend

<h1 mat-dialog-title> Conecta </h1>

<div mat-dialog-content>
Expand Down Expand Up @@ -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<ScannerComponent>);
errorMessage: string | null = null;

subject$ = new Subject<string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dale un nombre más significativo a esto, tal vez friendSubject$

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<void> {
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 {
Expand Down