]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core' |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
33c4972d | 3 | import { Subscription } from 'rxjs/Subscription' |
df98563e | 4 | import { BehaviorSubject } from 'rxjs/BehaviorSubject' |
dc8bc31b | 5 | |
df98563e | 6 | import { NotificationsService } from 'angular2-notifications' |
7ddd02c9 | 7 | |
41a2aee3 | 8 | import { |
41a2aee3 C |
9 | SortField, |
10 | Video, | |
d592e0a9 C |
11 | VideoService, |
12 | VideoPagination | |
df98563e C |
13 | } from '../shared' |
14 | import { AuthService, AuthUser } from '../../core' | |
d592e0a9 | 15 | import { Search, SearchField, SearchService } from '../../shared' |
dc8bc31b C |
16 | |
17 | @Component({ | |
18 | selector: 'my-videos-list', | |
ec8d8440 C |
19 | styleUrls: [ './video-list.component.scss' ], |
20 | templateUrl: './video-list.component.html' | |
dc8bc31b | 21 | }) |
0629423c | 22 | export class VideoListComponent implements OnInit, OnDestroy { |
df98563e | 23 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false) |
d592e0a9 | 24 | pagination: VideoPagination = { |
32294074 | 25 | currentPage: 1, |
383bfc83 | 26 | itemsPerPage: 25, |
0629423c | 27 | totalItems: null |
df98563e C |
28 | } |
29 | sort: SortField | |
30 | user: AuthUser = null | |
31 | videos: Video[] = [] | |
dc8bc31b | 32 | |
df98563e | 33 | private search: Search |
33c4972d C |
34 | private subActivatedRoute: Subscription |
35 | private subSearch: Subscription | |
98b01bac | 36 | |
df98563e | 37 | constructor ( |
7ddd02c9 | 38 | private notificationsService: NotificationsService, |
ccf6ed16 | 39 | private authService: AuthService, |
0629423c | 40 | private changeDetector: ChangeDetectorRef, |
4fd8aa32 | 41 | private router: Router, |
0629423c | 42 | private route: ActivatedRoute, |
00a44645 | 43 | private videoService: VideoService, |
0629423c | 44 | private searchService: SearchService |
00a44645 | 45 | ) {} |
dc8bc31b | 46 | |
df98563e | 47 | ngOnInit () { |
bddab65a | 48 | if (this.authService.isLoggedIn()) { |
df98563e | 49 | this.user = AuthUser.load() |
bddab65a | 50 | } |
1553e15d | 51 | |
bddab65a C |
52 | // Subscribe to route changes |
53 | this.subActivatedRoute = this.route.params.subscribe(routeParams => { | |
df98563e | 54 | this.loadRouteParams(routeParams) |
00a44645 | 55 | |
0629423c | 56 | // Update the search service component |
df98563e C |
57 | this.searchService.updateSearch.next(this.search) |
58 | this.getVideos() | |
59 | }) | |
00a44645 | 60 | |
bddab65a C |
61 | // Subscribe to search changes |
62 | this.subSearch = this.searchService.searchUpdated.subscribe(search => { | |
df98563e | 63 | this.search = search |
7eef9535 | 64 | // Reset pagination |
df98563e | 65 | this.pagination.currentPage = 1 |
0629423c | 66 | |
df98563e C |
67 | this.navigateToNewParams() |
68 | }) | |
0629423c | 69 | } |
00a44645 | 70 | |
df98563e C |
71 | ngOnDestroy () { |
72 | this.subActivatedRoute.unsubscribe() | |
73 | this.subSearch.unsubscribe() | |
dc8bc31b C |
74 | } |
75 | ||
df98563e C |
76 | getVideos () { |
77 | this.loading.next(true) | |
78 | this.videos = [] | |
0629423c | 79 | |
df98563e | 80 | let observable = null |
00a44645 | 81 | if (this.search.value) { |
df98563e | 82 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort) |
98b01bac | 83 | } else { |
df98563e | 84 | observable = this.videoService.getVideos(this.pagination, this.sort) |
98b01bac C |
85 | } |
86 | ||
87 | observable.subscribe( | |
32294074 | 88 | ({ videos, totalVideos }) => { |
df98563e C |
89 | this.videos = videos |
90 | this.pagination.totalItems = totalVideos | |
4fd8aa32 | 91 | |
df98563e | 92 | this.loading.next(false) |
32294074 | 93 | }, |
7ddd02c9 | 94 | error => this.notificationsService.error('Error', error.text) |
df98563e | 95 | ) |
dc8bc31b C |
96 | } |
97 | ||
df98563e C |
98 | isThereNoVideo () { |
99 | return !this.loading.getValue() && this.videos.length === 0 | |
bddab65a C |
100 | } |
101 | ||
33c4972d | 102 | onPageChanged (event: { page: number }) { |
bddab65a | 103 | // Be sure the current page is set |
df98563e | 104 | this.pagination.currentPage = event.page |
bddab65a | 105 | |
df98563e | 106 | this.navigateToNewParams() |
9bfe96e1 C |
107 | } |
108 | ||
df98563e C |
109 | onSort (sort: SortField) { |
110 | this.sort = sort | |
a99593ed | 111 | |
df98563e | 112 | this.navigateToNewParams() |
bddab65a C |
113 | } |
114 | ||
df98563e | 115 | private buildRouteParams () { |
bddab65a | 116 | // There is always a sort and a current page |
33c4972d | 117 | const params = { |
bddab65a C |
118 | sort: this.sort, |
119 | page: this.pagination.currentPage | |
df98563e | 120 | } |
a99593ed | 121 | |
bddab65a | 122 | // Maybe there is a search |
a99593ed | 123 | if (this.search.value) { |
33c4972d C |
124 | params['field'] = this.search.field |
125 | params['search'] = this.search.value | |
a99593ed C |
126 | } |
127 | ||
df98563e | 128 | return params |
bddab65a C |
129 | } |
130 | ||
33c4972d | 131 | private loadRouteParams (routeParams: { [ key: string ]: any }) { |
bddab65a C |
132 | if (routeParams['search'] !== undefined) { |
133 | this.search = { | |
134 | value: routeParams['search'], | |
df98563e C |
135 | field: routeParams['field'] as SearchField |
136 | } | |
bddab65a C |
137 | } else { |
138 | this.search = { | |
139 | value: '', | |
140 | field: 'name' | |
df98563e | 141 | } |
bddab65a C |
142 | } |
143 | ||
df98563e | 144 | this.sort = routeParams['sort'] as SortField || '-createdAt' |
bddab65a | 145 | |
7eef9535 | 146 | if (routeParams['page'] !== undefined) { |
df98563e | 147 | this.pagination.currentPage = parseInt(routeParams['page'], 10) |
7eef9535 | 148 | } else { |
df98563e | 149 | this.pagination.currentPage = 1 |
7eef9535 | 150 | } |
bddab65a C |
151 | } |
152 | ||
df98563e C |
153 | private navigateToNewParams () { |
154 | const routeParams = this.buildRouteParams() | |
d592e0a9 | 155 | this.router.navigate([ '/videos/list', routeParams ]) |
00a44645 | 156 | } |
dc8bc31b | 157 | } |