|
| 1 | +import {Component, ViewChild} from '@angular/core'; |
| 2 | +import {Http, Response} from '@angular/http'; |
| 3 | +import {DataSource} from '@angular/cdk'; |
| 4 | +import {MdPaginator, MdSort} from '@angular/material'; |
| 5 | +import {Observable} from 'rxjs/Observable'; |
| 6 | +import 'rxjs/add/operator/first'; |
| 7 | +import 'rxjs/add/operator/startWith'; |
| 8 | +import 'rxjs/add/operator/catch'; |
| 9 | +import 'rxjs/add/operator/switchMap'; |
| 10 | +import 'rxjs/add/observable/merge'; |
| 11 | +import 'rxjs/add/observable/of'; |
| 12 | +import 'rxjs/add/observable/interval'; |
| 13 | +import 'rxjs/add/operator/map'; |
| 14 | + |
| 15 | +@Component({ |
| 16 | + selector: 'table-http-example', |
| 17 | + styleUrls: ['table-http-example.css'], |
| 18 | + templateUrl: 'table-http-example.html', |
| 19 | +}) |
| 20 | +export class TableHttpExample { |
| 21 | + displayedColumns = ['created', 'state', 'number', 'title']; |
| 22 | + exampleDatabase: ExampleHttpDao | null; |
| 23 | + dataSource: ExampleDataSource | null; |
| 24 | + |
| 25 | + @ViewChild(MdPaginator) paginator: MdPaginator; |
| 26 | + @ViewChild(MdSort) sort: MdSort; |
| 27 | + |
| 28 | + constructor(http: Http) { |
| 29 | + this.exampleDatabase = new ExampleHttpDao(http); |
| 30 | + } |
| 31 | + |
| 32 | + ngOnInit() { |
| 33 | + this.dataSource = new ExampleDataSource(this.exampleDatabase!, |
| 34 | + this.sort, this.paginator); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export interface GithubIssue { |
| 39 | + number: string; |
| 40 | + state: string; |
| 41 | + title: string; |
| 42 | + created: Date; |
| 43 | +} |
| 44 | + |
| 45 | +/** An example database that the data source uses to retrieve data for the table. */ |
| 46 | +export class ExampleHttpDao { |
| 47 | + constructor(private http: Http) {} |
| 48 | + |
| 49 | + getRepoIssues(sort: string, order: string, page: number): Observable<Response> { |
| 50 | + const href = 'https://api.github.com/search/issues'; |
| 51 | + const requestUrl = |
| 52 | + `${href}?q=repo:angular/material2&sort=${sort}&order=${order}&page=${page + 1}`; |
| 53 | + return this.http.get(requestUrl); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Data source to provide what data should be rendered in the table. Note that the data source |
| 59 | + * can retrieve its data in any way. In this case, the data source is provided a reference |
| 60 | + * to a common data base, ExampleHttpDao. It is not the data source's responsibility to manage |
| 61 | + * the underlying data. Instead, it only needs to take the data and send the table exactly what |
| 62 | + * should be rendered. |
| 63 | + */ |
| 64 | +export class ExampleDataSource extends DataSource<GithubIssue> { |
| 65 | + // The number of issues returned by github matching the query. |
| 66 | + resultsLength: number = 0; |
| 67 | + isLoadingResults: boolean; |
| 68 | + isRateLimitReached: boolean; |
| 69 | + |
| 70 | + constructor(private _exampleDatabase: ExampleHttpDao, |
| 71 | + private _sort: MdSort, |
| 72 | + private _paginator: MdPaginator) { |
| 73 | + super(); |
| 74 | + } |
| 75 | + |
| 76 | + /** Connect function called by the table to retrieve one stream containing the data to render. */ |
| 77 | + connect(): Observable<GithubIssue[]> { |
| 78 | + const displayDataChanges = [ |
| 79 | + this._sort.mdSortChange, |
| 80 | + this._paginator.page, |
| 81 | + ]; |
| 82 | + |
| 83 | + // If the user changes the sort order, reset back to the first page. |
| 84 | + this._sort.mdSortChange.subscribe(() => { |
| 85 | + this._paginator.pageIndex = 0; |
| 86 | + }); |
| 87 | + |
| 88 | + return Observable.merge(...displayDataChanges) |
| 89 | + .startWith(null) |
| 90 | + .switchMap(() => { |
| 91 | + this.isLoadingResults = true; |
| 92 | + return this._exampleDatabase.getRepoIssues( |
| 93 | + this._sort.active, this._sort.direction, this._paginator.pageIndex); |
| 94 | + }) |
| 95 | + .catch(() => { |
| 96 | + // Catch if the GitHub API has reached its rate limit. Return empty result. |
| 97 | + this.isRateLimitReached = true; |
| 98 | + return Observable.of(null); |
| 99 | + }) |
| 100 | + .map(result => { |
| 101 | + // Flip flag to show that loading has finished. |
| 102 | + this.isLoadingResults = false; |
| 103 | + return result; |
| 104 | + }) |
| 105 | + .map(result => { |
| 106 | + if (!result) { return []; } |
| 107 | + |
| 108 | + this.isRateLimitReached = false; |
| 109 | + this.resultsLength = result.json().total_count; |
| 110 | + |
| 111 | + return this.readGithubResult(result); |
| 112 | + }); |
| 113 | + |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + disconnect() {} |
| 118 | + |
| 119 | + private readGithubResult(result: Response): GithubIssue[] { |
| 120 | + return result.json().items.map(issue => { |
| 121 | + return { |
| 122 | + number: issue.number, |
| 123 | + created: new Date(issue.created_at), |
| 124 | + state: issue.state, |
| 125 | + title: issue.title, |
| 126 | + }; |
| 127 | + }); |
| 128 | + } |
| 129 | +} |
0 commit comments