@@ -30,11 +30,18 @@ import {StandardValidationErrors} from "../../../../common/services/validators/v
3030import { AbstractControl , AsyncValidatorFn } from "@angular/forms" ;
3131import { AsyncProcessValidatorInterface } from "../aync-validator.Interface" ;
3232import { Process , ProcessService } from "../../../process/process.service" ;
33- import { map , take } from "rxjs/operators" ;
34- import { Observable } from "rxjs" ;
33+ import { map , switchMap , take } from "rxjs/operators" ;
34+ import { Observable , of , Subject } from "rxjs" ;
3535import { AsyncValidationDefinition } from "../../../../common/record/field.model" ;
36-
37- export const asyncValidator = ( validator : AsyncValidationDefinition , viewField : ViewFieldDefinition , record : Record , processService : ProcessService ) : AsyncValidatorFn => (
36+ import { ConfirmationModalService } from "../../../modals/confirmation-modal.service" ;
37+
38+ export const asyncValidator = (
39+ validator : AsyncValidationDefinition ,
40+ viewField : ViewFieldDefinition ,
41+ record : Record ,
42+ processService : ProcessService ,
43+ confirmationModalService : ConfirmationModalService
44+ ) : AsyncValidatorFn => (
3845 ( control : AbstractControl ) : Promise < StandardValidationErrors | null > | Observable < StandardValidationErrors | null > => {
3946
4047 const processKey = validator . key ;
@@ -48,10 +55,67 @@ export const asyncValidator = (validator: AsyncValidationDefinition, viewField:
4855 params : validator ?. params ?? { }
4956 } ;
5057
51- return processService . submit ( processKey , options ) . pipe ( map ( ( process : Process ) => {
58+ return processService . submit ( processKey , options ) . pipe ( switchMap ( ( process : Process ) => {
5259
5360 if ( process . status !== 'error' ) {
54- return null ;
61+ return of ( null ) ;
62+ }
63+
64+ if ( process ?. data ?. displayConfirmation ?? false ) {
65+ const confirmationSubject = new Subject < boolean > ( ) ;
66+ const confirmation$ = confirmationSubject . asObservable ( ) ;
67+
68+ const confirmationLabel = process . data ?. confirmationLabel ?? '' ;
69+ const confirmationMessages = process . data ?. confirmationMessages ?? [ ] ;
70+ const confirmation = [ confirmationLabel , ...confirmationMessages ] ;
71+
72+ if ( Object . entries ( confirmation ) . length === 0 ) {
73+ confirmation . push ( 'LBL_GENERIC_CONFIRMATION' ) ;
74+ }
75+
76+ confirmationModalService . showModal (
77+ confirmation ,
78+ ( ) => confirmationSubject . next ( true ) ,
79+ ( ) => confirmationSubject . next ( false ) ,
80+ record . fields ,
81+ { value : control . value }
82+ )
83+
84+ return confirmation$ . pipe (
85+ take ( 1 ) ,
86+ map ( ( confirmed : boolean ) => {
87+ if ( confirmed ) {
88+ return null ;
89+ }
90+
91+ const error = {
92+ [ processKey ] : {
93+ message : {
94+ labels : {
95+ startLabelKey : '' ,
96+ icon : '' ,
97+ endLabelKey : '' ,
98+ } ,
99+ context : {
100+ value : control . value
101+ }
102+ }
103+ }
104+ }
105+
106+ if ( process ?. data ?. errors ?? false ) {
107+ Object . keys ( process ?. data ?. errors ) . forEach ( ( key ) => {
108+ if ( error [ processKey ] . message . labels [ key ] === '' ) {
109+ error [ processKey ] . message . labels [ key ] = process ?. data ?. errors [ key ] ;
110+ }
111+ } ) ;
112+ }
113+
114+ record . fields [ viewField . name ] . asyncValidationErrors = error ;
115+
116+ return error ;
117+ } )
118+ ) ;
55119 }
56120
57121 const error = {
@@ -69,9 +133,9 @@ export const asyncValidator = (validator: AsyncValidationDefinition, viewField:
69133 }
70134 }
71135
72- if ( process ?. data ?. errors ?? false ) {
136+ if ( process ?. data ?. errors ?? false ) {
73137 Object . keys ( process ?. data ?. errors ) . forEach ( ( key ) => {
74- if ( error [ processKey ] . message . labels [ key ] === '' ) {
138+ if ( error [ processKey ] . message . labels [ key ] === '' ) {
75139 error [ processKey ] . message . labels [ key ] = process ?. data ?. errors [ key ] ;
76140 }
77141 } ) ;
@@ -80,7 +144,7 @@ export const asyncValidator = (validator: AsyncValidationDefinition, viewField:
80144
81145 record . fields [ viewField . name ] . asyncValidationErrors = error ;
82146
83- return error ;
147+ return of ( error ) ;
84148 } ) , take ( 1 ) ) ;
85149 }
86150) ;
@@ -91,7 +155,8 @@ export const asyncValidator = (validator: AsyncValidationDefinition, viewField:
91155export class AsyncProcessValidator implements AsyncProcessValidatorInterface {
92156
93157 constructor (
94- protected processService : ProcessService
158+ protected processService : ProcessService ,
159+ protected confirmationModalService : ConfirmationModalService
95160 ) {
96161 }
97162
@@ -104,10 +169,10 @@ export class AsyncProcessValidator implements AsyncProcessValidatorInterface {
104169 return null ;
105170 }
106171
107- if ( ! validator ?. key ) {
172+ if ( ! validator ?. key ) {
108173 return null ;
109174 }
110175
111- return asyncValidator ( validator , viewField , record , this . processService ) ;
176+ return asyncValidator ( validator , viewField , record , this . processService , this . confirmationModalService ) ;
112177 }
113178}
0 commit comments