allfree-angular-frontend / src / app / profile / profile.component.ts
profile.component.ts
Raw
import {UserLogged, User} from './../models/models';
import {Component, OnInit} from '@angular/core';
import {TokenStorageService} from '../_services/jwt-service/token-storage.service';
import {Router} from '@angular/router';
import {UserService} from '../_services/user-service/user.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss']
})
export class ProfileComponent implements OnInit {

  currentUser: User;
  userCountArticles: any = 0;
  userCountFavArticles: any = 0;
  userCountPendingArticles: any = 0;

  constructor(private token: TokenStorageService,
              private router: Router,
              private userService: UserService
  ) {
  }

  ngOnInit(): void {
    this.userService.getUserById
    (this.token.getUser().userDetails.id).subscribe(value => this.currentUser = value);
    this.getUserStats();
    console.log('user logged', this.currentUser);
  }

  updateUser() {
    this.router.navigate(['update-user', this.currentUser.id, this.currentUser.roles[0].name]);
  }

  getUserStats() {
    let user;
    this.userService.getUserById(this.currentUser.id).subscribe(value => {
        user = value;
        this.userCountArticles = value.articles.length ?? 0;
        this.userCountFavArticles = value.favArticles.length ?? 0;
      },
      error => {
        console.log(error);
      });
    if (user == null || user == undefined || user.articles == null) {
      console.log('null user');
    }
  }

}