Website Summary Files Commits Branches @commit @diff @stats
path: root / src / DevHive.Angular / src / app / components / profile / profile.component.ts
blob: d9f94e85267481daece981c34ba6b7cc35af94ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { UserService } from 'src/app/services/user.service';
import { User } from 'src/models/identity/user';
import { AppConstants } from 'src/app/app-constants.module';
import {HttpErrorResponse} from '@angular/common/http';
import {Location} from '@angular/common';
import {LanguageService} from 'src/app/services/language.service';
import {TechnologyService} from 'src/app/services/technology.service';
import {Post} from 'src/models/post';
import {FeedService} from 'src/app/services/feed.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
  private _urlUsername: string;
  public loggedInUser = false;
  public dataArrived = false;
  public user: User;
  public userPosts: Post[] = [];
  public showNoLangMsg = false;
  public showNoTechMsg = false;

  constructor(private _router: Router, private _userService: UserService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location)
  { }

  private setDefaultUser(): void {
    this.user = this._userService.getDefaultUser();
  }

  ngOnInit(): void {
    this._urlUsername = this._router.url.substring(9);
    this.user = this._userService.getDefaultUser();

    this._userService.getUserByUsernameRequest(this._urlUsername).subscribe(
      (res: object) => this.loadLanguages(res),
      (err: HttpErrorResponse) => { this._router.navigate(['/not-found']); }
    );
  }

  private loadLanguages(res: object): void {
    Object.assign(this.user, res);

    if (this.user.languages.length > 0) {
      // When user has languages, get their names and load technologies
      this._languageService.getFullLanguagesFromIncomplete(this.user.languages)
        .then(value => {
          this.user.languages = value;
          this.loadTechnologies();
       });
    }
    else {
      this.showNoLangMsg = true;
      this.loadTechnologies();
    }
  }

  private loadTechnologies(): void {
    if (this.user.technologies.length > 0) {
      // When user has technologies, get their names and then load posts
      this._technologyService.getFullTechnologiesFromIncomplete(this.user.technologies)
        .then(value => {
          this.user.technologies = value;
          this.loadPosts();
        });
    }
    else {
      this.showNoTechMsg = true;
      this.loadPosts();
    }
  }

  private loadPosts(): void {
    const now = new Date();
    now.setHours(now.getHours() + 2); // accounting for eastern europe timezone

    this._feedService.getUserPostsRequest(this.user.userName, 1, now.toISOString(), AppConstants.PAGE_SIZE).subscribe(
      (result: object) => {
        this.userPosts = Object.values(result)[0];
        this.finishUserLoading();
      }
    );
  }

  private finishUserLoading(): void {
    if (this.user.imageUrl === '') {
      this.user.imageUrl = AppConstants.FALLBACK_PROFILE_ICON;
    }

    if (sessionStorage.getItem('UserCred')) {
      const userFromToken: User = this._userService.getDefaultUser();

      this._userService.getUserFromSessionStorageRequest().subscribe(
        (tokenRes: object) => {
          Object.assign(userFromToken, tokenRes);

          if (userFromToken.userName === this._urlUsername) {
            this.loggedInUser = true;
          }
          this.dataArrived = true;
        },
        (err: HttpErrorResponse) => this.bailOnBadToken()
      );
    }
    else {
      this.dataArrived = true;
    }
  }

  private bailOnBadToken(): void {
    this._userService.logoutUserFromSessionStorage();
    this._router.navigate(['/login']);
  }

  goBack(): void {
    const currURL = this._location.path();
    this._location.back();
    if (this._location.path() === currURL) {
      this._router.navigate(['/']);
    }
  }

  navigateToSettings(): void {
    this._router.navigate([this._router.url + '/settings']);
  }

  logout(): void {
    this._userService.logoutUserFromSessionStorage();

    // Reload the page
    this._router.routeReuseStrategy.shouldReuseRoute = () => false;
    this._router.onSameUrlNavigation = 'reload';
    this._router.navigate([this._router.url]);
  }
}