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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
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.model';
import { AppConstants } from 'src/app/app-constants.module';
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.model';
import { FeedService } from 'src/app/services/feed.service';
import { TokenService } from 'src/app/services/token.service';
import { Title } from '@angular/platform-browser';
import { FriendService } from 'src/app/services/friend.service';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
private _title = 'Profile';
private _urlUsername: string;
private _timeLoaded: string;
private _currentPage: number;
public isTheLoggedInUser = false;
public isUserLoggedIn = false;
public isAdminUser = false;
public dataArrived = false;
public friendOfUser = false;
public user: User;
public userPosts: Post[];
constructor(private _titleService: Title, private _router: Router, private _userService: UserService, private _friendService: FriendService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) {
this._titleService.setTitle(this._title);
}
ngOnInit(): void {
this._urlUsername = this._router.url.substring(9);
const now = new Date();
now.setHours(now.getHours() + 3); // accounting for eastern europe timezone
this._timeLoaded = now.toISOString();
this._currentPage = 1;
this.user = this._userService.getDefaultUser();
this.userPosts = [];
this._userService.getUserByUsernameRequest(this._urlUsername).subscribe({
next: (res: object) => {
Object.assign(this.user, res);
this.isAdminUser = this.user.roles.map(x => x.name).includes(AppConstants.ADMIN_ROLE_NAME);
this.loadLanguages();
},
error: () => {
this._router.navigate(['/not-found']);
}
});
}
private loadLanguages(): void {
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.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.loadPosts();
}
}
private loadPosts(): void {
this._feedService.getUserPostsRequest(this.user.userName, this._currentPage++, this._timeLoaded, AppConstants.PAGE_SIZE).subscribe({
next: (result: object) => {
const resultArr: Post[] = Object.values(result)[0];
this.userPosts.push(...resultArr);
this.finishUserLoading();
},
error: () => {
this._currentPage = -1;
this.finishUserLoading();
}
});
}
private finishUserLoading(): void {
if (sessionStorage.getItem('UserCred')) {
this.isUserLoggedIn = true;
const userFromToken: User = this._userService.getDefaultUser();
this._userService.getUserFromSessionStorageRequest().subscribe({
next: (tokenRes: object) => {
Object.assign(userFromToken, tokenRes);
if (userFromToken.friends.map(x => x.userName).includes(this._urlUsername)) {
this.friendOfUser = true;
}
if (userFromToken.userName === this._urlUsername) {
this.isTheLoggedInUser = true;
}
this.dataArrived = true;
},
error: () => {
this.logout();
}
});
}
else {
this.dataArrived = true;
}
}
logout(): void {
this._tokenService.logoutUserFromSessionStorage();
// Reload the page
this._router.routeReuseStrategy.shouldReuseRoute = () => false;
this._router.onSameUrlNavigation = 'reload';
this._router.navigate([this._router.url]);
}
modifyFriend(): void {
this.dataArrived = false;
if (this.friendOfUser) {
this.removeFriendFromLoggedInUser();
}
else {
this.addFriendToLoggedInUser();
}
}
private addFriendToLoggedInUser(): void {
this._friendService.postFriendWithSessionStorageRequest(this.user.userName).subscribe({
next: () => {
this.reloadPage();
},
error: () => {
this._router.navigate(['/']);
}
});
}
private removeFriendFromLoggedInUser(): void {
this._friendService.deleteFriendWithSessionStorageRequest(this.user.userName).subscribe({
next: () => {
this.reloadPage();
},
error: () => {
this._router.navigate(['/']);
}
});
}
onScroll(event: any): void {
// Detects when the element has reached the bottom, thx https://stackoverflow.com/a/50038429/12036073
if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight && this._currentPage > 0) {
this.loadPosts();
}
}
private reloadPage(): void {
this._router.routeReuseStrategy.shouldReuseRoute = () => false;
this._router.onSameUrlNavigation = 'reload';
this._router.navigate([this._router.url]);
}
}
|