-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.ts
78 lines (70 loc) · 2.31 KB
/
app.component.ts
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
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DatePipe } from '@angular/common';
import { Title } from '@angular/platform-browser';
import { tap } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
repositories: any[] = [];
isLoading: boolean = true;
private currentPage: number = 1;
private repositoriesCreationDate: string = '';
constructor(private title: Title, private httpClient: HttpClient, private datePipe: DatePipe) {
// Set title
this.title.setTitle('Latest Github Repositories');
// Set repositories creation date
const date = new Date(new Date().setDate(new Date().getDate() - 30));
this.repositoriesCreationDate = this.datePipe.transform(date, 'yyyy-MM-dd');
}
ngOnInit() {
this.loadRepositories();
}
onScroll() {
if (!this.isLoading) {
this.loadRepositories();
}
}
private loadRepositories() {
this.isLoading = true;
this.getRepositories(
this.repositoriesCreationDate,
this.currentPage,
(repositories) => {
this.repositories = this.repositories.concat(repositories.items);
console.log(this.repositories);
},
(error) => {
console.log(error);
},
() => {
this.isLoading = false;
});
}
private getRepositories(date: string, page: number = 1, successCallback: (repositories) => void, errorCallback: (error) => void, completeCallback: () => void) {
const requestUrl = `https://api.github.com/search/repositories?q=created:>${date}&sort=stars&order=desc&page=${page}`;
return this.httpClient.get(requestUrl).pipe(
tap(() => {
console.log(requestUrl);
})
).subscribe((repositories: any[]) => {
this.currentPage++;
successCallback(repositories);
}, (error) => {
errorCallback(error);
}, () => {
completeCallback();
});
}
// stolen from: https://stackoverflow.com/a/3224854
dateInterval(dateString) {
const date = new Date(dateString);
const today = new Date();
const diffTime = Math.abs(date.getTime() - today.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
}
}