X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fsearch%2Fsearch.component.ts;h=475fdd277628469bf1a821097612367edd3da591;hb=9a0fc8409c7a783348ec212fa9f38d0a98413467;hp=8d615fd058dd990674217300592a632870ab6629;hpb=4278710d5b48546709720fac46657b84bba52a18;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/search/search.component.ts b/client/src/app/search/search.component.ts index 8d615fd05..475fdd277 100644 --- a/client/src/app/search/search.component.ts +++ b/client/src/app/search/search.component.ts @@ -1,14 +1,16 @@ import { Component, OnDestroy, OnInit } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' -import { RedirectService } from '@app/core' +import { AuthService, RedirectService } from '@app/core' import { NotificationsService } from 'angular2-notifications' -import { Subscription } from 'rxjs' +import { forkJoin, Subscription } from 'rxjs' import { SearchService } from '@app/search/search.service' import { ComponentPagination } from '@app/shared/rest/component-pagination.model' import { I18n } from '@ngx-translate/i18n-polyfill' -import { Video } from '../../../../shared' import { MetaService } from '@ngx-meta/core' import { AdvancedSearch } from '@app/search/advanced-search.model' +import { VideoChannel } from '@app/shared/video-channel/video-channel.model' +import { immutableAssign } from '@app/shared/misc/utils' +import { Video } from '@app/shared/video/video.model' @Component({ selector: 'my-search', @@ -16,18 +18,22 @@ import { AdvancedSearch } from '@app/search/advanced-search.model' templateUrl: './search.component.html' }) export class SearchComponent implements OnInit, OnDestroy { - videos: Video[] = [] + results: (Video | VideoChannel)[] = [] + pagination: ComponentPagination = { currentPage: 1, - itemsPerPage: 10, // It's per object type (so 10 videos, 10 video channels etc) + itemsPerPage: 10, // Only for videos, use another variable for channels totalItems: null } advancedSearch: AdvancedSearch = new AdvancedSearch() isSearchFilterCollapsed = true + currentSearch: string private subActivatedRoute: Subscription - private currentSearch: string private isInitialLoad = true + private firstSearch = true + + private channelsPerPage = 2 constructor ( private i18n: I18n, @@ -36,7 +42,8 @@ export class SearchComponent implements OnInit, OnDestroy { private metaService: MetaService, private redirectService: RedirectService, private notificationsService: NotificationsService, - private searchService: SearchService + private searchService: SearchService, + private authService: AuthService ) { } ngOnInit () { @@ -73,18 +80,47 @@ export class SearchComponent implements OnInit, OnDestroy { if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() } + isVideoChannel (d: VideoChannel | Video): d is VideoChannel { + return d instanceof VideoChannel + } + + isVideo (v: VideoChannel | Video): v is Video { + return v instanceof Video + } + + isUserLoggedIn () { + return this.authService.isLoggedIn() + } + search () { - return this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch) + forkJoin([ + this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch), + this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })) + ]) .subscribe( - ({ videos, totalVideos }) => { - this.videos = this.videos.concat(videos) - this.pagination.totalItems = totalVideos + ([ videosResult, videoChannelsResult ]) => { + this.results = this.results + .concat(videoChannelsResult.data) + .concat(videosResult.videos) + this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total + + // Focus on channels if there are no enough videos + if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) { + this.resetPagination() + this.firstSearch = false + + this.channelsPerPage = 10 + this.search() + } + + this.firstSearch = false }, error => { this.notificationsService.error(this.i18n('Error'), error.message) } ) + } onNearOfBottom () { @@ -104,8 +140,9 @@ export class SearchComponent implements OnInit, OnDestroy { private resetPagination () { this.pagination.currentPage = 1 this.pagination.totalItems = null + this.channelsPerPage = 2 - this.videos = [] + this.results = [] } private updateTitle () {