-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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" /> | ||
<h1 mat-dialog-title> Conecta </h1> | ||
|
||
<div mat-dialog-content> | ||
|
@@ -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>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dale un nombre más significativo a esto, tal vez |
||
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 { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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