Only patch saved views that have changed on settings save
This commit is contained in:
parent
782113a526
commit
7679154afa
@ -39,8 +39,8 @@ import { SettingsComponent } from './settings.component'
|
|||||||
import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
|
import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
|
||||||
|
|
||||||
const savedViews = [
|
const savedViews = [
|
||||||
{ id: 1, name: 'view1' },
|
{ id: 1, name: 'view1', show_in_sidebar: true, show_on_dashboard: true },
|
||||||
{ id: 2, name: 'view2' },
|
{ id: 2, name: 'view2', show_in_sidebar: false, show_on_dashboard: false },
|
||||||
]
|
]
|
||||||
const users = [
|
const users = [
|
||||||
{ id: 1, username: 'user1', is_superuser: false },
|
{ id: 1, username: 'user1', is_superuser: false },
|
||||||
@ -188,10 +188,20 @@ describe('SettingsComponent', () => {
|
|||||||
it('should support save saved views, show error', () => {
|
it('should support save saved views, show error', () => {
|
||||||
completeSetup()
|
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 toastErrorSpy = jest.spyOn(toastService, 'showError')
|
||||||
const toastSpy = jest.spyOn(toastService, 'show')
|
const toastSpy = jest.spyOn(toastService, 'show')
|
||||||
const savedViewPatchSpy = jest.spyOn(savedViewService, 'patchMany')
|
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
|
// saved views error first
|
||||||
savedViewPatchSpy.mockReturnValueOnce(
|
savedViewPatchSpy.mockReturnValueOnce(
|
||||||
throwError(() => new Error('unable to save saved views'))
|
throwError(() => new Error('unable to save saved views'))
|
||||||
@ -212,6 +222,40 @@ describe('SettingsComponent', () => {
|
|||||||
expect(savedViewPatchSpy).toHaveBeenCalled()
|
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', () => {
|
it('should support save local settings updating appearance settings and calling API, show error', () => {
|
||||||
completeSetup()
|
completeSetup()
|
||||||
jest.spyOn(savedViewService, 'patchMany').mockReturnValue(of([]))
|
jest.spyOn(savedViewService, 'patchMany').mockReturnValue(of([]))
|
||||||
|
@ -148,7 +148,6 @@ export class SettingsComponent
|
|||||||
.subscribe({
|
.subscribe({
|
||||||
next: (r) => {
|
next: (r) => {
|
||||||
this.users = r.results
|
this.users = r.results
|
||||||
this.initialize(false)
|
|
||||||
},
|
},
|
||||||
error: (e) => {
|
error: (e) => {
|
||||||
this.toastService.showError($localize`Error retrieving users`, e)
|
this.toastService.showError($localize`Error retrieving users`, e)
|
||||||
@ -168,7 +167,6 @@ export class SettingsComponent
|
|||||||
.subscribe({
|
.subscribe({
|
||||||
next: (r) => {
|
next: (r) => {
|
||||||
this.groups = r.results
|
this.groups = r.results
|
||||||
this.initialize(false)
|
|
||||||
},
|
},
|
||||||
error: (e) => {
|
error: (e) => {
|
||||||
this.toastService.showError($localize`Error retrieving groups`, e)
|
this.toastService.showError($localize`Error retrieving groups`, e)
|
||||||
@ -524,12 +522,15 @@ export class SettingsComponent
|
|||||||
}
|
}
|
||||||
|
|
||||||
saveSettings() {
|
saveSettings() {
|
||||||
let x = []
|
// only patch views that have actually changed
|
||||||
for (let id in this.savedViewGroup.value) {
|
const changed: PaperlessSavedView[] = []
|
||||||
x.push(this.savedViewGroup.value[id])
|
Object.values(this.savedViewGroup.controls)
|
||||||
}
|
.filter((g: FormGroup) => !g.pristine)
|
||||||
if (x.length > 0) {
|
.forEach((group: FormGroup) => {
|
||||||
this.savedViewService.patchMany(x).subscribe({
|
changed.push(group.value)
|
||||||
|
})
|
||||||
|
if (changed.length > 0) {
|
||||||
|
this.savedViewService.patchMany(changed).subscribe({
|
||||||
next: () => {
|
next: () => {
|
||||||
this.saveLocalSettings()
|
this.saveLocalSettings()
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user