Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 23.20: Create a clean parties list for web display
- Loading branch information
1 parent
2372469
commit 4c93a79
Showing
1 changed file
with
5 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,17 @@ | ||
import { Component, OnInit, OnDestroy } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Subject } from 'rxjs/Subject'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
import { MeteorObservable } from 'meteor-rxjs'; | ||
import { Component } from '@angular/core'; | ||
import { PaginationService } from 'ng2-pagination'; | ||
import { Counts } from 'meteor/tmeasday:publish-counts'; | ||
import { InjectUser } from "angular2-meteor-accounts-ui"; | ||
|
||
import 'rxjs/add/operator/combineLatest'; | ||
|
||
import { Parties } from '../../../../both/collections/parties.collection'; | ||
import { Party } from '../../../../both/models/party.model'; | ||
import { PartiesList } from "../shared-components/parties-list.class"; | ||
|
||
import template from './parties-list.component.html'; | ||
import style from './parties-list.component.scss'; | ||
|
||
interface Pagination { | ||
limit: number; | ||
skip: number; | ||
} | ||
|
||
interface Options extends Pagination { | ||
[key: string]: any | ||
} | ||
|
||
@Component({ | ||
selector: 'parties-list', | ||
template, | ||
styles: [ style ] | ||
}) | ||
@InjectUser('user') | ||
export class PartiesListComponent implements OnInit, OnDestroy { | ||
parties: Observable<Party[]>; | ||
partiesSub: Subscription; | ||
pageSize: Subject<number> = new Subject<number>(); | ||
curPage: Subject<number> = new Subject<number>(); | ||
nameOrder: Subject<number> = new Subject<number>(); | ||
optionsSub: Subscription; | ||
partiesSize: number = 0; | ||
autorunSub: Subscription; | ||
location: Subject<string> = new Subject<string>(); | ||
user: Meteor.User; | ||
imagesSubs: Subscription; | ||
|
||
constructor( | ||
private paginationService: PaginationService | ||
) {} | ||
|
||
ngOnInit() { | ||
this.imagesSubs = MeteorObservable.subscribe('images').subscribe(); | ||
|
||
this.optionsSub = Observable.combineLatest( | ||
this.pageSize, | ||
this.curPage, | ||
this.nameOrder, | ||
this.location | ||
).subscribe(([pageSize, curPage, nameOrder, location]) => { | ||
const options: Options = { | ||
limit: pageSize as number, | ||
skip: ((curPage as number) - 1) * (pageSize as number), | ||
sort: { name: nameOrder as number } | ||
}; | ||
|
||
this.paginationService.setCurrentPage(this.paginationService.defaultId, curPage as number); | ||
|
||
if (this.partiesSub) { | ||
this.partiesSub.unsubscribe(); | ||
} | ||
|
||
this.partiesSub = MeteorObservable.subscribe('parties', options, location).subscribe(() => { | ||
this.parties = Parties.find({}, { | ||
sort: { | ||
name: nameOrder | ||
} | ||
}).zone(); | ||
}); | ||
}); | ||
|
||
this.paginationService.register({ | ||
id: this.paginationService.defaultId, | ||
itemsPerPage: 10, | ||
currentPage: 1, | ||
totalItems: this.partiesSize | ||
}); | ||
|
||
this.pageSize.next(10); | ||
this.curPage.next(1); | ||
this.nameOrder.next(1); | ||
this.location.next(''); | ||
|
||
this.autorunSub = MeteorObservable.autorun().subscribe(() => { | ||
this.partiesSize = Counts.get('numberOfParties'); | ||
this.paginationService.setTotalItems(this.paginationService.defaultId, this.partiesSize); | ||
}); | ||
} | ||
|
||
removeParty(party: Party): void { | ||
Parties.remove(party._id); | ||
} | ||
|
||
search(value: string): void { | ||
this.curPage.next(1); | ||
this.location.next(value); | ||
} | ||
|
||
onPageChanged(page: number): void { | ||
this.curPage.next(page); | ||
} | ||
|
||
changeSortOrder(nameOrder: string): void { | ||
this.nameOrder.next(parseInt(nameOrder)); | ||
} | ||
|
||
isOwner(party: Party): boolean { | ||
return this.user && this.user._id === party.owner; | ||
} | ||
|
||
ngOnDestroy() { | ||
this.partiesSub.unsubscribe(); | ||
this.optionsSub.unsubscribe(); | ||
this.autorunSub.unsubscribe(); | ||
this.imagesSubs.unsubscribe(); | ||
export class PartiesListComponent extends PartiesList { | ||
constructor(paginationService: PaginationService) { | ||
super(paginationService); | ||
} | ||
} |