diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-22 10:43:06 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-22 10:44:35 +0200 |
commit | 322940742b4dca168de6dfed0d1ebf5926dab528 (patch) | |
tree | 7b7fdb77595aa7d6a89d1045ab6012a9e1d68aab /client/angular/videos/videos.service.ts | |
parent | 46246b5f194caafba4e3a72e9365acd8b35785de (diff) | |
download | PeerTube-322940742b4dca168de6dfed0d1ebf5926dab528.tar.gz PeerTube-322940742b4dca168de6dfed0d1ebf5926dab528.tar.zst PeerTube-322940742b4dca168de6dfed0d1ebf5926dab528.zip |
Add pagination support to the client
Diffstat (limited to 'client/angular/videos/videos.service.ts')
-rw-r--r-- | client/angular/videos/videos.service.ts | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/client/angular/videos/videos.service.ts b/client/angular/videos/videos.service.ts index f4790b511..94ef418eb 100644 --- a/client/angular/videos/videos.service.ts +++ b/client/angular/videos/videos.service.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Http, Response } from '@angular/http'; | 2 | import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http'; |
3 | import { Observable } from 'rxjs/Rx'; | 3 | import { Observable } from 'rxjs/Rx'; |
4 | 4 | ||
5 | import { Pagination } from './pagination'; | ||
5 | import { Video } from './video'; | 6 | import { Video } from './video'; |
6 | import { AuthService } from '../users/services/auth.service'; | 7 | import { AuthService } from '../users/services/auth.service'; |
7 | 8 | ||
@@ -11,8 +12,9 @@ export class VideosService { | |||
11 | 12 | ||
12 | constructor (private http: Http, private _authService: AuthService) {} | 13 | constructor (private http: Http, private _authService: AuthService) {} |
13 | 14 | ||
14 | getVideos() { | 15 | getVideos(pagination: Pagination) { |
15 | return this.http.get(this._baseVideoUrl) | 16 | const params = { search: this.createPaginationParams(pagination) }; |
17 | return this.http.get(this._baseVideoUrl, params) | ||
16 | .map(res => res.json()) | 18 | .map(res => res.json()) |
17 | .map(this.extractVideos) | 19 | .map(this.extractVideos) |
18 | .catch(this.handleError); | 20 | .catch(this.handleError); |
@@ -31,24 +33,38 @@ export class VideosService { | |||
31 | .catch(this.handleError); | 33 | .catch(this.handleError); |
32 | } | 34 | } |
33 | 35 | ||
34 | searchVideos(search: string) { | 36 | searchVideos(search: string, pagination: Pagination) { |
35 | return this.http.get(this._baseVideoUrl + 'search/' + search) | 37 | const params = { search: this.createPaginationParams(pagination) }; |
38 | return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search), params) | ||
36 | .map(res => res.json()) | 39 | .map(res => res.json()) |
37 | .map(this.extractVideos) | 40 | .map(this.extractVideos) |
38 | .catch(this.handleError); | 41 | .catch(this.handleError); |
39 | } | 42 | } |
40 | 43 | ||
41 | private extractVideos (body: any[]) { | 44 | private extractVideos (body: any) { |
45 | const videos_json = body.data; | ||
46 | const totalVideos = body.total; | ||
42 | const videos = []; | 47 | const videos = []; |
43 | for (const video_json of body) { | 48 | for (const video_json of videos_json) { |
44 | videos.push(new Video(video_json)); | 49 | videos.push(new Video(video_json)); |
45 | } | 50 | } |
46 | 51 | ||
47 | return videos; | 52 | return { videos, totalVideos }; |
48 | } | 53 | } |
49 | 54 | ||
50 | private handleError (error: Response) { | 55 | private handleError (error: Response) { |
51 | console.error(error); | 56 | console.error(error); |
52 | return Observable.throw(error.json().error || 'Server error'); | 57 | return Observable.throw(error.json().error || 'Server error'); |
53 | } | 58 | } |
59 | |||
60 | private createPaginationParams(pagination: Pagination) { | ||
61 | const params = new URLSearchParams(); | ||
62 | const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage; | ||
63 | const count: number = pagination.itemsPerPage; | ||
64 | |||
65 | params.set('start', start.toString()); | ||
66 | params.set('count', count.toString()); | ||
67 | |||
68 | return params; | ||
69 | } | ||
54 | } | 70 | } |