Fix search results on mobile
[oweals/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 = true
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         // New empty search
55         if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
56
57         // Search updated, reset filters
58         if (this.currentSearch !== querySearch) {
59           this.resetPagination()
60           this.advancedSearch.reset()
61
62           this.currentSearch = querySearch
63           this.updateTitle()
64         }
65
66         this.advancedSearch = new AdvancedSearch(queryParams)
67
68         // Don't hide filters if we have some of them AND the user just came on the webpage
69         this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
70         this.isInitialLoad = false
71
72         this.search()
73       },
74
75       err => this.notificationsService.error('Error', err.text)
76     )
77   }
78
79   ngOnDestroy () {
80     if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
81   }
82
83   isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
84     return d instanceof VideoChannel
85   }
86
87   isVideo (v: VideoChannel | Video): v is Video {
88     return v instanceof Video
89   }
90
91   isUserLoggedIn () {
92     return this.authService.isLoggedIn()
93   }
94
95   search () {
96     forkJoin([
97       this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
98       this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
99     ])
100       .subscribe(
101         ([ videosResult, videoChannelsResult ]) => {
102           this.results = this.results
103                              .concat(videoChannelsResult.data)
104                              .concat(videosResult.videos)
105           this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
106
107           // Focus on channels if there are no enough videos
108           if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
109             this.resetPagination()
110             this.firstSearch = false
111
112             this.channelsPerPage = 10
113             this.search()
114           }
115
116           this.firstSearch = false
117         },
118
119         error => {
120           this.notificationsService.error(this.i18n('Error'), error.message)
121         }
122       )
123
124   }
125
126   onNearOfBottom () {
127     // Last page
128     if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
129
130     this.pagination.currentPage += 1
131     this.search()
132   }
133
134   onFiltered () {
135     this.resetPagination()
136
137     this.updateUrlFromAdvancedSearch()
138   }
139
140   private resetPagination () {
141     this.pagination.currentPage = 1
142     this.pagination.totalItems = null
143     this.channelsPerPage = 2
144
145     this.results = []
146   }
147
148   private updateTitle () {
149     this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
150   }
151
152   private updateUrlFromAdvancedSearch () {
153     this.router.navigate([], {
154       relativeTo: this.route,
155       queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
156     })
157   }
158 }