From f51c09157ff0199a7782c4f4974e2146e5fde77f Mon Sep 17 00:00:00 2001 From: Jason Stephenson Date: Fri, 15 Jul 2022 13:34:18 -0400 Subject: [PATCH] Revert "LP#1851884: eg-fm-record-editor: avoid fetching all rows from linked table" This reverts commit f2824b8457fa1e941053cdc7fa715ab21e6f58c9. It causes the following error when building Angular: ERROR in src/app/share/fm-editor/fm-editor.component.ts:517:30 - error TS2339: Property 'linkedSearchConditions' does not exist on type 'FmFieldOptions'. 517 if (fieldOptions.linkedSearchConditions) { ~~~~~~~~~~~~~~~~~~~~~~ src/app/share/fm-editor/fm-editor.component.ts:518:51 - error TS2339: Property 'linkedSearchConditions' does not exist on type 'FmFieldOptions'. 518 field.idlBaseQuery = fieldOptions.linkedSearchConditions; There does not appear to be any way to resolve this as the patch also relies on other changes in rel_3_8 and rel_3_9 that are not present in rel_3_7. For these reasons, I am reverting the patch from rel_3_7. Signed-off-by: Jason Stephenson --- Open-ILS/src/eg2/src/app/core/idl.service.ts | 11 +-- .../app/share/fm-editor/fm-editor.component.html | 13 ---- .../src/app/share/fm-editor/fm-editor.component.ts | 86 ++++++++++++++++++---- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/core/idl.service.ts b/Open-ILS/src/eg2/src/app/core/idl.service.ts index 3741e930e3..662c73586b 100644 --- a/Open-ILS/src/eg2/src/app/core/idl.service.ts +++ b/Open-ILS/src/eg2/src/app/core/idl.service.ts @@ -161,9 +161,7 @@ export class IdlService { } // Return the selector field for the class. If no selector is - // defined, use 'name' if it exists as a field on the class. As - // a last ditch fallback, if there's no selector but the primary - // key is a text field, use that. + // defined, use 'name' if it exists as a field on the class. getClassSelector(idlClass: string): string { if (idlClass) { @@ -175,13 +173,6 @@ export class IdlService { // No selector defined in the IDL, try 'name'. if ('name' in classDef.field_map) { return 'name'; } - - // last ditch - if the primary key is a text field, - // treat it as the selector - if (classDef.field_map[classDef.pkey].datatype === 'text') { - return classDef.pkey; - } - } } diff --git a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html index ab5f36ef94..af9f5f0197 100644 --- a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html +++ b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html @@ -159,19 +159,6 @@ - - - - - field.helpTextValue = help); @@ -535,6 +533,70 @@ export class FmRecordEditorComponent return promise || Promise.resolve(); } + wireUpCombobox(field: any): Promise { + + const fieldOptions = this.fieldOptions[field.name] || {}; + + // globally preloading unless a field-specific value is set. + if (this.preloadLinkedValues) { + if (!('preloadLinkedValues' in fieldOptions)) { + fieldOptions.preloadLinkedValues = true; + } + } + + const selector = fieldOptions.linkedSearchField || + this.idl.getClassSelector(field.class); + + if (!selector && !fieldOptions.preloadLinkedValues) { + // User probably expects an async data source, but we can't + // provide one without a selector. Warn the user. + console.warn(`Class ${field.class} has no selector. + Pre-fetching all rows for combobox`); + } + + if (fieldOptions.preloadLinkedValues || !selector) { + return this.pcrud.retrieveAll(field.class, {}, {atomic : true}) + .toPromise().then(list => { + field.linkedValues = + this.flattenLinkedValues(field, list); + }); + } + + // If we have a selector, wire up for async data retrieval + field.linkedValuesSource = + (term: string): Observable => { + + const search = {}; + const orderBy = {order_by: {}}; + const idField = this.idl.classes[field.class].pkey || 'id'; + + search[selector] = {'ilike': `%${term}%`}; + orderBy.order_by[field.class] = selector; + + return this.pcrud.search(field.class, search, orderBy) + .pipe(map(idlThing => + // Map each object into a ComboboxEntry upon arrival + this.flattenLinkedValues(field, [idlThing])[0] + )); + }; + + // Using an async data source, but a value is already set + // on the field. Fetch the linked object and add it to the + // combobox entry list so it will be avilable for display + // at dialog load time. + const linkVal = this.record[field.name](); + if (linkVal !== null && linkVal !== undefined) { + return this.pcrud.retrieve(field.class, linkVal).toPromise() + .then(idlThing => { + field.linkedValues = + this.flattenLinkedValues(field, Array(idlThing)); + }); + } + + // No linked value applied, nothing to pre-fetch. + return Promise.resolve(); + } + // Returns a context object to be inserted into a custom // field template. customTemplateFieldContext(fieldDef: any): CustomFieldContext { @@ -645,11 +707,7 @@ export class FmRecordEditorComponent return field.datatype; } - if (field.datatype === 'link') { - return 'link'; - } - - if (field.linkedValues) { + if (field.datatype === 'link' || field.linkedValues) { return 'list'; } -- 2.11.0