diff options
Diffstat (limited to 'client/src/app/search/search.component.ts')
-rw-r--r-- | client/src/app/search/search.component.ts | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/client/src/app/search/search.component.ts b/client/src/app/search/search.component.ts new file mode 100644 index 000000000..be1cb3689 --- /dev/null +++ b/client/src/app/search/search.component.ts | |||
@@ -0,0 +1,93 @@ | |||
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | ||
2 | import { ActivatedRoute } from '@angular/router' | ||
3 | import { RedirectService } from '@app/core' | ||
4 | import { NotificationsService } from 'angular2-notifications' | ||
5 | import { Subscription } from 'rxjs' | ||
6 | import { SearchService } from '@app/search/search.service' | ||
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | ||
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
9 | import { Video } from '../../../../shared' | ||
10 | import { MetaService } from '@ngx-meta/core' | ||
11 | |||
12 | @Component({ | ||
13 | selector: 'my-search', | ||
14 | styleUrls: [ './search.component.scss' ], | ||
15 | templateUrl: './search.component.html' | ||
16 | }) | ||
17 | export class SearchComponent implements OnInit, OnDestroy { | ||
18 | videos: Video[] = [] | ||
19 | pagination: ComponentPagination = { | ||
20 | currentPage: 1, | ||
21 | itemsPerPage: 10, // It's per object type (so 10 videos, 10 video channels etc) | ||
22 | totalItems: null | ||
23 | } | ||
24 | |||
25 | private subActivatedRoute: Subscription | ||
26 | private currentSearch: string | ||
27 | |||
28 | constructor ( | ||
29 | private i18n: I18n, | ||
30 | private route: ActivatedRoute, | ||
31 | private metaService: MetaService, | ||
32 | private redirectService: RedirectService, | ||
33 | private notificationsService: NotificationsService, | ||
34 | private searchService: SearchService | ||
35 | ) { } | ||
36 | |||
37 | ngOnInit () { | ||
38 | this.subActivatedRoute = this.route.queryParams.subscribe( | ||
39 | queryParams => { | ||
40 | const querySearch = queryParams['search'] | ||
41 | |||
42 | if (!querySearch) return this.redirectService.redirectToHomepage() | ||
43 | if (querySearch === this.currentSearch) return | ||
44 | |||
45 | this.currentSearch = querySearch | ||
46 | this.updateTitle() | ||
47 | |||
48 | this.reload() | ||
49 | }, | ||
50 | |||
51 | err => this.notificationsService.error('Error', err.text) | ||
52 | ) | ||
53 | } | ||
54 | |||
55 | ngOnDestroy () { | ||
56 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | ||
57 | } | ||
58 | |||
59 | search () { | ||
60 | return this.searchService.searchVideos(this.currentSearch, this.pagination) | ||
61 | .subscribe( | ||
62 | ({ videos, totalVideos }) => { | ||
63 | this.videos = this.videos.concat(videos) | ||
64 | this.pagination.totalItems = totalVideos | ||
65 | }, | ||
66 | |||
67 | error => { | ||
68 | this.notificationsService.error(this.i18n('Error'), error.message) | ||
69 | } | ||
70 | ) | ||
71 | } | ||
72 | |||
73 | onNearOfBottom () { | ||
74 | // Last page | ||
75 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | ||
76 | |||
77 | this.pagination.currentPage += 1 | ||
78 | this.search() | ||
79 | } | ||
80 | |||
81 | private reload () { | ||
82 | this.pagination.currentPage = 1 | ||
83 | this.pagination.totalItems = null | ||
84 | |||
85 | this.videos = [] | ||
86 | |||
87 | this.search() | ||
88 | } | ||
89 | |||
90 | private updateTitle () { | ||
91 | this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch) | ||
92 | } | ||
93 | } | ||