LP#1855780: add option to include environment in clone of an event
authorGalen Charlton <gmc@equinoxOLI.org>
Mon, 8 Nov 2021 17:28:19 +0000 (12:28 -0500)
committerJane Sandberg <sandbergja@gmail.com>
Wed, 17 Nov 2021 19:55:11 +0000 (11:55 -0800)
This recovers a patch from Mike Risher that got dropped
when attempting to get a clean rebase from a branch merge.

Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Signed-off-by: Jane Sandberg <sandbergja@gmail.com>
Open-ILS/src/eg2/src/app/staff/admin/local/triggers/triggers.component.html
Open-ILS/src/eg2/src/app/staff/admin/local/triggers/triggers.component.ts

index c578368..43c3f04 100644 (file)
         </ng-template>
       </li>
   </ul>
-  
 <div [ngbNavOutlet]="triggerNav" class="mt-2"></div>
-  
+
+<eg-confirm-dialog #confirmDialog
+  i18n-dialogTitle i18n-dialogBody
+  dialogTitle="Alert"
+  dialogBody="Clone event definition environment as well?">
+</eg-confirm-dialog>
+
 <eg-string #createSuccessString i18n-text text="New entry Added"></eg-string>
 <eg-string #createErrString i18n-text text="Failed to create new entry"></eg-string>
 <eg-string #deleteFailedString i18n-text text="Deletion of entry failed or was not allowed">
index a54424a..84acf4e 100644 (file)
@@ -6,6 +6,7 @@ import {Router} from '@angular/router';
 import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {PcrudService} from '@eg/core/pcrud.service';
 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
+import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
 import {StringComponent} from '@eg/share/string/string.component';
 import {ToastService} from '@eg/share/toast/toast.service';
 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
@@ -28,6 +29,8 @@ export class TriggersComponent implements OnInit {
     @ViewChild('reactorDialog', {static: false}) reactorDialog: FmRecordEditorComponent;
     @ViewChild('validatorDialog', {static: false}) validatorDialog: FmRecordEditorComponent;
 
+    @ViewChild('confirmDialog', {static: false}) private confirmDialog: ConfirmDialogComponent;
+
     @ViewChild('eventsGrid', {static: false}) eventsGrid: GridComponent;
     @ViewChild('hooksGrid', {static: false}) hooksGrid: GridComponent;
     @ViewChild('reactorsGrid', {static: false}) reactorsGrid: GridComponent;
@@ -228,17 +231,40 @@ export class TriggersComponent implements OnInit {
         );
     }
 
-    cloneSelected = (idlThings: IdlObject[]) => {
-        const clone = this.idl.clone(idlThings[0]);
-        clone.id(null);
+    cloneSelected = (selectedRecords: IdlObject[]) => {
+        const clone = this.idl.clone(selectedRecords[0]);
+        // look for existing environments
+        this.pcrud.search('atenv', {event_def: selectedRecords[0].id()}, {}, {atomic: true})
+            .toPromise().then(envs => {
+            if (envs) {
+                // if environments found, ask user if they want to clone them
+                this.confirmDialog.open().toPromise().then(ok => {
+                    if (ok) {
+                        this.doClone(clone, envs);
+                    } else {
+                        this.doClone(clone, []);
+                    }
+                });
+            } else {
+                this.doClone(clone, []);
+            }
+        });
+    }
+
+    doClone(eventDef, env_list) {
+        eventDef.id(null);
         this.eventDialog.mode = 'create';
         this.eventDialog.recordId = null;
-        this.eventDialog.record = clone;
+        this.eventDialog.record = eventDef;
         this.eventDialog.open({size: 'lg'}).subscribe(
-            ok => {
+            response => {
                 this.cloneSuccessString.current()
                     .then(str => this.toast.success(str));
                 this.eventsGrid.reload();
+                // clone environments also if user previously confirmed
+                if (env_list.length) {
+                    this.cloneEnvs(response.id(), env_list);
+                }
             },
             rejection => {
                 if (!rejection.dismissed) {
@@ -248,4 +274,20 @@ export class TriggersComponent implements OnInit {
             }
         );
     }
+
+    cloneEnvs(cloneId, env_list) {
+        env_list.forEach(env => {
+            env.event_def(cloneId);
+            env.id(null);
+        });
+        this.pcrud.create(env_list).toPromise().then(
+            ok => {
+                console.debug(ok);
+            },
+            err => {
+                console.debug(err);
+            }
+        );
+    }
+
 }