From fb440e7dca5540a5609a1f60cf68e2f3cf652e86 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Tue, 6 Aug 2019 14:41:54 -0600 Subject: [PATCH] TS cleanup on new IndexPatterns implementation. (#42646) --- .../public/index_patterns/index_patterns.ts | 32 +++++++++++-------- .../step_details/step_details_form.tsx | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/legacy/ui/public/index_patterns/index_patterns.ts b/src/legacy/ui/public/index_patterns/index_patterns.ts index f1cbc45fd9ff1..9eac851596ef9 100644 --- a/src/legacy/ui/public/index_patterns/index_patterns.ts +++ b/src/legacy/ui/public/index_patterns/index_patterns.ts @@ -18,6 +18,7 @@ */ import { SavedObjectsClientContract, SimpleSavedObject, UiSettingsClient } from 'src/core/public'; +import { idx } from '@kbn/elastic-idx'; // @ts-ignore import { fieldFormats } from '../registry/field_formats'; @@ -33,7 +34,7 @@ export class IndexPatterns { private config: UiSettingsClient; private savedObjectsClient: SavedObjectsClientContract; - private savedObjectsCache?: Array> | null; + private savedObjectsCache?: Array>> | null; constructor(config: UiSettingsClient, savedObjectsClient: SavedObjectsClientContract) { this.config = config; @@ -48,35 +49,38 @@ export class IndexPatterns { })).savedObjects; } - getIds = async (refresh: boolean) => { + getIds = async (refresh: boolean = false) => { if (!this.savedObjectsCache || refresh) { await this.refreshSavedObjectsCache(); } - if (this.savedObjectsCache) { - return this.savedObjectsCache.map(obj => _.get(obj, 'id')); + if (!this.savedObjectsCache) { + return []; } + return this.savedObjectsCache.map(obj => idx(obj, _ => _.id)); }; - getTitles = async (refresh: boolean) => { + getTitles = async (refresh: boolean = false): Promise => { if (!this.savedObjectsCache || refresh) { await this.refreshSavedObjectsCache(); } - if (this.savedObjectsCache) { - return this.savedObjectsCache.map(obj => _.get(obj, 'attributes.title')); + if (!this.savedObjectsCache) { + return []; } + return this.savedObjectsCache.map(obj => idx(obj, _ => _.attributes.title)); }; - getFields = async (fields: string[], refresh: boolean) => { + getFields = async (fields: string[], refresh: boolean = false) => { if (!this.savedObjectsCache || refresh) { await this.refreshSavedObjectsCache(); } - if (this.savedObjectsCache) { - return this.savedObjectsCache.map(obj => { - const result: Record = {}; - fields.forEach(f => (result[f] = _.get(obj, f) || _.get(obj, `attributes.${f}`))); - return result; - }); + if (!this.savedObjectsCache) { + return []; } + return this.savedObjectsCache.map((obj: Record) => { + const result: Record = {}; + fields.forEach((f: string) => (result[f] = obj[f] || idx(obj, _ => _.attributes[f]))); + return result; + }); }; clearCache = (id?: string) => { diff --git a/x-pack/legacy/plugins/ml/public/data_frame/pages/data_frame_new_pivot/components/step_details/step_details_form.tsx b/x-pack/legacy/plugins/ml/public/data_frame/pages/data_frame_new_pivot/components/step_details/step_details_form.tsx index f5e97c43b6dda..19d243363a9a2 100644 --- a/x-pack/legacy/plugins/ml/public/data_frame/pages/data_frame_new_pivot/components/step_details/step_details_form.tsx +++ b/x-pack/legacy/plugins/ml/public/data_frame/pages/data_frame_new_pivot/components/step_details/step_details_form.tsx @@ -118,7 +118,7 @@ export const StepDetailsForm: SFC = React.memo(({ overrides = {}, onChang } try { - setIndexPatternTitles((await kibanaContext.indexPatterns.getTitles(false)) as string[]); + setIndexPatternTitles(await kibanaContext.indexPatterns.getTitles()); } catch (e) { toastNotifications.addDanger( i18n.translate('xpack.ml.dataframe.stepDetailsForm.errorGettingIndexPatternTitles', {