@@ -5,7 +5,7 @@ import { isEqual as _isEqual } from 'lodash-es';
5
5
import { ActivatedRoute , Router } from '@angular/router' ;
6
6
7
7
import { LineageModelGeneratorService } from '@mm-services/lineage-model-generator.service' ;
8
- import { FormService } from '@mm-services/form.service' ;
8
+ import { FormService , DuplicatesFoundError , Duplicate } from '@mm-services/form.service' ;
9
9
import { EnketoFormContext } from '@mm-services/enketo.service' ;
10
10
import { ContactTypesService } from '@mm-services/contact-types.service' ;
11
11
import { DbService } from '@mm-services/db.service' ;
@@ -55,6 +55,18 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
55
55
private trackSave ;
56
56
private trackMetadata = { action : '' , form : '' } ;
57
57
58
+ private duplicateCheck ;
59
+ acknowledged = false ;
60
+ onAcknowledgeChange ( value : boolean ) {
61
+ this . acknowledged = value ;
62
+ }
63
+
64
+ onNavigateToDuplicate ( _id : string ) {
65
+ this . router . navigate ( [ '/contacts' , _id , 'edit' ] ) ;
66
+ }
67
+
68
+ duplicates : Duplicate [ ] = [ ] ;
69
+
58
70
ngOnInit ( ) {
59
71
this . trackRender = this . performanceService . track ( ) ;
60
72
this . subscribeToStore ( ) ;
@@ -153,6 +165,10 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
153
165
this . contentError = false ;
154
166
this . errorTranslationKey = false ;
155
167
168
+ // Reset when when navigated to duplicate
169
+ this . duplicates = [ ] ;
170
+ this . acknowledged = false ;
171
+
156
172
try {
157
173
const contact = await this . getContact ( ) ;
158
174
const contactTypeId = this . contactTypesService . getTypeId ( contact ) || this . routeSnapshot . params ?. type ;
@@ -272,6 +288,7 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
272
288
private async renderForm ( formId : string , titleKey : string ) {
273
289
const formDoc = await this . dbService . get ( ) . get ( formId ) ;
274
290
this . xmlVersion = formDoc . xmlVersion ;
291
+ this . duplicateCheck = formDoc . context ?. duplicate_check ;
275
292
276
293
this . globalActions . setEnketoEditedStatus ( false ) ;
277
294
@@ -326,7 +343,9 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
326
343
$ ( 'form.or' ) . trigger ( 'beforesave' ) ;
327
344
328
345
return this . formService
329
- . saveContact ( form , docId , this . enketoContact . type , this . xmlVersion )
346
+ . saveContact ( {
347
+ form, docId, type : this . enketoContact . type , xmlVersion : this . xmlVersion
348
+ } , this . duplicateCheck , this . acknowledged )
330
349
. then ( ( result ) => {
331
350
console . debug ( 'saved contact' , result ) ;
332
351
@@ -345,6 +364,11 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
345
364
this . router . navigate ( [ '/contacts' , result . docId ] ) ;
346
365
} )
347
366
. catch ( ( err ) => {
367
+ if ( err instanceof DuplicatesFoundError ) {
368
+ this . duplicates = err . duplicates ;
369
+ err = Error ( err . message ) ;
370
+ }
371
+
348
372
console . error ( 'Error submitting form data' , err ) ;
349
373
350
374
this . globalActions . setEnketoSavingStatus ( false ) ;
0 commit comments