Advertisement
nikolayneykov

Untitled

Dec 5th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { FormGroup } from '@angular/forms';
  2. import { ConfirmationComponent } from './../../shared/confirmation/confirmation.component';
  3. import { NotificationService } from './../../core/services/notification.service';
  4. import { UsersService } from './../../core/services/users.service';
  5. import { Component, OnInit, OnDestroy } from '@angular/core';
  6. import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
  7. import { AuthService } from '../../core/services/auth.service';
  8. import { UserDTO } from '../../models/user/user.dto';
  9. import { Subscription } from 'rxjs';
  10. import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
  11. import { NgxSpinnerService } from 'ngx-spinner';
  12. import { Lightbox } from 'ngx-lightbox';
  13.  
  14. @Component({
  15.   selector: 'app-profile',
  16.   templateUrl: './profile.component.html',
  17.   styleUrls: ['./profile.component.scss']
  18. })
  19. export class ProfileComponent implements OnInit, OnDestroy {
  20.   private loggedUserSubscription: Subscription;
  21.   private profilePhoto: any[];
  22.   loggedUserData: UserDTO;
  23.   user: UserDTO;
  24.   followers: UserDTO[];
  25.   following: UserDTO[];
  26.   isOpenSettings = false;
  27.  
  28.   constructor(
  29.     private readonly route: ActivatedRoute,
  30.     private readonly authService: AuthService,
  31.     private readonly usersService: UsersService,
  32.     private readonly notificationService: NotificationService,
  33.     private readonly modalService: NgbModal,
  34.     private readonly spinner: NgxSpinnerService,
  35.     private readonly router: Router,
  36.     private readonly lightbox: Lightbox,
  37.   ) { }
  38.  
  39.   open(): void {
  40.     this.lightbox.open(this.profilePhoto, 0);
  41.   }
  42.  
  43.   openSettings(): void {
  44.     if (this.loggedUserData.id === this.user.id) {
  45.       this.isOpenSettings = !this.isOpenSettings;
  46.     }
  47.   }
  48.  
  49.   followUser(): void {
  50.     this.usersService.followUser(this.loggedUserData.id, this.user.id)
  51.       .subscribe(({ userFollowed, userFollower }) => {
  52.         this.user = userFollowed;
  53.         this.followers = [userFollower, ...this.followers];
  54.         this.notificationService.success(`You have started following ${userFollowed.username}.`);
  55.       })
  56.   }
  57.  
  58.   unFollowUser(): void {
  59.     this.usersService.unFollowUser(this.loggedUserData.id, this.user.id)
  60.       .subscribe(({ userFollowed, userFollower }) => {
  61.         this.user = userFollowed;
  62.         this.followers = this.followers.filter((u: UserDTO) => u.id !== userFollower.id);
  63.         this.notificationService.warning(`You are no longer following ${userFollowed.username}.`);
  64.       })
  65.   }
  66.  
  67.   confirmDeleteUser(): void {
  68.     const modalRef = this.modalService.open(ConfirmationComponent);
  69.     modalRef.componentInstance.message = 'Are you sure you want to delete your profile?';
  70.  
  71.     modalRef
  72.       .componentInstance
  73.       .confirm
  74.       .subscribe((confirmed: boolean) => {
  75.         if (confirmed && this.user.id === this.loggedUserData.id) {
  76.           this.usersService.deleteUser(this.loggedUserData.id)
  77.             .subscribe((data: UserDTO) => {
  78.               modalRef.close();
  79.               this.notificationService.warning(`Goodbye ${data.username}. We will miss you...`);
  80.               this.router.navigate(['/home-world']);
  81.             });
  82.         } else {
  83.           modalRef.close();
  84.         }
  85.       })
  86.   }
  87.  
  88.   confirmUpdateUser(updateUserInfo): void {
  89.     const modalRef = this.modalService.open(ConfirmationComponent);
  90.     modalRef.componentInstance.message = 'Are you sure you want to update your account?';
  91.  
  92.     modalRef
  93.       .componentInstance
  94.       .confirm
  95.       .subscribe((confirmed: boolean) => {
  96.         if (
  97.           confirmed &&
  98.           (this.user.id === this.loggedUserData.id || this.loggedUserData.role === 'Admin')
  99.         ) {
  100.           this.spinner.show();
  101.           const username = updateUserInfo.newUsername || this.loggedUserData.username;
  102.           const email = updateUserInfo.newEmail || this.loggedUserData.email;
  103.           const password = updateUserInfo.newPassword || updateUserInfo.currentPassword;
  104.           const photo = updateUserInfo.photo || null;
  105.           const formData = new FormData();
  106.  
  107.           formData.append('username', username);
  108.           formData.append('email', email);
  109.           formData.append('password', password);
  110.           formData.append('photo', photo);
  111.  
  112.           this.usersService.updateUser(this.user.id, formData)
  113.             .subscribe(
  114.               (data: UserDTO) => {
  115.                 this.user = data;
  116.                 this.profilePhoto = [{ src: data.photoLink, caption: data.username, thumb: '' }]
  117.  
  118.                 modalRef.close();
  119.  
  120.                 if (this.loggedUserData.role === 'User') {
  121.                   this.authService.login({ username, password })
  122.                     .subscribe(
  123.                       () => {
  124.                         this.notificationService.success(`You have updated your account successfully!`);
  125.                         this.spinner.hide();
  126.                       },
  127.                       ({ error }) => {
  128.                         this.notificationService.error(error.message);
  129.                         this.spinner.hide();
  130.                       }
  131.                     );
  132.                 } else {
  133.                   this.spinner.hide();
  134.                 }
  135.               },
  136.               ({ error }) => {
  137.                 this.notificationService.error(error.message);
  138.                 modalRef.close();
  139.                 this.spinner.hide();
  140.               }
  141.             );
  142.         } else {
  143.           modalRef.close();
  144.         }
  145.       })
  146.   }
  147.  
  148.   ngOnInit(): void {
  149.     this.route.data
  150.       .subscribe(({ data }) => {
  151.         this.profilePhoto = [{ src: data.user.photoLink, caption: data.user.username, thumb: '' }]
  152.         this.user = data.user;
  153.         this.following = data.following;
  154.         this.followers = data.followers;
  155.       });
  156.  
  157.     this.loggedUserSubscription = this.authService.loggedUserData$
  158.       .subscribe((data: UserDTO) => {
  159.         this.loggedUserData = data;
  160.       });
  161.  
  162.     this.router.events.subscribe((evt) => {
  163.       if (!(evt instanceof NavigationEnd)) {
  164.         return;
  165.       }
  166.       window.scrollTo(0, 0)
  167.     });
  168.   }
  169.  
  170.   ngOnDestroy(): void {
  171.     this.loggedUserSubscription.unsubscribe();
  172.   }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement