Prevent duplicate api calls on text filtering

This commit is contained in:
Michael Shamoon
2022-06-13 15:35:00 -07:00
parent 19c525001b
commit 1c87b5c995
3 changed files with 58 additions and 33 deletions

View File

@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'
import { ParamMap, Router } from '@angular/router'
import { Observable } from 'rxjs'
import {
filterRulesDiffer,
cloneFilterRules,
FilterRule,
isFullTextFilterRule,
@@ -67,6 +68,7 @@ export interface ListViewState {
})
export class DocumentListViewService {
isReloading: boolean = false
initialized: boolean = false
error: string = null
rangeSelectionAnchorIndex: number
@@ -172,11 +174,24 @@ export class DocumentListViewService {
if (!paramsEmpty) newState = parseParams(queryParams)
if (newState == undefined) newState = this.defaultListViewState() // if nothing in local storage
this.activeListViewState.filterRules = newState.filterRules
this.activeListViewState.sortField = newState.sortField
this.activeListViewState.sortReverse = newState.sortReverse
this.activeListViewState.currentPage = newState.currentPage
this.reload(null, paramsEmpty) // update the params if there arent any
// only reload if things have changed
if (
!this.initialized ||
paramsEmpty ||
this.activeListViewState.sortField !== newState.sortField ||
this.activeListViewState.sortReverse !== newState.sortReverse ||
this.activeListViewState.currentPage !== newState.currentPage ||
filterRulesDiffer(
this.activeListViewState.filterRules,
newState.filterRules
)
) {
this.activeListViewState.filterRules = newState.filterRules
this.activeListViewState.sortField = newState.sortField
this.activeListViewState.sortReverse = newState.sortReverse
this.activeListViewState.currentPage = newState.currentPage
this.reload(null, paramsEmpty) // update the params if there arent any
}
}
reload(onFinish?, updateQueryParams: boolean = true) {
@@ -193,6 +208,7 @@ export class DocumentListViewService {
)
.subscribe({
next: (result) => {
this.initialized = true
this.isReloading = false
activeListViewState.collectionSize = result.count
activeListViewState.documents = result.results