Client: save page params as well

This commit is contained in:
Chocobozzz 2016-07-18 15:39:10 +02:00
parent 0629423ce3
commit bddab65ae5
9 changed files with 85 additions and 49 deletions

View File

@ -6,7 +6,7 @@
</div> </div>
<div class="col-md-9"> <div class="col-md-9">
<my-search (search)="onSearch($event)"></my-search> <my-search></my-search>
</div> </div>
</header> </header>

View File

@ -1,12 +1,11 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http'; import { HTTP_PROVIDERS } from '@angular/http';
import { Router, ROUTER_DIRECTIVES } from '@angular/router'; import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router';
import { FriendService } from './friends'; import { FriendService } from './friends';
import { import {
AuthService, AuthService,
AuthStatus, AuthStatus,
Search,
SearchComponent, SearchComponent,
SearchService SearchService
} from './shared'; } from './shared';
@ -27,6 +26,7 @@ export class AppComponent {
constructor( constructor(
private authService: AuthService, private authService: AuthService,
private friendService: FriendService, private friendService: FriendService,
private route: ActivatedRoute,
private router: Router private router: Router
) { ) {
this.isLoggedIn = this.authService.isLoggedIn(); this.isLoggedIn = this.authService.isLoggedIn();
@ -40,19 +40,6 @@ export class AppComponent {
); );
} }
onSearch(search: Search) {
if (search.value !== '') {
const params = {
field: search.field,
search: search.value
};
this.router.navigate(['/videos/list', params]);
} else {
this.router.navigate(['/videos/list']);
}
}
// FIXME // FIXME
logout() { logout() {
// this._authService.logout(); // this._authService.logout();

View File

@ -1,4 +1,4 @@
import { Component, EventEmitter, Output, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown'; import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown';
@ -13,8 +13,6 @@ import { SearchService } from './search.service';
}) })
export class SearchComponent implements OnInit { export class SearchComponent implements OnInit {
@Output() search = new EventEmitter<Search>();
fieldChoices = { fieldChoices = {
name: 'Name', name: 'Name',
author: 'Author', author: 'Author',
@ -30,7 +28,9 @@ export class SearchComponent implements OnInit {
constructor(private searchService: SearchService) {} constructor(private searchService: SearchService) {}
ngOnInit() { ngOnInit() {
this.searchService.searchChanged.subscribe( // Subscribe is the search changed
// Usually changed by videos list component
this.searchService.updateSearch.subscribe(
newSearchCriterias => { newSearchCriterias => {
// Put a field by default // Put a field by default
if (!newSearchCriterias.field) { if (!newSearchCriterias.field) {
@ -58,7 +58,7 @@ export class SearchComponent implements OnInit {
} }
doSearch() { doSearch() {
this.search.emit(this.searchCriterias); this.searchService.searchUpdated.next(this.searchCriterias);
} }
getStringChoice(choiceKey: SearchField) { getStringChoice(choiceKey: SearchField) {

View File

@ -7,9 +7,11 @@ import { Search } from './search.model';
// Remove it when we'll be able to subscribe to router changes // Remove it when we'll be able to subscribe to router changes
@Injectable() @Injectable()
export class SearchService { export class SearchService {
searchChanged: Subject<Search>; searchUpdated: Subject<Search>;
updateSearch: Subject<Search>;
constructor() { constructor() {
this.searchChanged = new Subject<Search>(); this.updateSearch = new Subject<Search>();
this.searchUpdated = new Subject<Search>();
} }
} }

View File

@ -8,13 +8,16 @@
</div> </div>
<div class="videos-miniatures"> <div class="videos-miniatures">
<div class="col-md-12 no-video" *ngIf="noVideo()">There is no video.</div> <div class="col-md-12 no-video" *ngIf="isThereNoVideo()">There is no video.</div>
<my-video-miniature class="ng-animate "*ngFor="let video of videos" [video]="video" [user]="user" (removed)="onRemoved(video)"> <my-video-miniature
class="ng-animate"
*ngFor="let video of videos" [video]="video" [user]="user" [currentSort]="sort" (removed)="onRemoved(video)"
>
</my-video-miniature> </my-video-miniature>
</div> </div>
<pagination *ngIf="pagination.totalItems !== null" <pagination *ngIf="pagination.totalItems !== null"
[totalItems]="pagination.totalItems" [itemsPerPage]="pagination.itemsPerPage" [maxSize]="6" [boundaryLinks]="true" [rotate]="false" [totalItems]="pagination.totalItems" [itemsPerPage]="pagination.itemsPerPage" [maxSize]="6" [boundaryLinks]="true" [rotate]="false"
[(ngModel)]="pagination.currentPage" (pageChanged)="getVideos()" [(ngModel)]="pagination.currentPage" (pageChanged)="onPageChanged($event)"
></pagination> ></pagination>

View File

@ -37,7 +37,8 @@ export class VideoListComponent implements OnInit, OnDestroy {
videos: Video[] = []; videos: Video[] = [];
private search: Search; private search: Search;
private sub: any; private subActivatedRoute: any;
private subSearch: any;
constructor( constructor(
private authService: AuthService, private authService: AuthService,
@ -49,33 +50,35 @@ export class VideoListComponent implements OnInit, OnDestroy {
) {} ) {}
ngOnInit() { ngOnInit() {
this.sub = this.route.params.subscribe(routeParams => { if (this.authService.isLoggedIn()) {
if (this.authService.isLoggedIn()) { this.user = User.load();
this.user = User.load(); }
}
this.search = { // Subscribe to route changes
value: routeParams['search'], this.subActivatedRoute = this.route.params.subscribe(routeParams => {
field: <SearchField>routeParams['field'] this.loadRouteParams(routeParams);
};
// Update the search service component // Update the search service component
this.searchService.searchChanged.next(this.search); this.searchService.updateSearch.next(this.search);
this.sort = <SortField>routeParams['sort'] || '-createdDate';
this.getVideos(); this.getVideos();
}); });
// Subscribe to search changes
this.subSearch = this.searchService.searchUpdated.subscribe(search => {
this.search = search;
this.navigateToNewParams();
});
} }
ngOnDestroy() { ngOnDestroy() {
this.sub.unsubscribe(); this.subActivatedRoute.unsubscribe();
this.subSearch.unsubscribe();
} }
getVideos(detectChanges = true) { getVideos(detectChanges = true) {
this.loading.next(true); this.loading.next(true);
this.videos = []; this.videos = [];
this.pagination.currentPage = 1;
this.changeDetector.detectChanges(); this.changeDetector.detectChanges();
@ -98,26 +101,65 @@ export class VideoListComponent implements OnInit, OnDestroy {
); );
} }
noVideo() { isThereNoVideo() {
return !this.loading && this.videos.length === 0; 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) { onRemoved(video: Video) {
this.getVideos(false); this.getVideos();
} }
onSort(sort: SortField) { onSort(sort: SortField) {
this.sort = sort; this.sort = sort;
this.navigateToNewParams();
}
private buildRouteParams() {
// There is always a sort and a current page
const params: any = { const params: any = {
sort: this.sort sort: this.sort,
page: this.pagination.currentPage
}; };
// Maybe there is a search
if (this.search.value) { if (this.search.value) {
params.field = this.search.field; params.field = this.search.field;
params.search = this.search.value; 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: <SearchField>routeParams['field']
};
} else {
this.search = {
value: '',
field: 'name'
};
}
this.sort = <SortField>routeParams['sort'] || '-createdDate';
this.pagination.currentPage = parseInt(routeParams['page']) || 1;
this.changeDetector.detectChanges();
}
private navigateToNewParams() {
const routeParams = this.buildRouteParams();
this.router.navigate(['/videos/list', routeParams]);
} }
} }

View File

@ -17,12 +17,12 @@
<div class="video-miniature-tags"> <div class="video-miniature-tags">
<span *ngFor="let tag of video.tags" class="video-miniature-tag"> <span *ngFor="let tag of video.tags" class="video-miniature-tag">
<a [routerLink]="['/videos/list', { field: 'tags', search: tag }]" class="label label-primary">{{ tag }}</a> <a [routerLink]="['/videos/list', { field: 'tags', search: tag, sort: currentSort }]" class="label label-primary">{{ tag }}</a>
</span> </span>
</div> </div>
</span> </span>
<a [routerLink]="['/videos/list', { field: 'author', search: video.author }]" class="video-miniature-author">{{ video.by }}</a> <a [routerLink]="['/videos/list', { field: 'author', search: video.author, sort: currentSort }]" class="video-miniature-author">{{ video.by }}</a>
<span class="video-miniature-created-date">{{ video.createdDate | date:'short' }}</span> <span class="video-miniature-created-date">{{ video.createdDate | date:'short' }}</span>
</div> </div>
</div> </div>

View File

@ -2,7 +2,7 @@ import { DatePipe } from '@angular/common';
import { Component, Input, Output, EventEmitter } from '@angular/core'; import { Component, Input, Output, EventEmitter } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router'; import { ROUTER_DIRECTIVES } from '@angular/router';
import { Video, VideoService } from '../shared'; import { SortField, Video, VideoService } from '../shared';
import { User } from '../../shared'; import { User } from '../../shared';
@Component({ @Component({
@ -16,6 +16,7 @@ import { User } from '../../shared';
export class VideoMiniatureComponent { export class VideoMiniatureComponent {
@Output() removed = new EventEmitter<any>(); @Output() removed = new EventEmitter<any>();
@Input() currentSort: SortField;
@Input() user: User; @Input() user: User;
@Input() video: Video; @Input() video: Video;

View File

@ -45,6 +45,7 @@
"src/app/shared/users/index.ts", "src/app/shared/users/index.ts",
"src/app/shared/users/token.model.ts", "src/app/shared/users/token.model.ts",
"src/app/shared/users/user.model.ts", "src/app/shared/users/user.model.ts",
"src/app/shared/videos-params.ts",
"src/app/videos/index.ts", "src/app/videos/index.ts",
"src/app/videos/shared/index.ts", "src/app/videos/shared/index.ts",
"src/app/videos/shared/loader/index.ts", "src/app/videos/shared/loader/index.ts",