From 7679154afa716c321412bc2f402b4a4c373db82f Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 23 Sep 2023 09:58:03 -0700 Subject: [PATCH] Only patch saved views that have changed on settings save --- .../admin/settings/settings.component.spec.ts | 48 ++++++++++++++++++- .../admin/settings/settings.component.ts | 17 +++---- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src-ui/src/app/components/admin/settings/settings.component.spec.ts b/src-ui/src/app/components/admin/settings/settings.component.spec.ts index b7e0c0ed3..7ea025fe5 100644 --- a/src-ui/src/app/components/admin/settings/settings.component.spec.ts +++ b/src-ui/src/app/components/admin/settings/settings.component.spec.ts @@ -39,8 +39,8 @@ import { SettingsComponent } from './settings.component' import { IfOwnerDirective } from 'src/app/directives/if-owner.directive' const savedViews = [ - { id: 1, name: 'view1' }, - { id: 2, name: 'view2' }, + { id: 1, name: 'view1', show_in_sidebar: true, show_on_dashboard: true }, + { id: 2, name: 'view2', show_in_sidebar: false, show_on_dashboard: false }, ] const users = [ { id: 1, username: 'user1', is_superuser: false }, @@ -188,10 +188,20 @@ describe('SettingsComponent', () => { it('should support save saved views, show error', () => { completeSetup() + const tabButtons = fixture.debugElement.queryAll(By.directive(NgbNavLink)) + tabButtons[3].nativeElement.dispatchEvent(new MouseEvent('click')) + fixture.detectChanges() + const toastErrorSpy = jest.spyOn(toastService, 'showError') const toastSpy = jest.spyOn(toastService, 'show') const savedViewPatchSpy = jest.spyOn(savedViewService, 'patchMany') + const toggle = fixture.debugElement.query( + By.css('.form-check.form-switch input') + ) + toggle.nativeElement.checked = true + toggle.nativeElement.dispatchEvent(new Event('change')) + // saved views error first savedViewPatchSpy.mockReturnValueOnce( throwError(() => new Error('unable to save saved views')) @@ -212,6 +222,40 @@ describe('SettingsComponent', () => { expect(savedViewPatchSpy).toHaveBeenCalled() }) + it('should update only patch saved views that have changed', () => { + completeSetup() + + const tabButtons = fixture.debugElement.queryAll(By.directive(NgbNavLink)) + tabButtons[3].nativeElement.dispatchEvent(new MouseEvent('click')) + fixture.detectChanges() + + const patchSpy = jest.spyOn(savedViewService, 'patchMany') + component.saveSettings() + expect(patchSpy).not.toHaveBeenCalled() + + const view = savedViews[0] + const toggle = fixture.debugElement.query( + By.css('.form-check.form-switch input') + ) + toggle.nativeElement.checked = true + toggle.nativeElement.dispatchEvent(new Event('change')) + // register change + component.savedViewGroup.get(view.id.toString()).value[ + 'show_on_dashboard' + ] = !view.show_on_dashboard + fixture.detectChanges() + + component.saveSettings() + expect(patchSpy).toHaveBeenCalledWith([ + { + id: view.id, + name: view.name, + show_in_sidebar: view.show_in_sidebar, + show_on_dashboard: !view.show_on_dashboard, + }, + ]) + }) + it('should support save local settings updating appearance settings and calling API, show error', () => { completeSetup() jest.spyOn(savedViewService, 'patchMany').mockReturnValue(of([])) diff --git a/src-ui/src/app/components/admin/settings/settings.component.ts b/src-ui/src/app/components/admin/settings/settings.component.ts index 78992a32c..0c9a92efa 100644 --- a/src-ui/src/app/components/admin/settings/settings.component.ts +++ b/src-ui/src/app/components/admin/settings/settings.component.ts @@ -148,7 +148,6 @@ export class SettingsComponent .subscribe({ next: (r) => { this.users = r.results - this.initialize(false) }, error: (e) => { this.toastService.showError($localize`Error retrieving users`, e) @@ -168,7 +167,6 @@ export class SettingsComponent .subscribe({ next: (r) => { this.groups = r.results - this.initialize(false) }, error: (e) => { this.toastService.showError($localize`Error retrieving groups`, e) @@ -524,12 +522,15 @@ export class SettingsComponent } saveSettings() { - let x = [] - for (let id in this.savedViewGroup.value) { - x.push(this.savedViewGroup.value[id]) - } - if (x.length > 0) { - this.savedViewService.patchMany(x).subscribe({ + // only patch views that have actually changed + const changed: PaperlessSavedView[] = [] + Object.values(this.savedViewGroup.controls) + .filter((g: FormGroup) => !g.pristine) + .forEach((group: FormGroup) => { + changed.push(group.value) + }) + if (changed.length > 0) { + this.savedViewService.patchMany(changed).subscribe({ next: () => { this.saveLocalSettings() },