]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix search results
authorChocobozzz <me@florianbigard.com>
Tue, 28 Aug 2018 14:02:02 +0000 (16:02 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 28 Aug 2018 14:02:02 +0000 (16:02 +0200)
client/src/app/search/search.component.html
client/src/app/search/search.component.ts

index 0d09ebbe64fffb27c3c0cfc76e96bda0d75ac330..d2ed1f881412ae7fe4e022784b39bd50249e0ae3 100644 (file)
     </div>
   </div>
 
-  <div i18n *ngIf="pagination.totalItems === 0 && videoChannels.length === 0" class="no-result">
+  <div i18n *ngIf="pagination.totalItems === 0 && results.length === 0" class="no-result">
     No results found
   </div>
 
-  <div *ngFor="let videoChannel of videoChannels" class="entry video-channel">
-    <a [routerLink]="[ '/video-channels', videoChannel.nameWithHost ]">
-      <img [src]="videoChannel.avatarUrl" alt="Avatar" />
-    </a>
-
-    <div class="video-channel-info">
-      <a [routerLink]="[ '/video-channels', videoChannel.nameWithHost ]" class="video-channel-names">
-        <div class="video-channel-display-name">{{ videoChannel.displayName }}</div>
-        <div class="video-channel-name">{{ videoChannel.nameWithHost }}</div>
+  <ng-container *ngFor="let result of results">
+    <div *ngIf="isVideoChannel(result)" class="entry video-channel">
+      <a [routerLink]="[ '/video-channels', result.nameWithHost ]">
+        <img [src]="result.avatarUrl" alt="Avatar" />
       </a>
 
-      <div i18n class="video-channel-followers">{{ videoChannel.followersCount }} subscribers</div>
-    </div>
+      <div class="video-channel-info">
+        <a [routerLink]="[ '/video-channels', result.nameWithHost ]" class="video-channel-names">
+          <div class="video-channel-display-name">{{ result.displayName }}</div>
+          <div class="video-channel-name">{{ result.nameWithHost }}</div>
+        </a>
 
-    <my-subscribe-button [videoChannel]="videoChannel"></my-subscribe-button>
-  </div>
+        <div i18n class="video-channel-followers">{{ result.followersCount }} subscribers</div>
+      </div>
 
-  <div *ngFor="let video of videos" class="entry video">
-    <my-video-thumbnail [video]="video"></my-video-thumbnail>
+      <my-subscribe-button *ngIf="isUserLoggedIn()" [videoChannel]="result"></my-subscribe-button>
+    </div>
 
-    <div class="video-info">
-      <a class="video-info-name" [routerLink]="['/videos/watch', video.uuid]" [attr.title]="video.name">{{ video.name }}</a>
-      <span i18n class="video-info-date-views">{{ video.publishedAt | myFromNow }} - {{ video.views | myNumberFormatter }} views</span>
-      <a class="video-info-account" [routerLink]="[ '/accounts', video.by ]">{{ video.by }}</a>
+    <div *ngIf="isVideo(result)" class="entry video">
+      <my-video-thumbnail [video]="result"></my-video-thumbnail>
+
+      <div class="video-info">
+        <a class="video-info-name" [routerLink]="['/videos/watch', result.uuid]" [attr.title]="result.name">{{ result.name }}</a>
+        <span i18n class="video-info-date-views">{{ result.publishedAt | myFromNow }} - {{ result.views | myNumberFormatter }} views</span>
+        <a class="video-info-account" [routerLink]="[ '/accounts', result.by ]">{{ result.by }}</a>
+      </div>
     </div>
-  </div>
+  </ng-container>
+
 </div>
index ed84e24d9fbb8cf3d512d636552066b0304b7eee..4c540ca72b2b467d57affba42ad2a36ece9e592a 100644 (file)
@@ -1,16 +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 { 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',
@@ -18,8 +18,7 @@ import { immutableAssign } from '@app/shared/misc/utils'
   templateUrl: './search.component.html'
 })
 export class SearchComponent implements OnInit, OnDestroy {
-  videos: Video[] = []
-  videoChannels: VideoChannel[] = []
+  results: (Video | VideoChannel)[] = []
 
   pagination: ComponentPagination = {
     currentPage: 1,
@@ -42,7 +41,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 () {
@@ -79,6 +79,18 @@ 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 () {
     forkJoin([
       this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
@@ -86,13 +98,13 @@ export class SearchComponent implements OnInit, OnDestroy {
     ])
       .subscribe(
         ([ videosResult, videoChannelsResult ]) => {
-          this.videos = this.videos.concat(videosResult.videos)
+          this.results = this.results
+                             .concat(videoChannelsResult.data)
+                             .concat(videosResult.videos)
           this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
 
-          this.videoChannels = this.videoChannels.concat(videoChannelsResult.data)
-
           // Focus on channels
-          if (this.channelsPerPage !== 10 && this.videos.length < this.pagination.itemsPerPage) {
+          if (this.channelsPerPage !== 10 && videosResult.videos.length < this.pagination.itemsPerPage) {
             this.resetPagination()
 
             this.channelsPerPage = 10
@@ -126,8 +138,7 @@ export class SearchComponent implements OnInit, OnDestroy {
     this.pagination.totalItems = null
     this.channelsPerPage = 2
 
-    this.videos = []
-    this.videoChannels = []
+    this.results = []
   }
 
   private updateTitle () {