Commits
Tim Donohue authored and GitHub committed c515cb20ef2 Merge
1 1 | import { Component, Input, OnDestroy, OnInit } from '@angular/core'; |
2 2 | import { ActivatedRoute } from '@angular/router'; |
3 3 | |
4 - | import { BehaviorSubject, combineLatest as observableCombineLatest } from 'rxjs'; |
4 + | import { BehaviorSubject, combineLatest as observableCombineLatest, Subscription } from 'rxjs'; |
5 5 | |
6 6 | import { RemoteData } from '../../core/data/remote-data'; |
7 7 | import { Collection } from '../../core/shared/collection.model'; |
8 8 | import { Community } from '../../core/shared/community.model'; |
9 9 | import { fadeIn } from '../../shared/animations/fade'; |
10 10 | import { PaginatedList } from '../../core/data/paginated-list.model'; |
11 11 | import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model'; |
12 12 | import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model'; |
13 13 | import { CollectionDataService } from '../../core/data/collection-data.service'; |
14 14 | import { PaginationService } from '../../core/pagination/pagination.service'; |
43 43 | /** |
44 44 | * The sorting configuration |
45 45 | */ |
46 46 | sortConfig: SortOptions; |
47 47 | |
48 48 | /** |
49 49 | * A list of remote data objects of communities' collections |
50 50 | */ |
51 51 | subCollectionsRDObs: BehaviorSubject<RemoteData<PaginatedList<Collection>>> = new BehaviorSubject<RemoteData<PaginatedList<Collection>>>({} as any); |
52 52 | |
53 + | subscriptions: Subscription[] = []; |
54 + | |
53 55 | constructor( |
54 56 | protected cds: CollectionDataService, |
55 57 | protected paginationService: PaginationService, |
56 58 | protected route: ActivatedRoute, |
57 59 | ) { |
58 60 | } |
59 61 | |
60 62 | ngOnInit(): void { |
61 63 | this.config = new PaginationComponentOptions(); |
62 64 | this.config.id = this.pageId; |
70 72 | this.initPage(); |
71 73 | } |
72 74 | |
73 75 | /** |
74 76 | * Initialise the list of collections |
75 77 | */ |
76 78 | initPage() { |
77 79 | const pagination$ = this.paginationService.getCurrentPagination(this.config.id, this.config); |
78 80 | const sort$ = this.paginationService.getCurrentSort(this.config.id, this.sortConfig); |
79 81 | |
80 - | observableCombineLatest([pagination$, sort$]).pipe( |
82 + | this.subscriptions.push(observableCombineLatest([pagination$, sort$]).pipe( |
81 83 | switchMap(([currentPagination, currentSort]) => { |
82 84 | return this.cds.findByParent(this.community.id, { |
83 85 | currentPage: currentPagination.currentPage, |
84 86 | elementsPerPage: currentPagination.pageSize, |
85 87 | sort: {field: currentSort.field, direction: currentSort.direction} |
86 88 | }); |
87 89 | }) |
88 90 | ).subscribe((results) => { |
89 91 | this.subCollectionsRDObs.next(results); |
90 - | }); |
92 + | })); |
91 93 | } |
92 94 | |
93 95 | ngOnDestroy(): void { |
94 - | this.paginationService.clearPagination(this.config.id); |
96 + | this.paginationService.clearPagination(this.config?.id); |
97 + | this.subscriptions.map((subscription: Subscription) => subscription.unsubscribe()); |
95 98 | } |
96 99 | |
97 100 | } |