]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { RedirectService } from '@app/core' | |
4 | import { immutableAssign } from '@app/shared/misc/utils' | |
5 | import { NotificationsService } from 'angular2-notifications' | |
6 | import { Subscription } from 'rxjs/Subscription' | |
7 | import { AuthService } from '../../core/auth' | |
8 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' | |
9 | import { VideoService } from '../../shared/video/video.service' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-videos-search', | |
13 | styleUrls: [ '../../shared/video/abstract-video-list.scss' ], | |
14 | templateUrl: '../../shared/video/abstract-video-list.html' | |
15 | }) | |
16 | export class VideoSearchComponent extends AbstractVideoList implements OnInit, OnDestroy { | |
17 | titlePage = 'Search' | |
18 | currentRoute = '/videos/search' | |
19 | loadOnInit = false | |
20 | ||
21 | protected otherRouteParams = { | |
22 | search: '' | |
23 | } | |
24 | private subActivatedRoute: Subscription | |
25 | ||
26 | constructor (protected router: Router, | |
27 | protected route: ActivatedRoute, | |
28 | protected notificationsService: NotificationsService, | |
29 | protected authService: AuthService, | |
30 | private videoService: VideoService, | |
31 | private redirectService: RedirectService | |
32 | ) { | |
33 | super() | |
34 | } | |
35 | ||
36 | ngOnInit () { | |
37 | super.ngOnInit() | |
38 | ||
39 | this.subActivatedRoute = this.route.queryParams.subscribe( | |
40 | queryParams => { | |
41 | const querySearch = queryParams['search'] | |
42 | ||
43 | if (!querySearch) return this.redirectService.redirectToHomepage() | |
44 | if (this.otherRouteParams.search === querySearch) return | |
45 | ||
46 | this.otherRouteParams.search = querySearch | |
47 | this.reloadVideos() | |
48 | }, | |
49 | ||
50 | err => this.notificationsService.error('Error', err.text) | |
51 | ) | |
52 | } | |
53 | ||
54 | ngOnDestroy () { | |
55 | super.ngOnDestroy() | |
56 | ||
57 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
58 | } | |
59 | ||
60 | getVideosObservable (page: number) { | |
61 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
62 | return this.videoService.searchVideos(this.otherRouteParams.search, newPagination, this.sort) | |
63 | } | |
64 | } |