X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fvideos%2Fvideo-list%2Fvideo-list.component.ts;h=7c6d4b992d387463136502c05b85a81e51fe73c9;hb=def16d33d19153c6583fa8a30634760b3d64d34c;hp=0ebf0ef5c459016e9a2bc9edafe7f25366eb311f;hpb=0629423ce335137ce77d1ee8fe30fc0eee36d83b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index 0ebf0ef5c..7c6d4b992 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts @@ -12,7 +12,7 @@ import { Video, VideoService } from '../shared'; -import { AuthService, Search, SearchField, User } from '../../shared'; +import { AuthService, AuthUser, Search, SearchField } from '../../shared'; import { VideoMiniatureComponent } from './video-miniature.component'; import { VideoSortComponent } from './video-sort.component'; import { SearchService } from '../../shared'; @@ -33,11 +33,12 @@ export class VideoListComponent implements OnInit, OnDestroy { totalItems: null }; sort: SortField; - user: User = null; + user: AuthUser = null; videos: Video[] = []; private search: Search; - private sub: any; + private subActivatedRoute: any; + private subSearch: any; constructor( private authService: AuthService, @@ -49,33 +50,37 @@ export class VideoListComponent implements OnInit, OnDestroy { ) {} ngOnInit() { - this.sub = this.route.params.subscribe(routeParams => { - if (this.authService.isLoggedIn()) { - this.user = User.load(); - } + if (this.authService.isLoggedIn()) { + this.user = AuthUser.load(); + } - this.search = { - value: routeParams['search'], - field: routeParams['field'] - }; + // Subscribe to route changes + this.subActivatedRoute = this.route.params.subscribe(routeParams => { + this.loadRouteParams(routeParams); // Update the search service component - this.searchService.searchChanged.next(this.search); + this.searchService.updateSearch.next(this.search); + this.getVideos(); + }); - this.sort = routeParams['sort'] || '-createdDate'; + // Subscribe to search changes + this.subSearch = this.searchService.searchUpdated.subscribe(search => { + this.search = search; + // Reset pagination + this.pagination.currentPage = 1; - this.getVideos(); + this.navigateToNewParams(); }); } ngOnDestroy() { - this.sub.unsubscribe(); + this.subActivatedRoute.unsubscribe(); + this.subSearch.unsubscribe(); } - getVideos(detectChanges = true) { + getVideos() { this.loading.next(true); this.videos = []; - this.pagination.currentPage = 1; this.changeDetector.detectChanges(); @@ -98,26 +103,69 @@ export class VideoListComponent implements OnInit, OnDestroy { ); } - noVideo() { - return !this.loading && this.videos.length === 0; + isThereNoVideo() { + return !this.loading.getValue() && this.videos.length === 0; + } + + onPageChanged(event: any) { + // Be sure the current page is set + this.pagination.currentPage = event.page; + + this.navigateToNewParams(); } onRemoved(video: Video) { - this.getVideos(false); + this.getVideos(); } onSort(sort: SortField) { this.sort = sort; + this.navigateToNewParams(); + } + + private buildRouteParams() { + // There is always a sort and a current page const params: any = { - sort: this.sort + sort: this.sort, + page: this.pagination.currentPage }; + // Maybe there is a search if (this.search.value) { params.field = this.search.field; params.search = this.search.value; } - this.router.navigate(['/videos/list', params]); + return params; + } + + private loadRouteParams(routeParams) { + if (routeParams['search'] !== undefined) { + this.search = { + value: routeParams['search'], + field: routeParams['field'] + }; + } else { + this.search = { + value: '', + field: 'name' + }; + } + + this.sort = routeParams['sort'] || '-createdDate'; + + if (routeParams['page'] !== undefined) { + this.pagination.currentPage = parseInt(routeParams['page']); + } else { + this.pagination.currentPage = 1; + } + + this.changeDetector.detectChanges(); + } + + private navigateToNewParams() { + const routeParams = this.buildRouteParams(); + this.router.navigate(['/videos/list', routeParams]); } }