Skip to content

Commit 893e263

Browse files
authored
Ush 1559 - Saved Filters (#1370)
* Fix for cached filter persisting.... * Fixed a few filter storage issues and killed old requests * changed the reset order slightly
1 parent 8990eec commit 893e263

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

apps/web-mzima-client/src/app/feed/feed.component.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { searchFormHelper } from '@helpers';
99
import { TranslateService } from '@ngx-translate/core';
1010
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
1111
import { NgxMasonryComponent, NgxMasonryOptions } from 'ngx-masonry';
12-
import { filter, forkJoin, Subject, Subscription } from 'rxjs';
12+
import { filter, forkJoin, Subscription } from 'rxjs';
1313
import { PostDetailsModalComponent } from '../map';
1414
import { MainViewComponent } from '@shared';
1515
import { SessionService, BreakpointService, EventBusService, EventType } from '@services';
@@ -18,7 +18,6 @@ import { LanguageService } from '../core/services/language.service';
1818
import {
1919
SavedsearchesService,
2020
PostsService,
21-
GeoJsonFilter,
2221
PostResult,
2322
PostStatus,
2423
postHelpers,
@@ -43,10 +42,10 @@ export class FeedComponent extends MainViewComponent implements OnInit, OnDestro
4342
private _routerEvent = Subscription.EMPTY;
4443
@ViewChild('feed') public feed: ElementRef;
4544
@ViewChild('masonry') public masonry: NgxMasonryComponent;
46-
private readonly getPostsSubject = new Subject<{
47-
params: GeoJsonFilter;
48-
add?: boolean;
49-
}>();
45+
// private readonly getPostsSubject = new Subject<{
46+
// params: GeoJsonFilter;
47+
// add?: boolean;
48+
// }>();
5049
public pagination = {
5150
page: 0,
5251
limit: 20,
@@ -104,6 +103,7 @@ export class FeedComponent extends MainViewComponent implements OnInit, OnDestro
104103
public initialLoad = true;
105104
public urlFromRouteTrigger: string;
106105
public urlAfterInteractionWithFilters: string;
106+
private postRequests: Subscription[] = [];
107107

108108
constructor(
109109
protected override router: Router,
@@ -428,17 +428,26 @@ export class FeedComponent extends MainViewComponent implements OnInit, OnDestro
428428
loadData(): void {}
429429

430430
private getPosts({ params, loadMore }: { params: any; loadMore?: boolean }): void {
431-
/* --------------------------------------------
432-
Work with Posts Service to get posts from API
433-
----------------------------------------------*/
434-
this.postsService.getPosts('', { ...params, ...this.activeSorting }).subscribe({
435-
next: (data) => {
436-
this.posts = loadMore ? [...this.posts, ...data.results] : data.results;
437-
},
438-
// complete: () => {
439-
// // console.log('complete?');
440-
// },
431+
// Call the posts service, keeping the subscription for later
432+
const postRequestSubscription = this.postsService
433+
.getPosts('', { ...params, ...this.activeSorting })
434+
.subscribe({
435+
next: (data) => {
436+
this.posts = loadMore ? [...this.posts, ...data.results] : data.results;
437+
},
438+
});
439+
440+
// Unsubscribe and destroy existing subscriptions....
441+
this.postRequests.forEach((subscription) => {
442+
subscription.unsubscribe();
441443
});
444+
445+
// Reset everything so the user sees some loading indicators
446+
this.posts = [];
447+
this.isLoading = true;
448+
449+
// Keep the subscription so we can end it later if its replaced with a new api call
450+
this.postRequests.push(postRequestSubscription);
442451
}
443452

444453
public updateMasonry(): void {

apps/web-mzima-client/src/app/map/map.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class MapComponent extends MainViewComponent implements OnInit {
4848
fitBoundsOptions: FitBoundsOptions = {
4949
animate: true,
5050
};
51-
cachedFilter: string;
51+
// cachedFilter: string;
5252
filtersSubscription$: Observable<any>;
5353
public leafletOptions: MapOptions;
5454
public progress = 0;
@@ -310,6 +310,12 @@ export class MapComponent extends MainViewComponent implements OnInit {
310310
const isThisInProgress =
311311
pageNumber > 1 && posts.meta.total !== this.mapLayers[0].getLayers().length;
312312

313+
const doMarkersAndResultsMismatch =
314+
this.mapLayers.length === 0 ||
315+
posts.meta.total !== this.mapLayers[0].getLayers().length;
316+
317+
const isFirstAndOnlyPage = pageNumber === 1 && pageNumber === posts.meta.last_page;
318+
313319
// Has the filter changed from when we last saw it?
314320
let hasTheFilterChanged = false;
315321
if (filter !== undefined) {
@@ -325,7 +331,8 @@ export class MapComponent extends MainViewComponent implements OnInit {
325331
if (
326332
isFirstLayerEmpty ||
327333
hasTheFilterChanged ||
328-
isThisInProgress // ||
334+
isThisInProgress ||
335+
(isFirstAndOnlyPage && doMarkersAndResultsMismatch)
329336
// isLayerCountMismatch
330337
) {
331338
if (!isFirstLayerEmpty && !isThisInProgress) {

apps/web-mzima-client/src/app/shared/components/main-view.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export abstract class MainViewComponent {
2121
limit: 500,
2222
offset: 0,
2323
};
24+
cachedFilter: string;
2425
public user: UserInterface;
2526
public isDesktop: boolean = false;
2627

apps/web-mzima-client/src/app/shared/components/search-form/search-form.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ export class SearchFormComponent extends BaseComponent implements OnInit {
632632
this.activeSavedSearch = activeSavedSearch.result;
633633
this.checkSavedSearchNotifications();
634634
}
635+
localStorage.setItem(
636+
this.session.getLocalStorageNameMapper('filters'),
637+
JSON.stringify(this.activeSavedSearch!.filter),
638+
);
635639
}
636640

637641
async applySavedFilter(value: number | null) {
@@ -689,13 +693,17 @@ export class SearchFormComponent extends BaseComponent implements OnInit {
689693
this.activeSavedSearch.filter.source = [this.activeSavedSearch.filter.source];
690694
}
691695

696+
this.activeSavedSearch.filter.currentView = this.activeSavedSearch.view;
697+
692698
this.resetForm(this.activeSavedSearch.filter);
693699
} else {
694700
this.resetForm();
695701
}
696702
}
697703

698704
public resetSavedFilter(): void {
705+
localStorage.removeItem(this.session.getLocalStorageNameMapper('filters'));
706+
localStorage.removeItem(this.session.getLocalStorageNameMapper('activeSavedSearch'));
699707
this.clearSavedFilter();
700708
this.resetForm();
701709
this.defaultFormValue = this.formBuilder.group(searchFormHelper.DEFAULT_FILTERS).value;

0 commit comments

Comments
 (0)