Skip to content

Commit

Permalink
Merge pull request #4761 from manuelmeister/bugfix/api-wrapper-dirty
Browse files Browse the repository at this point in the history
Bugfix: Prevent wrong dirty reset
  • Loading branch information
manuelmeister committed Mar 17, 2024
2 parents a15b036 + 1c01512 commit e08a8f1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions frontend/src/components/form/api/ApiRichtext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Displays a field as a e-textarea + write access via API wrapper
:outlined="outlined"
:filled="filled"
@input="wrapper.on.input"
@blur="wrapper.on.blur"
>
<template #append>
<api-wrapper-append :wrapper="wrapper" />
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/form/api/ApiTextField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Displays a field as a e-text-field + write access via API wrapper
:filled="filled"
:dense="dense"
@input="wrapper.on.input"
@blur="wrapper.on.blur"
>
<template #append>
<api-wrapper-append :wrapper="wrapper" />
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/form/api/ApiTextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Displays a field as a e-textarea + write access via API wrapper
:filled="filled"
:dense="dense"
@input="wrapper.on.input"
@blur="wrapper.on.blur"
>
<template #append>
<api-wrapper-append :wrapper="wrapper" />
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/components/form/api/ApiWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default {
localValue: null,
parsedLocalValue: null,
isSaving: false,
isPreSaving: false,
isLoading: false,
isMounted: false,
showIconSuccess: false,
Expand All @@ -67,6 +68,7 @@ export default {
reset: this.reset,
reload: this.reload,
input: this.onInput,
blur: this.onBlur,
},
}
},
Expand Down Expand Up @@ -172,11 +174,18 @@ export default {
this.localValue = newValue
this.parsedLocalValue = this.parse ? this.parse(newValue) : newValue
this.dirty = this.parsedLocalValue !== this.apiValue
this.isPreSaving = true
if (this.autoSave) {
this.debouncedSave()
}
},
async onBlur() {
if (!(this.isSaving || this.isPreSaving) && this.autoSave) {
this.localValue = this.apiValue
this.parsedLocalValue = this.parse ? this.parse(this.apiValue) : this.apiValue
}
},
// reload data from API (doesn't force loading from server if available locally)
reload() {
this.resetErrors()
Expand All @@ -196,12 +205,12 @@ export default {
},
reset() {
this.localValue = this.apiValue
this.dirty = false
this.resetErrors()
this.$emit('reseted')
this.$emit('finished')
},
resetErrors() {
this.dirty = false
this.hasLoadingError = false
this.hasServerError = false
this.serverErrorMessage = null
Expand Down Expand Up @@ -229,6 +238,7 @@ export default {
// reset all dirty flags and start saving
this.resetErrors()
this.isPreSaving = false
this.isSaving = true
// construct payload (nested path allowed)
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/form/api/__tests__/ApiWrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('Testing ApiWrapper [autoSave=true; manual external value]', () => {

test('calls api.patch after onInput was triggered', async () => {
const newValue = 'new value'
const newValueFromApi = 'NEW VALUE'
const newValueFromApi = 'new value'

await vm.onInput(newValue)

Expand All @@ -148,7 +148,7 @@ describe('Testing ApiWrapper [autoSave=true; manual external value]', () => {

// saving started
expect(vm.isSaving).toBe(true)
expect(vm.dirty).toBe(false)
expect(vm.dirty).toBe(true)
expect(vm.status).toBe('saving')

// API patch method called
Expand All @@ -163,6 +163,7 @@ describe('Testing ApiWrapper [autoSave=true; manual external value]', () => {
await wrapper.setProps({ value: newValueFromApi })
await wrapper.vm.$nextTick()
expect(vm.localValue).toBe(newValueFromApi)
expect(vm.dirty).toBe(false)

// success state
expect(vm.status).toBe('success')
Expand Down

0 comments on commit e08a8f1

Please sign in to comment.