forked from tabvn/angular2.github.api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
153 lines (104 loc) · 3.45 KB
/
Copy pathapp.component.ts
File metadata and controls
153 lines (104 loc) · 3.45 KB
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
import {Component, OnInit} from '@angular/core';
import {User} from "./models/user";
import {UserService} from "./services/user.service";
import {Subject} from "rxjs";
import {Title} from "@angular/platform-browser";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
defaultPageTitle = 'Welcome to Github Page';
title = this.defaultPageTitle;
cache = {
users: [],
selectedUser: [],
};
users: User[] = [];
search: Subject<string> = new Subject<string>();
selectedUser: User = new User();
loadingFollowers: boolean = false;
constructor(private pageTitle: Title, private userService: UserService) {
this.search.debounceTime(200).distinctUntilChanged().subscribe((searchTerm) => {
// call to user service and search by query
this.userService.search(searchTerm).subscribe(res => {
this.users = res.items as User[];
});
})
}
ngOnInit() {
this.userService.getUsers().subscribe(res => {
this.cache.users = res; // store cached for next time.
this.users = res;
}, error => {
console.log(error); // for development only.
});
}
/**
* On user typing key to search.
*/
onSearch(q: string) {
if (q !== "") {
this.go('home');
this.search.next(q);
this.setPageTitle("Searching for: " + q);
} else {
//if empty search box we restore first users
this.users = this.cache.users;
this.setPageTitle(this.defaultPageTitle);
}
}
setPageTitle(title: string) {
this.title = title;
this.pageTitle.setTitle(this.title);
}
go(s: string) {
if (s == 'home') {
this.selectedUser = new User();
this.users = this.cache.users;
this.setPageTitle(this.defaultPageTitle);
}
}
viewUser(user: User) {
this.selectedUser = user;
this.setPageTitle(user.login);
let userInCache: User = this.findUserInCache(user);
// let find if existing in cache we return and no longer call to api again
if (userInCache) {
this.selectedUser = userInCache;
} else {
// get followers of this user
this.loadingFollowers = true;
this.userService.getUserFollowers(user.login).subscribe(res => {
this.selectedUser.followers = res as User[];
this.cacheSelectUser(this.selectedUser);
this.loadingFollowers = false;
}, err => {
console.log(err);
this.loadingFollowers = false;
});
}
}
/**
* we storage selected user and dont again to api. just simply function for now.
* */
cacheSelectUser(user: User) {
if (!this.findUserInCache(user)) {
this.cache.selectedUser.push(user);
}
}
/**
* Find user if exist in cache we return user object
* @param user
* @returns User
*/
findUserInCache(user: User): User {
for (var i = 0; i < this.cache.selectedUser.length; i++) {
if (this.cache.selectedUser[i].login == user.login) {
return this.cache.selectedUser[i];
}
}
return null;
}
}