From 0ecb7298b9f4ce6ef2e217290c0e20392db8aae9 Mon Sep 17 00:00:00 2001 From: Ketevani Kvirikashvili Date: Tue, 31 May 2022 15:58:39 +0200 Subject: [PATCH] [AAE-8740] Add a confirmation message in ADW --- .../form/components/form-base.component.ts | 1 + .../components/widgets/core/form.model.ts | 2 + .../components/form-cloud.component.spec.ts | 96 +++++++++++-------- .../form/components/form-cloud.component.ts | 82 ++++++++++++---- 4 files changed, 119 insertions(+), 62 deletions(-) diff --git a/lib/core/form/components/form-base.component.ts b/lib/core/form/components/form-base.component.ts index 8bdfa9450a..6aed4e6ad4 100644 --- a/lib/core/form/components/form-base.component.ts +++ b/lib/core/form/components/form-base.component.ts @@ -161,6 +161,7 @@ export abstract class FormBaseComponent { * @param outcome Form outcome model */ onOutcomeClicked(outcome: FormOutcomeModel): boolean { + debugger if (!this.readOnly && outcome && this.form) { if (!this.onExecuteOutcome(outcome)) { diff --git a/lib/core/form/components/widgets/core/form.model.ts b/lib/core/form/components/widgets/core/form.model.ts index 8edb5c97f7..2f40c1f44d 100644 --- a/lib/core/form/components/widgets/core/form.model.ts +++ b/lib/core/form/components/widgets/core/form.model.ts @@ -65,6 +65,7 @@ export class FormModel implements ProcessFormModel { readonly id: string | number; readonly name: string; + readonly confirmMessage: {show: boolean, message: string}; readonly taskId: string; readonly taskName = FormModel.UNSET_TASK_NAME; readonly processDefinitionId: string; @@ -93,6 +94,7 @@ export class FormModel implements ProcessFormModel { if (json) { this.id = json.id; this.name = json.name; + this.confirmMessage = json.confirmMessage || {}; this.taskId = json.taskId; this.taskName = json.taskName || json.name || FormModel.UNSET_TASK_NAME; this.processDefinitionId = json.processDefinitionId; diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts index 8ac02de243..648ba00bbb 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts @@ -689,6 +689,7 @@ fdescribe('FormCloudComponent', () => { }); it('should require form with appName and taskId to complete', () => { + debugger; spyOn(formCloudService, 'completeTaskForm').and.stub(); formComponent.form = null; @@ -744,60 +745,73 @@ fdescribe('FormCloudComponent', () => { expect(formComponent.parseForm(null)).toBeNull(); }); - fit('should open confirmation dialog on complete task', () => { - spyOn(matDialog, 'open').and.returnValue({ - afterClosed: () => of(false) - } as any); + it('should open confirmation dialog on complete task', () => { + // spyOn(matDialog, 'open').and.returnValue({ beforeClosed: () => of() } as any); + spyOn(matDialog, 'open').and.returnValue({ afterClosed: () => of(false) } as any); + formComponent.form = new FormModel({ + confirmMessage: { + show: true, + message: 'Are you sure you want to submit the form?' + } + }); + formComponent.completeTaskForm(); expect(matDialog.open).toHaveBeenCalled(); }); - // fit('should submit form when user confirms', () => { - // fixture.detectChanges(); - // spyOn(matDialog, 'open').and.returnValue({ - // afterClosed: () => of(true) - // } as any); + it('should submit form when user confirms', () => { + fixture.detectChanges(); + const afterClose = of(true); - // spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); + spyOn(matDialog, 'open').and.returnValue({ + afterClosed: () => afterClose + } as any); - // const taskId = '123-223'; - // const appVersion = 1; - // const appName = 'test-app'; - // const processInstanceId = '333-444'; + spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); + const outcome = 'complete'; - // const formModel = new FormModel({ - // id: '23', - // taskId, - // fields: [ - // { id: 'field1' }, - // { id: 'field2' } - // ] - // }); + const taskId = '123-223'; + const appVersion = 1; + const appName = 'test-app'; + const processInstanceId = '333-444'; - // formComponent.appVersion = appVersion; - // formComponent.form = formModel; - // formComponent.taskId = taskId; - // formComponent.appName = appName; - // formComponent.processInstanceId = processInstanceId; - // formComponent.completeTaskForm('outcome'); - // expect(matDialog.open).toHaveBeenCalled(); - // expect(formComponent['formCloudService'].completeTaskForm).toHaveBeenCalledWith(appName, formModel.taskId, processInstanceId, formModel.id, formModel.values, 'outcome', appVersion); - // }); + const formModel = new FormModel({ + id: '23', + taskId, + fields: [ + { id: 'field1' }, + { id: 'field2' } + ], + confirmMessage: { + show: true, + message: 'Are you sure you want to submit the form?' + } + }); - // fit('should not confirm form if user rejects', () => { - // spyOn(matDialog, 'open').and.returnValue({ - // afterClosed: () => of(false) - // } as any); + formComponent.appVersion = appVersion; + formComponent.form = formModel; + formComponent.taskId = taskId; + formComponent.appName = appName; + formComponent.processInstanceId = processInstanceId; + formComponent.completeTaskForm(outcome); + expect(matDialog.open).toHaveBeenCalled(); - // spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); - // formComponent.completeTaskForm(); + expect(formComponent['formCloudService'].completeTaskForm).toHaveBeenCalledWith(appName, formModel.taskId, processInstanceId, formModel.id, formModel.values, outcome, appVersion); + }); - // expect(formCloudService.completeTaskForm).not.toHaveBeenCalled(); + it('should not confirm form if user rejects', () => { + const outcome = 'complete'; - // formComponent.completeTaskForm('outcome'); - // expect(formComponent['formCloudService'].completeTaskForm).not.toHaveBeenCalled(); - // }); + spyOn(matDialog, 'open').and.returnValue({ + afterClosed: () => of(false) + } as any); + + spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); + formComponent.completeTaskForm(outcome); + + expect(formComponent['formCloudService'].completeTaskForm).not.toHaveBeenCalled(); + }); it('should parse form from json', () => { const form = formComponent.parseForm({ diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts index 72110711a2..9ecba447e0 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts @@ -272,30 +272,70 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges, completeTaskForm(outcome?: string) { debugger; - // this.form.values.confirmMessage - const dialogRef = this.dialog.open(ConfirmDialogComponent, { - data: { - title: 'Save the form', - message: 'Do you want to save the form?' - }, - minWidth: '250px' - }); - dialogRef.afterClosed().subscribe((result) => { - if (result === true) { - if (this.form && this.appName && this.taskId) { - this.formCloudService - .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) - .pipe(takeUntil(this.onDestroy$)) - .subscribe( - () => { - this.onTaskCompleted(this.form); - }, - (error) => this.onTaskCompletedError(error) - ); + // const confirmMessage = this.form.json.confirmMessage.message; + + if (this.form?.confirmMessage?.show === true) { + const dialogRef = this.dialog.open(ConfirmDialogComponent, { + data: { + title: 'Save the form', + message: this.form.confirmMessage.message + }, + minWidth: '450px' + }); + + dialogRef.afterClosed().subscribe((result) => { + if (result === true) { + if (this.form && this.appName && this.taskId) { + this.formCloudService + .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) + .pipe(takeUntil(this.onDestroy$)) + .subscribe( + () => { + this.onTaskCompleted(this.form); + }, + (error) => this.onTaskCompletedError(error) + ); + } } + }); + } else { + if (this.form && this.appName && this.taskId) { + this.formCloudService + .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) + .pipe(takeUntil(this.onDestroy$)) + .subscribe( + () => { + this.onTaskCompleted(this.form); + }, + (error) => this.onTaskCompletedError(error) + ); } - }); + } + + // const dialogRef = this.dialog.open(ConfirmDialogComponent, { + // data: { + // title: 'Save the form', + // message: this.form.confirmMessage.message + // }, + // minWidth: '450px' + // }); + + // dialogRef.afterClosed().subscribe((result) => { + // if (result === true) { + // if (this.form && this.appName && this.taskId) { + // this.formCloudService + // .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) + // .pipe(takeUntil(this.onDestroy$)) + // .subscribe( + // () => { + // this.onTaskCompleted(this.form); + // }, + // (error) => this.onTaskCompletedError(error) + // ); + // } + // } + // }); } parseForm(formCloudRepresentationJSON: any): FormModel {