Advertisement
bebo231312312321

Untitled

Mar 28th, 2024
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CommonModule, SlicePipe } from '@angular/common';
  2.  
  3. import { Component, Input, OnInit, Pipe } from '@angular/core';
  4. import { RouterLink } from '@angular/router';
  5. import { ApiService } from '../../services/api.service';
  6. import { Destination } from '../../types/destination';
  7. import { pipe } from 'rxjs';
  8. import { FavoritesService } from '../../services/favorites.service';
  9. import { UserService } from '../../User/user.service';
  10.  
  11. @Component({
  12.   selector: 'app-destination-catalog',
  13.   standalone: true,
  14.   imports: [CommonModule,RouterLink,SlicePipe ],
  15.   templateUrl: './destination-catalog.component.html',
  16.   styleUrl: './destination-catalog.component.css'
  17. })
  18. export class DestinationCatalogComponent implements OnInit {
  19.   currentSlide = 0;
  20.   // isFavorite = false;
  21.   isFavorite: { [destinationId: string]: { favorited: boolean, favoriteId?: string } } = {};
  22.  
  23.   private userId: string | undefined;
  24.   @Input() destinations: Destination[] = [];
  25.   constructor(private apiService: ApiService,private userService: UserService, private favoritesService: FavoritesService) {
  26.     this.userService.user$.subscribe(user => {
  27.       this.userId = user?._id;
  28.     });
  29.   }
  30. ngOnInit(): void {
  31.   const userId = localStorage.getItem('userId'); // или друг метод за получаване на userId
  32.   if (userId) {
  33.     this.loadFavorites(userId);
  34.   }
  35. }
  36.  
  37.  
  38.  
  39.   moveSlide(direction: 'up' | 'down'): void {
  40.     if (direction === 'up' && this.currentSlide > 0) {
  41.       this.currentSlide--;
  42.     } else if (direction === 'down' && this.currentSlide < this.destinations.length - 1) {
  43.       this.currentSlide++;
  44.     }
  45.   }
  46.  
  47.   getSlideStyle(index: number): string {
  48.     const offset = this.currentSlide * -70;
  49.     return `translateY(${offset}vh)`;
  50.   }
  51.  
  52.   loadFavorites(userId: string): void {
  53.     this.favoritesService.getAllFavorites().subscribe(allFavorites => {
  54.      
  55.       this.isFavorite = {}; // Може да пропуснете това, ако не искам да изчистват предишни стойности
  56.  
  57.     // филтрирам favorites по ownerId за да получа само тези които аз съм харесал
  58.       const userFavorites = allFavorites.filter(favorite => favorite._ownerId === userId);
  59.  
  60.     // обхождам favorites и ги разпределям по съответните дестинаци
  61.       userFavorites.forEach(favorite => {
  62.         this.isFavorite[favorite.destinationId] = { favorited: true, favoriteId: favorite._id };
  63.       });
  64.  
  65.       console.log('isFavorite', this.isFavorite);
  66.     });
  67.   }
  68.  
  69.  
  70.  
  71.  
  72.   toggleFavorite(destinationId: string): void {
  73.     const currentFavorite = this.isFavorite[destinationId];
  74.     console.log('Before:', this.isFavorite);
  75.  
  76.     if (currentFavorite && currentFavorite.favorited) {
  77.       if (typeof currentFavorite.favoriteId === 'string') {
  78.         this.favoritesService.removeFromFavorites(currentFavorite.favoriteId).subscribe(() => {
  79.           this.isFavorite[destinationId] = { favorited: false };
  80.           console.log('Removed from favorites!', this.isFavorite);
  81.         });
  82.       } else {
  83.         console.error('Favorite ID is undefined, cannot delete favorite');
  84.       }
  85.     } else {
  86.       this.favoritesService.addToFavorites(destinationId).subscribe((response: any) => {
  87.         const favoriteId = response._id;
  88.         this.isFavorite[destinationId] = { favorited: true, favoriteId: favoriteId };
  89.         console.log('Added to favorites!', this.isFavorite);
  90.       });
  91.     }
  92.   }
  93.  
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement