]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
Don't throw an error on empty search
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, RedirectService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { forkJoin, 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 { MetaService } from '@ngx-meta/core'
10 import { AdvancedSearch } from '@app/search/advanced-search.model'
11 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
12 import { immutableAssign } from '@app/shared/misc/utils'
13 import { Video } from '@app/shared/video/video.model'
14
15 @Component({
16 selector: 'my-search',
17 styleUrls: [ './search.component.scss' ],
18 templateUrl: './search.component.html'
19 })
20 export class SearchComponent implements OnInit, OnDestroy {
21 results: (Video | VideoChannel)[] = []
22
23 pagination: ComponentPagination = {
24 currentPage: 1,
25 itemsPerPage: 10, // Only for videos, use another variable for channels
26 totalItems: null
27 }
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
30 currentSearch: string
31
32 private subActivatedRoute: Subscription
33 private isInitialLoad = false // set to false to show the search filters on first arrival
34 private firstSearch = true
35
36 private channelsPerPage = 2
37
38 constructor (
39 private i18n: I18n,
40 private route: ActivatedRoute,
41 private router: Router,
42 private metaService: MetaService,
43 private redirectService: RedirectService,
44 private notificationsService: NotificationsService,
45 private searchService: SearchService,
46 private authService: AuthService
47 ) { }
48
49 ngOnInit () {
50 this.subActivatedRoute = this.route.queryParams.subscribe(
51 queryParams => {
52 const querySearch = queryParams['search']
53
54 // Search updated, reset filters
55 if (this.currentSearch !== querySearch) {
56 this.resetPagination()
57 this.advancedSearch.reset()
58
59 this.currentSearch = querySearch || undefined
60 this.updateTitle()
61 }
62
63 this.advancedSearch = new AdvancedSearch(queryParams)
64
65 // Don't hide filters if we have some of them AND the user just came on the webpage
66 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
67 this.isInitialLoad = false
68
69 this.search()
70 },
71
72 err => this.notificationsService.error('Error', err.text)
73 )
74 }
75
76 ngOnDestroy () {
77 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
78 }
79
80 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
81 return d instanceof VideoChannel
82 }
83
84 isVideo (v: VideoChannel | Video): v is Video {
85 return v instanceof Video
86 }
87
88 isUserLoggedIn () {
89 return this.authService.isLoggedIn()
90 }
91
92 search () {
93 forkJoin([
94 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
95 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
96 ])
97 .subscribe(
98 ([ videosResult, videoChannelsResult ]) => {
99 this.results = this.results
100 .concat(videoChannelsResult.data)
101 .concat(videosResult.videos)
102 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
103
104 // Focus on channels if there are no enough videos
105 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
106 this.resetPagination()
107 this.firstSearch = false
108
109 this.channelsPerPage = 10
110 this.search()
111 }
112
113 this.firstSearch = false
114 },
115
116 error => {
117 this.notificationsService.error(this.i18n('Error'), error.message)
118 }
119 )
120
121 }
122
123 onNearOfBottom () {
124 // Last page
125 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
126
127 this.pagination.currentPage += 1
128 this.search()
129 }
130
131 onFiltered () {
132 this.resetPagination()
133
134 this.updateUrlFromAdvancedSearch()
135 }
136
137 numberOfFilters () {
138 return this.advancedSearch.size()
139 }
140
141 private resetPagination () {
142 this.pagination.currentPage = 1
143 this.pagination.totalItems = null
144 this.channelsPerPage = 2
145
146 this.results = []
147 }
148
149 private updateTitle () {
150 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
151 }
152
153 private updateUrlFromAdvancedSearch () {
154 const search = this.currentSearch || undefined
155
156 this.router.navigate([], {
157 relativeTo: this.route,
158 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
159 })
160 }
161 }