]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
Fetch outbox when searching an actor
[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 { 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 { Video } from '../../../../shared'
10 import { MetaService } from '@ngx-meta/core'
11 import { AdvancedSearch } from '@app/search/advanced-search.model'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13 import { immutableAssign } from '@app/shared/misc/utils'
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 videos: Video[] = []
22 videoChannels: VideoChannel[] = []
23
24 pagination: ComponentPagination = {
25 currentPage: 1,
26 itemsPerPage: 10, // Only for videos, use another variable for channels
27 totalItems: null
28 }
29 advancedSearch: AdvancedSearch = new AdvancedSearch()
30 isSearchFilterCollapsed = true
31 currentSearch: string
32
33 private subActivatedRoute: Subscription
34 private isInitialLoad = 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 ) { }
47
48 ngOnInit () {
49 this.subActivatedRoute = this.route.queryParams.subscribe(
50 queryParams => {
51 const querySearch = queryParams['search']
52
53 // New empty search
54 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
55
56 // Search updated, reset filters
57 if (this.currentSearch !== querySearch) {
58 this.resetPagination()
59 this.advancedSearch.reset()
60
61 this.currentSearch = querySearch
62 this.updateTitle()
63 }
64
65 this.advancedSearch = new AdvancedSearch(queryParams)
66
67 // Don't hide filters if we have some of them AND the user just came on the webpage
68 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
69 this.isInitialLoad = false
70
71 this.search()
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
76 }
77
78 ngOnDestroy () {
79 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
80 }
81
82 search () {
83 forkJoin([
84 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
85 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
86 ])
87 .subscribe(
88 ([ videosResult, videoChannelsResult ]) => {
89 this.videos = this.videos.concat(videosResult.videos)
90 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
91
92 this.videoChannels = this.videoChannels.concat(videoChannelsResult.data)
93
94 // Focus on channels
95 if (this.channelsPerPage !== 10 && this.videos.length < this.pagination.itemsPerPage) {
96 this.resetPagination()
97
98 this.channelsPerPage = 10
99 this.search()
100 }
101 },
102
103 error => {
104 this.notificationsService.error(this.i18n('Error'), error.message)
105 }
106 )
107
108 }
109
110 onNearOfBottom () {
111 // Last page
112 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
113
114 this.pagination.currentPage += 1
115 this.search()
116 }
117
118 onFiltered () {
119 this.resetPagination()
120
121 this.updateUrlFromAdvancedSearch()
122 }
123
124 private resetPagination () {
125 this.pagination.currentPage = 1
126 this.pagination.totalItems = null
127 this.channelsPerPage = 2
128
129 this.videos = []
130 this.videoChannels = []
131 }
132
133 private updateTitle () {
134 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
135 }
136
137 private updateUrlFromAdvancedSearch () {
138 this.router.navigate([], {
139 relativeTo: this.route,
140 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
141 })
142 }
143 }