X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2F%2Bsearch%2Fsearch.component.ts;h=366fbd459906b293f085fabb2cbb533b3ae41543;hb=4158e67c8d7eb39712e598d3f409c6739eef5f0b;hp=250062e0cae1624155cb58447d95b05caed12576;hpb=af7fd04a6706fb781e4622167b08dc6c9376f06a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/+search/search.component.ts b/client/src/app/+search/search.component.ts index 250062e0c..366fbd459 100644 --- a/client/src/app/+search/search.component.ts +++ b/client/src/app/+search/search.component.ts @@ -1,4 +1,4 @@ -import { forkJoin, of, Subscription } from 'rxjs' +import { forkJoin, Subscription } from 'rxjs' import { LinkType } from 'src/types/link.type' import { Component, OnDestroy, OnInit } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' @@ -19,7 +19,7 @@ import { HTMLServerConfig, SearchTargetType } from '@shared/models' export class SearchComponent implements OnInit, OnDestroy { error: string - results: (Video | VideoChannel)[] = [] + results: (Video | VideoChannel | VideoPlaylist)[] = [] pagination = { currentPage: 1, @@ -47,10 +47,6 @@ export class SearchComponent implements OnInit, OnDestroy { private subActivatedRoute: Subscription private isInitialLoad = false // set to false to show the search filters on first arrival - private channelsPerPage = 2 - private playlistsPerPage = 2 - private videosPerPage = 10 - private hasMoreResults = true private isSearching = false @@ -73,36 +69,37 @@ export class SearchComponent implements OnInit, OnDestroy { ngOnInit () { this.serverConfig = this.serverService.getHTMLConfig() - this.subActivatedRoute = this.route.queryParams.subscribe( - async queryParams => { - const querySearch = queryParams['search'] - const searchTarget = queryParams['searchTarget'] + this.subActivatedRoute = this.route.queryParams + .subscribe({ + next: queryParams => { + const querySearch = queryParams['search'] + const searchTarget = queryParams['searchTarget'] - // Search updated, reset filters - if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) { - this.resetPagination() - this.advancedSearch.reset() + // Search updated, reset filters + if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) { + this.resetPagination() + this.advancedSearch.reset() - this.currentSearch = querySearch || undefined - this.updateTitle() - } + this.currentSearch = querySearch || undefined + this.updateTitle() + } - this.advancedSearch = new AdvancedSearch(queryParams) - if (!this.advancedSearch.searchTarget) { - this.advancedSearch.searchTarget = this.getDefaultSearchTarget() - } + this.advancedSearch = new AdvancedSearch(queryParams) + if (!this.advancedSearch.searchTarget) { + this.advancedSearch.searchTarget = this.getDefaultSearchTarget() + } - this.error = this.checkFieldsAndGetError() + this.error = this.checkFieldsAndGetError() - // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error - this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues()) - this.isInitialLoad = false + // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error + this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues()) + this.isInitialLoad = false - this.search() - }, + this.search() + }, - err => this.notifier.error(err.text) - ) + error: err => this.notifier.error(err.message) + }) this.userService.getAnonymousOrLoggedUser() .subscribe(user => this.userMiniature = user) @@ -140,33 +137,35 @@ export class SearchComponent implements OnInit, OnDestroy { this.getVideoChannelObs(), this.getVideoPlaylistObs(), this.getVideosObs() - ]).subscribe(results => { - for (const result of results) { - this.results = this.results.concat(result.data) - } + ]).subscribe({ + next: results => { + for (const result of results) { + this.results = this.results.concat(result.data) + } - this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0) - this.lastSearchTarget = this.advancedSearch.searchTarget + this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0) + this.lastSearchTarget = this.advancedSearch.searchTarget - this.hasMoreResults = this.results.length < this.pagination.totalItems - }, + this.hasMoreResults = this.results.length < this.pagination.totalItems + }, - err => { - if (this.advancedSearch.searchTarget !== 'search-index') { - this.notifier.error(err.message) - return - } + error: err => { + if (this.advancedSearch.searchTarget !== 'search-index') { + this.notifier.error(err.message) + return + } - this.notifier.error( - $localize`Search index is unavailable. Retrying with instance results instead.`, - $localize`Search error` - ) - this.advancedSearch.searchTarget = 'local' - this.search() - }, + this.notifier.error( + $localize`Search index is unavailable. Retrying with instance results instead.`, + $localize`Search error` + ) + this.advancedSearch.searchTarget = 'local' + this.search() + }, - () => { - this.isSearching = false + complete: () => { + this.isSearching = false + } }) } @@ -244,17 +243,16 @@ export class SearchComponent implements OnInit, OnDestroy { private resetPagination () { this.pagination.currentPage = 1 this.pagination.totalItems = null - this.channelsPerPage = 2 this.results = [] } private updateTitle () { - const suffix = this.currentSearch - ? ' ' + this.currentSearch - : '' + const title = this.currentSearch + ? $localize`Search ${this.currentSearch}` + : $localize`Search` - this.metaService.setTitle($localize`Search` + suffix) + this.metaService.setTitle(title) } private updateUrlFromAdvancedSearch () { @@ -269,7 +267,7 @@ export class SearchComponent implements OnInit, OnDestroy { private getVideosObs () { const params = { search: this.currentSearch, - componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.videosPerPage }), + componentPagination: immutableAssign(this.pagination, { itemsPerPage: 10 }), advancedSearch: this.advancedSearch } @@ -283,11 +281,9 @@ export class SearchComponent implements OnInit, OnDestroy { } private getVideoChannelObs () { - if (!this.currentSearch) return of({ data: [], total: 0 }) - const params = { search: this.currentSearch, - componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }), + componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.buildChannelsPerPage() }), advancedSearch: this.advancedSearch } @@ -301,11 +297,9 @@ export class SearchComponent implements OnInit, OnDestroy { } private getVideoPlaylistObs () { - if (!this.currentSearch) return of({ data: [], total: 0 }) - const params = { search: this.currentSearch, - componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.playlistsPerPage }), + componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.buildPlaylistsPerPage() }), advancedSearch: this.advancedSearch } @@ -335,4 +329,16 @@ export class SearchComponent implements OnInit, OnDestroy { return undefined } + + private buildChannelsPerPage () { + if (this.advancedSearch.resultType === 'channels') return 10 + + return 2 + } + + private buildPlaylistsPerPage () { + if (this.advancedSearch.resultType === 'playlists') return 10 + + return 2 + } }