Fix: workflow edit form loses unsaved changes in v2.3.1 (#5299)

This commit is contained in:
shamoon 2024-01-07 08:16:58 -08:00 committed by GitHub
parent a41dbdd12c
commit 86338465fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 54 deletions

View File

@ -66,7 +66,7 @@ const workflow: Workflow = {
], ],
} }
describe('ConsumptionTemplateEditDialogComponent', () => { describe('WorkflowEditDialogComponent', () => {
let component: WorkflowEditDialogComponent let component: WorkflowEditDialogComponent
let settingsService: SettingsService let settingsService: SettingsService
let fixture: ComponentFixture<WorkflowEditDialogComponent> let fixture: ComponentFixture<WorkflowEditDialogComponent>
@ -219,6 +219,7 @@ describe('ConsumptionTemplateEditDialogComponent', () => {
const action1 = workflow.actions[0] const action1 = workflow.actions[0]
const action2 = workflow.actions[1] const action2 = workflow.actions[1]
component.object = workflow component.object = workflow
component.ngOnInit()
component.onActionDrop({ previousIndex: 0, currentIndex: 1 } as CdkDragDrop< component.onActionDrop({ previousIndex: 0, currentIndex: 1 } as CdkDragDrop<
WorkflowAction[] WorkflowAction[]
>) >)

View File

@ -19,6 +19,7 @@ import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service
import { CustomField } from 'src/app/data/custom-field' import { CustomField } from 'src/app/data/custom-field'
import { import {
DocumentSource, DocumentSource,
WorkflowTrigger,
WorkflowTriggerType, WorkflowTriggerType,
} from 'src/app/data/workflow-trigger' } from 'src/app/data/workflow-trigger'
import { import {
@ -157,7 +158,7 @@ export class WorkflowEditDialogComponent
ngOnInit(): void { ngOnInit(): void {
super.ngOnInit() super.ngOnInit()
this.updateTriggerActionFields() this.updateAllTriggerActionFields()
} }
get triggerFields(): FormArray { get triggerFields(): FormArray {
@ -168,52 +169,66 @@ export class WorkflowEditDialogComponent
return this.objectForm.get('actions') as FormArray return this.objectForm.get('actions') as FormArray
} }
private updateTriggerActionFields(emitEvent: boolean = false) { private createTriggerField(
trigger: WorkflowTrigger,
emitEvent: boolean = false
) {
this.triggerFields.push(
new FormGroup({
id: new FormControl(trigger.id),
type: new FormControl(trigger.type),
sources: new FormControl(trigger.sources),
filter_filename: new FormControl(trigger.filter_filename),
filter_path: new FormControl(trigger.filter_path),
filter_mailrule: new FormControl(trigger.filter_mailrule),
matching_algorithm: new FormControl(trigger.matching_algorithm),
match: new FormControl(trigger.match),
is_insensitive: new FormControl(trigger.is_insensitive),
filter_has_tags: new FormControl(trigger.filter_has_tags),
filter_has_correspondent: new FormControl(
trigger.filter_has_correspondent
),
filter_has_document_type: new FormControl(
trigger.filter_has_document_type
),
}),
{ emitEvent }
)
}
private createActionField(
action: WorkflowAction,
emitEvent: boolean = false
) {
this.actionFields.push(
new FormGroup({
id: new FormControl(action.id),
type: new FormControl(action.type),
assign_title: new FormControl(action.assign_title),
assign_tags: new FormControl(action.assign_tags),
assign_owner: new FormControl(action.assign_owner),
assign_document_type: new FormControl(action.assign_document_type),
assign_correspondent: new FormControl(action.assign_correspondent),
assign_storage_path: new FormControl(action.assign_storage_path),
assign_view_users: new FormControl(action.assign_view_users),
assign_view_groups: new FormControl(action.assign_view_groups),
assign_change_users: new FormControl(action.assign_change_users),
assign_change_groups: new FormControl(action.assign_change_groups),
assign_custom_fields: new FormControl(action.assign_custom_fields),
}),
{ emitEvent }
)
}
private updateAllTriggerActionFields(emitEvent: boolean = false) {
this.triggerFields.clear({ emitEvent: false }) this.triggerFields.clear({ emitEvent: false })
this.object?.triggers.forEach((trigger) => { this.object?.triggers.forEach((trigger) => {
this.triggerFields.push( this.createTriggerField(trigger, emitEvent)
new FormGroup({
id: new FormControl(trigger.id),
type: new FormControl(trigger.type),
sources: new FormControl(trigger.sources),
filter_filename: new FormControl(trigger.filter_filename),
filter_path: new FormControl(trigger.filter_path),
filter_mailrule: new FormControl(trigger.filter_mailrule),
matching_algorithm: new FormControl(trigger.matching_algorithm),
match: new FormControl(trigger.match),
is_insensitive: new FormControl(trigger.is_insensitive),
filter_has_tags: new FormControl(trigger.filter_has_tags),
filter_has_correspondent: new FormControl(
trigger.filter_has_correspondent
),
filter_has_document_type: new FormControl(
trigger.filter_has_document_type
),
}),
{ emitEvent }
)
}) })
this.actionFields.clear({ emitEvent: false }) this.actionFields.clear({ emitEvent: false })
this.object?.actions.forEach((action) => { this.object?.actions.forEach((action) => {
this.actionFields.push( this.createActionField(action, emitEvent)
new FormGroup({
id: new FormControl(action.id),
type: new FormControl(action.type),
assign_title: new FormControl(action.assign_title),
assign_tags: new FormControl(action.assign_tags),
assign_owner: new FormControl(action.assign_owner),
assign_document_type: new FormControl(action.assign_document_type),
assign_correspondent: new FormControl(action.assign_correspondent),
assign_storage_path: new FormControl(action.assign_storage_path),
assign_view_users: new FormControl(action.assign_view_users),
assign_view_groups: new FormControl(action.assign_view_groups),
assign_change_users: new FormControl(action.assign_change_users),
assign_change_groups: new FormControl(action.assign_change_groups),
assign_custom_fields: new FormControl(action.assign_custom_fields),
}),
{ emitEvent }
)
}) })
} }
@ -233,7 +248,7 @@ export class WorkflowEditDialogComponent
if (!this.object) { if (!this.object) {
this.object = Object.assign({}, this.objectForm.value) this.object = Object.assign({}, this.objectForm.value)
} }
this.object.triggers.push({ const trigger: WorkflowTrigger = {
type: WorkflowTriggerType.Consumption, type: WorkflowTriggerType.Consumption,
sources: [], sources: [],
filter_filename: null, filter_filename: null,
@ -245,9 +260,9 @@ export class WorkflowEditDialogComponent
matching_algorithm: MATCH_NONE, matching_algorithm: MATCH_NONE,
match: null, match: null,
is_insensitive: true, is_insensitive: true,
}) }
this.object.triggers.push(trigger)
this.updateTriggerActionFields() this.createTriggerField(trigger)
} }
get actionTypeOptions() { get actionTypeOptions() {
@ -262,7 +277,7 @@ export class WorkflowEditDialogComponent
if (!this.object) { if (!this.object) {
this.object = Object.assign({}, this.objectForm.value) this.object = Object.assign({}, this.objectForm.value)
} }
this.object.actions.push({ const action: WorkflowAction = {
type: WorkflowActionType.Assignment, type: WorkflowActionType.Assignment,
assign_title: null, assign_title: null,
assign_tags: [], assign_tags: [],
@ -275,19 +290,19 @@ export class WorkflowEditDialogComponent
assign_change_users: [], assign_change_users: [],
assign_change_groups: [], assign_change_groups: [],
assign_custom_fields: [], assign_custom_fields: [],
}) }
this.object.actions.push(action)
this.updateTriggerActionFields() this.createActionField(action)
} }
removeTrigger(index: number) { removeTrigger(index: number) {
this.object.triggers.splice(index, 1) this.object.triggers.splice(index, 1).pop()
this.updateTriggerActionFields() this.triggerFields.removeAt(index)
} }
removeAction(index: number) { removeAction(index: number) {
this.object.actions.splice(index, 1) this.object.actions.splice(index, 1)
this.updateTriggerActionFields() this.actionFields.removeAt(index)
} }
onActionDrop(event: CdkDragDrop<WorkflowAction[]>) { onActionDrop(event: CdkDragDrop<WorkflowAction[]>) {
@ -296,8 +311,10 @@ export class WorkflowEditDialogComponent
event.previousIndex, event.previousIndex,
event.currentIndex event.currentIndex
) )
const actionField = this.actionFields.at(event.previousIndex)
this.actionFields.removeAt(event.previousIndex)
this.actionFields.insert(event.currentIndex, actionField)
// removing id will effectively re-create the actions in this order // removing id will effectively re-create the actions in this order
this.object.actions.forEach((a) => (a.id = null)) this.object.actions.forEach((a) => (a.id = null))
this.updateTriggerActionFields()
} }
} }