From 3e4458c9dc2cf26f288345ad635dbb2883a16867 Mon Sep 17 00:00:00 2001 From: FazCodeFR <30906528+FazCodeFR@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:26:48 +0200 Subject: [PATCH] feature/favoris --- src/app/app.module.ts | 8 ++ .../animaux-list/animaux-list.component.html | 26 +++++-- .../animaux-list/animaux-list.component.ts | 73 ++++++++++++++----- .../components/profil/profil.component.html | 4 +- src/app/interfaces/interfaces.ts | 2 +- src/app/services/app.service.ts | 18 ++++- 6 files changed, 103 insertions(+), 28 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9a2c1ce..bb3f9ec 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -88,6 +88,14 @@ import { ToastModule } from 'primeng/toast'; httpInterceptor: { allowedList: [ // Tous les post, put, delete et patch sont protégés par le token + { + uri: `${environment.urlAPI}/favoris`, + httpMethod: HttpMethod.Get, + }, + { + uri: `${environment.urlAPI}/chats/favoris`, + httpMethod: HttpMethod.Get, + }, { uri: `${environment.urlAPI}/*`, httpMethod: HttpMethod.Post, diff --git a/src/app/components/animaux/animaux-list/animaux-list.component.html b/src/app/components/animaux/animaux-list/animaux-list.component.html index 9993d0f..d8ab614 100644 --- a/src/app/components/animaux/animaux-list/animaux-list.component.html +++ b/src/app/components/animaux/animaux-list/animaux-list.component.html @@ -1,6 +1,6 @@ -
@@ -21,10 +21,15 @@ aria-label="Association" title="L'association">{{chat.association.nom}} - {{formatDate(chat.createdAt)}}

- + + +
{{chat.nom}} @@ -40,4 +45,13 @@
-
\ No newline at end of file + + + +
+

+ Pas de chat pour le moment :/ +

+
+ + diff --git a/src/app/components/animaux/animaux-list/animaux-list.component.ts b/src/app/components/animaux/animaux-list/animaux-list.component.ts index 28e6331..4d7bb1e 100644 --- a/src/app/components/animaux/animaux-list/animaux-list.component.ts +++ b/src/app/components/animaux/animaux-list/animaux-list.component.ts @@ -1,4 +1,4 @@ -import { Component, Inject } from '@angular/core'; +import { Component, Inject, Input } from '@angular/core'; import { faMars, faVenus, faHeart } from '@fortawesome/free-solid-svg-icons'; import { faHeart as fasHeart } from '@fortawesome/free-regular-svg-icons'; @@ -17,6 +17,7 @@ import { Subscription } from 'rxjs'; providers: [DatePipe], }) export class AnimauxListComponent { + @Input() profileContext: boolean = false; isLoaded: boolean = false; faMars = faMars; faVenus = faVenus; @@ -35,33 +36,32 @@ export class AnimauxListComponent { ) {} chats: Chat[] = []; - isFav = false; + favoris: Favori[] = []; private subscriptions = new Subscription(); ngOnInit() { - this.getCats(); + if (this.profileContext) { + this.getCatByFavoris(); + } else { + this.getFavs(); + this.getCats(); + } } toggleFavori(chat: Chat) { - const isChatInFavoris = chat.favoris.length > 0; - - if (isChatInFavoris) { - // Si le chat est déjà dans la liste des favoris, retire-le - const favoriToRemove = chat.favoris[0]; - chat.favoris = []; - - this.appService.removeFavori(favoriToRemove.id!).subscribe((favori) => { - console.log('Favori retiré:', favori); + if (chat.isFavori) { + // Si le chat est déjà un favori, retirez-le des favoris + this.appService.removeFavoriByCat(chat.id).subscribe(() => { + chat.isFavori = false; + console.log('Favori retiré'); this.toastr.info('Chat retiré des favoris', 'Favori'); }); } else { - // Sinon, ajoute-le aux favoris - const newFavori: Favori = { - chatId: chat.id, - }; - this.appService.createFavori(newFavori).subscribe((favori) => { - chat.favoris.push(favori); + // Sinon, ajoutez-le aux favoris + this.appService.createFavori({ chatId: chat.id }).subscribe(() => { + chat.isFavori = true; + console.log('Favori ajouté'); this.toastr.info('Chat ajouté aux favoris', 'Favori'); }); } @@ -84,6 +84,43 @@ export class AnimauxListComponent { this.subscriptions.add(catSubscription); } + getCatByFavoris() { + const favCatSubscription = this.appService.getCatByFavoris().subscribe({ + next: (chats) => { + this.chats = chats; + this.isLoaded = true; + this.chats.forEach(chat => chat.isFavori = true); + }, + error: (error) => { + const errorText = 'Erreur lors de la récupération des chats favoris'; + console.error(errorText, error); + this.toastr.error(errorText, 'Erreur'); + }, + complete: () => console.log('Chats favoris chargés') + }); + + this.subscriptions.add(favCatSubscription); + } + + getFavs() { + const favSubscription = this.appService.getFavorisByUser().subscribe({ + next: (favoriIds: number[]) => { + this.chats.forEach(chat => { + chat.isFavori = favoriIds.includes(chat.id); + }); + }, + error: (error) => { + const errorText = 'Erreur lors de la récupération des favoris'; + console.error(errorText, error); + this.toastr.error(errorText, 'Erreur'); + }, + complete: () => console.log('Completion handler') + }); + this.subscriptions.add(favSubscription); + } + + + ngOnDestroy() { this.subscriptions.unsubscribe(); } diff --git a/src/app/components/profil/profil.component.html b/src/app/components/profil/profil.component.html index 69940c6..07ec338 100644 --- a/src/app/components/profil/profil.component.html +++ b/src/app/components/profil/profil.component.html @@ -58,9 +58,9 @@

{{
-

Mes favoris:

+

Mes favoris:

- +
diff --git a/src/app/interfaces/interfaces.ts b/src/app/interfaces/interfaces.ts index b0b3938..75ef3a2 100644 --- a/src/app/interfaces/interfaces.ts +++ b/src/app/interfaces/interfaces.ts @@ -30,7 +30,7 @@ export interface Chat { favoris: Favori[]; photos: Photo[]; videos: Video[]; - isFav: boolean; + isFavori?: boolean; } export interface Photo { diff --git a/src/app/services/app.service.ts b/src/app/services/app.service.ts index 0127e53..0270016 100644 --- a/src/app/services/app.service.ts +++ b/src/app/services/app.service.ts @@ -34,6 +34,14 @@ export class AppService { ); } + getCatByFavoris(): Observable { + return this.http.get(this.api + '/chats/favoris').pipe( + map((res: any) => res), + share(), + take(1) + ); + } + getByIdCat(id: number): Observable { return this.http.get(this.api + '/chats/' + id).pipe( map((res: any) => res), @@ -58,7 +66,15 @@ export class AppService { ); } - removeFavori(id: number): Observable { + getFavorisByUser(): Observable { + return this.http.get(this.api + '/favoris').pipe( + map((res: any) => res), + share(), + take(1) + ); + } + + removeFavoriByCat(id: number): Observable { return this.http.delete(this.api + '/favoris/' + id).pipe( map((res: any) => res), share(),