Advertisement
nikolayneykov

Untitled

Dec 5th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { SocketService } from '../../core/services/socket.service';
  2. import { NotificationService } from './../../core/services/notification.service';
  3. import { AuthService } from './../../core/services/auth.service';
  4. import { UsersService } from './../../core/services/users.service';
  5. import { UserDTO } from './../../models/user/user.dto';
  6. import { Component, OnInit, OnDestroy } from '@angular/core';
  7. import { ActivatedRoute } from '@angular/router';
  8. import { Subscription } from 'rxjs/internal/Subscription';
  9.  
  10. @Component({
  11.   selector: 'app-users-list',
  12.   templateUrl: './users-list.component.html',
  13.   styleUrls: ['./users-list.component.scss']
  14. })
  15. export class UsersListComponent implements OnInit, OnDestroy {
  16.   private loggedUserSubscription: Subscription;
  17.   loggedUserData: UserDTO;
  18.   users: UserDTO[];
  19.   keyword = '';
  20.   take = 10;
  21.   skip = 0;
  22.  
  23.   constructor(
  24.     private readonly notificationService: NotificationService,
  25.     private readonly authService: AuthService,
  26.     private readonly route: ActivatedRoute,
  27.     private readonly usersService: UsersService,
  28.     private readonly webSocketService: SocketService,
  29.   ) { }
  30.  
  31.   onScroll(): void {
  32.     this.skip += this.take;
  33.  
  34.     this.usersService.findUsers(this.keyword, this.skip, this.take)
  35.       .subscribe((data: UserDTO[]) => {
  36.         this.users = [...this.users, ...data];
  37.         this.skip += this.take;
  38.       })
  39.   }
  40.  
  41.   search(keyword: string): void {
  42.     this.keyword = keyword;
  43.     this.skip = 0;
  44.  
  45.     this.usersService.findUsers(this.keyword, this.skip, this.take)
  46.       .subscribe((data: UserDTO[]) => {
  47.         this.users = data;
  48.       });
  49.   }
  50.  
  51.   followUser(user: UserDTO): void {
  52.     if (user.id === this.loggedUserData.id) {
  53.       return;
  54.     }
  55.  
  56.     this.usersService
  57.       .followUser(this.loggedUserData.id, user.id)
  58.       .subscribe(({ userFollower, userFollowed }) => {
  59.         const followerIndex = this.users.findIndex(u => u.id === userFollower.id);
  60.         const followedIndex = this.users.findIndex(u => u.id === userFollowed.id);
  61.  
  62.         this.users[followerIndex] = userFollower;
  63.         this.users[followedIndex] = userFollowed;
  64.         this.notificationService.success(`You have started following ${userFollowed.username}.`);
  65.       });
  66.   }
  67.  
  68.   unFollowUser(user: UserDTO): void {
  69.     if (user.id === this.loggedUserData.id) {
  70.       return;
  71.     }
  72.  
  73.     this.usersService
  74.       .unFollowUser(this.loggedUserData.id, user.id)
  75.       .subscribe(({ userFollower, userFollowed }) => {
  76.         const followerIndex = this.users.findIndex(u => u.id === userFollower.id);
  77.         const followedIndex = this.users.findIndex(u => u.id === userFollowed.id);
  78.  
  79.         this.users[followerIndex] = userFollower;
  80.         this.users[followedIndex] = userFollowed;
  81.         this.notificationService.warning(`You are no longer following ${userFollowed.username}.`);
  82.       });
  83.   }
  84.  
  85.   ngOnInit(): void {
  86.     this.loggedUserSubscription = this.authService.loggedUserData$
  87.       .subscribe((loggedUserData: UserDTO) => {
  88.         this.loggedUserData = loggedUserData;
  89.       });
  90.  
  91.     this.route.data
  92.       .subscribe(({ data }) => {
  93.         this.users = data;
  94.       });
  95.   }
  96.  
  97.   ngOnDestroy(): void {
  98.     this.loggedUserSubscription.unsubscribe();
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement