mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-14 00:26:21 +00:00
Enhancement: display saved view counts (#10246)
This commit is contained in:
@@ -17,7 +17,7 @@ const saved_views = [
|
||||
id: 1,
|
||||
show_on_dashboard: true,
|
||||
show_in_sidebar: true,
|
||||
sort_field: 'name',
|
||||
sort_field: 'title',
|
||||
sort_reverse: true,
|
||||
filter_rules: [],
|
||||
},
|
||||
@@ -26,7 +26,7 @@ const saved_views = [
|
||||
id: 2,
|
||||
show_on_dashboard: true,
|
||||
show_in_sidebar: true,
|
||||
sort_field: 'name',
|
||||
sort_field: 'created',
|
||||
sort_reverse: true,
|
||||
filter_rules: [],
|
||||
},
|
||||
@@ -35,7 +35,7 @@ const saved_views = [
|
||||
id: 3,
|
||||
show_on_dashboard: true,
|
||||
show_in_sidebar: true,
|
||||
sort_field: 'name',
|
||||
sort_field: 'added',
|
||||
sort_reverse: true,
|
||||
filter_rules: [],
|
||||
},
|
||||
@@ -44,7 +44,7 @@ const saved_views = [
|
||||
id: 4,
|
||||
show_on_dashboard: false,
|
||||
show_in_sidebar: false,
|
||||
sort_field: 'name',
|
||||
sort_field: 'owner',
|
||||
sort_reverse: true,
|
||||
filter_rules: [],
|
||||
},
|
||||
@@ -222,6 +222,43 @@ describe(`Additional service tests for SavedViewService`, () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should accept a callback for reload', () => {
|
||||
const reloadSpy = jest.fn()
|
||||
service.reload(reloadSpy)
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}${endpoint}/?page=1&page_size=100000`
|
||||
)
|
||||
req.flush({
|
||||
results: saved_views,
|
||||
})
|
||||
expect(reloadSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should support getting document counts for views', () => {
|
||||
service.maybeRefreshDocumentCounts(saved_views)
|
||||
saved_views.forEach((saved_view) => {
|
||||
const req = httpTestingController.expectOne(
|
||||
`${environment.apiBaseUrl}documents/?page=1&page_size=1&ordering=-${saved_view.sort_field}&fields=id&truncate_content=true`
|
||||
)
|
||||
req.flush({
|
||||
all: [],
|
||||
count: 1,
|
||||
results: [{ id: 1 }],
|
||||
})
|
||||
})
|
||||
expect(service.getDocumentCount(saved_views[0])).toEqual(1)
|
||||
})
|
||||
|
||||
it('should not refresh document counts if setting is disabled', () => {
|
||||
jest.spyOn(settingsService, 'get').mockImplementation((key) => {
|
||||
if (key === SETTINGS_KEYS.SIDEBAR_VIEWS_SHOW_COUNT) return false
|
||||
})
|
||||
service.maybeRefreshDocumentCounts(saved_views)
|
||||
httpTestingController.expectNone(
|
||||
`${environment.apiBaseUrl}documents/?page=1&page_size=1&ordering=-${saved_views[0].sort_field}&fields=id&truncate_content=true`
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
// Dont need to setup again
|
||||
|
||||
|
@@ -1,12 +1,13 @@
|
||||
import { HttpClient } from '@angular/common/http'
|
||||
import { inject, Injectable } from '@angular/core'
|
||||
import { combineLatest, Observable } from 'rxjs'
|
||||
import { tap } from 'rxjs/operators'
|
||||
import { combineLatest, Observable, Subject } from 'rxjs'
|
||||
import { takeUntil, tap } from 'rxjs/operators'
|
||||
import { Results } from 'src/app/data/results'
|
||||
import { SavedView } from 'src/app/data/saved-view'
|
||||
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
||||
import { SettingsService } from '../settings.service'
|
||||
import { AbstractPaperlessService } from './abstract-paperless-service'
|
||||
import { DocumentService } from './document.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -14,9 +15,12 @@ import { AbstractPaperlessService } from './abstract-paperless-service'
|
||||
export class SavedViewService extends AbstractPaperlessService<SavedView> {
|
||||
protected http: HttpClient
|
||||
private settingsService = inject(SettingsService)
|
||||
private documentService = inject(DocumentService)
|
||||
|
||||
public loading: boolean = true
|
||||
private savedViews: SavedView[] = []
|
||||
private savedViewDocumentCounts: Map<number, number> = new Map()
|
||||
private unsubscribeNotifier: Subject<void> = new Subject<void>()
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
@@ -46,8 +50,16 @@ export class SavedViewService extends AbstractPaperlessService<SavedView> {
|
||||
)
|
||||
}
|
||||
|
||||
public reload() {
|
||||
this.listAll().subscribe()
|
||||
public reload(callback: any = null) {
|
||||
this.listAll()
|
||||
.pipe(
|
||||
tap((r) => {
|
||||
if (callback) {
|
||||
callback(r)
|
||||
}
|
||||
})
|
||||
)
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
get allViews() {
|
||||
@@ -110,4 +122,30 @@ export class SavedViewService extends AbstractPaperlessService<SavedView> {
|
||||
delete(o: SavedView) {
|
||||
return super.delete(o).pipe(tap(() => this.reload()))
|
||||
}
|
||||
|
||||
public maybeRefreshDocumentCounts(views: SavedView[] = this.sidebarViews) {
|
||||
if (!this.settingsService.get(SETTINGS_KEYS.SIDEBAR_VIEWS_SHOW_COUNT)) {
|
||||
return
|
||||
}
|
||||
this.unsubscribeNotifier.next() // clear previous subscriptions
|
||||
views.forEach((view) => {
|
||||
this.documentService
|
||||
.listFiltered(
|
||||
1,
|
||||
1,
|
||||
view.sort_field,
|
||||
view.sort_reverse,
|
||||
view.filter_rules,
|
||||
{ fields: 'id', truncate_content: true }
|
||||
)
|
||||
.pipe(takeUntil(this.unsubscribeNotifier))
|
||||
.subscribe((results: Results<Document>) => {
|
||||
this.savedViewDocumentCounts.set(view.id, results.count)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
public getDocumentCount(view: SavedView): number {
|
||||
return this.savedViewDocumentCounts.get(view.id)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user