]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Component, OnInit } from '@angular/core'; |
41a2aee3 | 2 | import { Router, ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated'; |
dc8bc31b | 3 | |
32294074 C |
4 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; |
5 | ||
41a2aee3 C |
6 | import { |
7 | LoaderComponent, | |
8 | Pagination, | |
9 | SortField, | |
10 | Video, | |
11 | VideoService | |
4a6995be C |
12 | } from '../shared'; |
13 | import { AuthService, Search, SearchField, User } from '../../shared'; | |
501bc6c2 | 14 | import { VideoMiniatureComponent } from './video-miniature.component'; |
cf20596c | 15 | import { VideoSortComponent } from './video-sort.component'; |
dc8bc31b C |
16 | |
17 | @Component({ | |
18 | selector: 'my-videos-list', | |
4a6995be C |
19 | styles: [ require('./video-list.component.scss') ], |
20 | template: require('./video-list.component.html'), | |
f0a397ee | 21 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] |
dc8bc31b C |
22 | }) |
23 | ||
41a2aee3 | 24 | export class VideoListComponent implements OnInit { |
4fd8aa32 | 25 | loading = false; |
32294074 C |
26 | pagination: Pagination = { |
27 | currentPage: 1, | |
28 | itemsPerPage: 9, | |
29 | total: 0 | |
aff038cd | 30 | }; |
cf20596c | 31 | sort: SortField; |
4fd8aa32 C |
32 | user: User = null; |
33 | videos: Video[] = []; | |
dc8bc31b | 34 | |
471bc22f | 35 | private search: Search; |
98b01bac | 36 | |
dc8bc31b | 37 | constructor( |
ccf6ed16 | 38 | private authService: AuthService, |
4fd8aa32 | 39 | private router: Router, |
ccf6ed16 | 40 | private routeParams: RouteParams, |
4fd8aa32 | 41 | private videoService: VideoService |
98b01bac | 42 | ) { |
471bc22f | 43 | this.search = { |
ccf6ed16 C |
44 | value: this.routeParams.get('search'), |
45 | field: <SearchField>this.routeParams.get('field') | |
aff038cd | 46 | }; |
cf20596c | 47 | |
ccf6ed16 | 48 | this.sort = <SortField>this.routeParams.get('sort') || '-createdDate'; |
98b01bac | 49 | } |
dc8bc31b C |
50 | |
51 | ngOnInit() { | |
ccf6ed16 | 52 | if (this.authService.isLoggedIn()) { |
1553e15d C |
53 | this.user = User.load(); |
54 | } | |
55 | ||
dc8bc31b C |
56 | this.getVideos(); |
57 | } | |
58 | ||
59 | getVideos() { | |
157cb9c9 C |
60 | this.loading = true; |
61 | this.videos = []; | |
62 | ||
98b01bac C |
63 | let observable = null; |
64 | ||
471bc22f | 65 | if (this.search.value !== null) { |
ccf6ed16 | 66 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); |
98b01bac | 67 | } else { |
ccf6ed16 | 68 | observable = this.videoService.getVideos(this.pagination, this.sort); |
98b01bac C |
69 | } |
70 | ||
71 | observable.subscribe( | |
32294074 C |
72 | ({ videos, totalVideos }) => { |
73 | this.videos = videos; | |
74 | this.pagination.total = totalVideos; | |
4fd8aa32 | 75 | |
157cb9c9 | 76 | this.loading = false; |
32294074 | 77 | }, |
dc8bc31b C |
78 | error => alert(error) |
79 | ); | |
80 | } | |
81 | ||
9bfe96e1 C |
82 | noVideo() { |
83 | return !this.loading && this.videos.length === 0; | |
84 | } | |
85 | ||
ccf6ed16 | 86 | onRemoved(video: Video) { |
443ecefe | 87 | this.getVideos(); |
dc8bc31b C |
88 | } |
89 | ||
cf20596c C |
90 | onSort(sort: SortField) { |
91 | this.sort = sort; | |
a99593ed C |
92 | |
93 | const params: any = { | |
94 | sort: this.sort | |
95 | }; | |
96 | ||
97 | if (this.search.value) { | |
a99593ed | 98 | params.field = this.search.field; |
4fd8aa32 | 99 | params.search = this.search.value; |
a99593ed C |
100 | } |
101 | ||
ccf6ed16 | 102 | this.router.navigate(['VideosList', params]); |
cf20596c C |
103 | this.getVideos(); |
104 | } | |
dc8bc31b | 105 | } |