/
parties-list.component.ts
89 lines (74 loc) · 2.24 KB
/
parties-list.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 { PaginationService } from 'ng2-pagination';
import 'rxjs/add/operator/combineLatest';
import { Parties } from '../../../../both/collections/parties.collection';
import { Party } from '../../../../both/models/party.model';
import template from './parties-list.component.html';
interface Pagination {
limit: number;
skip: number;
}
interface Options extends Pagination {
[key: string]: any
}
@Component({
selector: 'parties-list',
template
})
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;
constructor(
private paginationService: PaginationService
) {}
ngOnInit() {
this.optionsSub = Observable.combineLatest(
this.pageSize,
this.curPage,
this.nameOrder
).subscribe(([pageSize, curPage, nameOrder]) => {
const options: Options = {
limit: pageSize as number,
skip: ((curPage as number) - 1) * (pageSize as number),
sort: { name: nameOrder as number }
};
if (this.partiesSub) {
this.partiesSub.unsubscribe();
}
this.partiesSub = MeteorObservable.subscribe('parties', options).subscribe(() => {
this.parties = Parties.find({}, {
sort: {
name: nameOrder
}
}).zone();
});
});
this.paginationService.register({
id: this.paginationService.defaultId,
itemsPerPage: 10,
currentPage: 1,
totalItems: 30,
});
this.pageSize.next(10);
this.curPage.next(1);
this.nameOrder.next(1);
}
removeParty(party: Party): void {
Parties.remove(party._id);
}
search(value: string): void {
this.parties = Parties.find(value ? { location: value } : {}).zone();
}
ngOnDestroy() {
this.partiesSub.unsubscribe();
this.optionsSub.unsubscribe();
}
}