LP#1626157 fm editor xport
authorBill Erickson <berickxx@gmail.com>
Mon, 16 Apr 2018 21:24:52 +0000 (21:24 +0000)
committerBill Erickson <berickxx@gmail.com>
Mon, 16 Apr 2018 21:24:52 +0000 (21:24 +0000)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts

index ac45e3d..e08d2e1 100644 (file)
@@ -21,8 +21,11 @@ export class FmRecordEditorComponent
     //       'view' for viewing an existing record without editing
     @Input() mode: string;
 
-    // record ID to update.  Not all IDs are numbers.
-    @Input() recordId: string; 
+    // Record ID to view/update.  Value is dynamic.
+    recId: any;
+    @Input() set recordId(id: any) {
+        if (id) this.recId = id;
+    }
 
     // TODO
     // customFieldTemplates
@@ -65,6 +68,7 @@ export class FmRecordEditorComponent
     idlDef: any;
 
     // IDL record we are editing
+    // TODO: allow this to be provided by the caller?
     record: EgIdlObject;
 
     // Can we edit the primary key?
@@ -102,25 +106,24 @@ export class FmRecordEditorComponent
     private initRecord(): Promise<any> {
 
         if (this.mode == 'update' || this.mode == 'view') {
-            return this.pcrud.retrieve(this.idlClass, this.recordId)
+            return this.pcrud.retrieve(this.idlClass, this.recId)
             .toPromise().then(rec => {
 
                 if (!rec) {
-                    return Promise.reject(
-                        `No record found with id ${this.recordId}`);
+                    return Promise.reject(`No '${this.idlClass}' 
+                        record found with id ${this.recId}`);
                 }
 
                 this.record = rec;
                 this.convertDatatypesToJs();
-                return this.getFieldList().then(
-                    fields => this.fields = fields);
+                return this.applyFields();
             });
         } 
 
         // create a new record from scratch
         this.pkeyIsEditable = !('pkey_sequence' in this.idlDef);
         this.record = this.idl.create(this.idlClass);
-        return this.getFieldList().then(fields => this.fields = fields);
+        return this.applyFields();
     }
 
     // Modifies the FM record in place, replacing IDL-compatible values
@@ -141,21 +144,22 @@ export class FmRecordEditorComponent
         let idField = this.idl.classes[cls].pkey;
         let selector = 
             this.idl.classes[cls].field_map[idField].selector || idField;
+
         return list.map(item => {
             return {id: item[idField](), name: item[selector]()}
         });
     }
 
-    private getFieldList(): Promise<any[]> {
+    private applyFields(): Promise<any> {
 
-        let fields = this.idlDef.fields.filter(f => 
+        this.fields = this.idlDef.fields.filter(f => 
             f.virtual != 'true' && 
             !this.hiddenFields.includes(f.name)
         );
 
         let promises = [];
 
-        fields.forEach(field => {
+        this.fields.forEach(field => {
             field.readOnly = this.mode == 'view' 
                 || this.readonlyFields.includes(field.name);
 
@@ -185,7 +189,8 @@ export class FmRecordEditorComponent
             // TODO custom field templates
         });
 
-        return Promise.all(promises).then(ok => fields);
+        // Wait for all network calls to complete
+        return Promise.all(promises);
     }
 
     save() {