From 46568c22133535b8136ef61789220a6469f16daf Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Thu, 16 Jan 2020 11:53:43 -0500 Subject: [PATCH] [Mappings editor] Clean up dynamic_templates code (#54950) --- .../components/templates_form/templates_form.tsx | 7 +++++-- .../components/mappings_editor/mappings_editor.tsx | 2 +- .../components/mappings_editor/mappings_state.tsx | 7 +++++-- .../public/app/components/mappings_editor/reducer.ts | 12 ++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/components/templates_form/templates_form.tsx b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/components/templates_form/templates_form.tsx index 0aa6a90039a86..471217108ba6f 100644 --- a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/components/templates_form/templates_form.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/components/templates_form/templates_form.tsx @@ -62,8 +62,11 @@ export const TemplatesForm = React.memo(({ defaultValue }: Props) => { const dispatch = useDispatch(); useEffect(() => { - const subscription = form.subscribe(updatedTemplates => { - dispatch({ type: 'templates.update', value: { ...updatedTemplates, form } }); + const subscription = form.subscribe(({ data, isValid, validate }) => { + dispatch({ + type: 'templates.update', + value: { data, isValid, validate, submitForm: form.submit }, + }); }); return subscription.unsubscribe; }, [form]); diff --git a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_editor.tsx b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_editor.tsx index d1fee4c0af745..e3fdf42d889e9 100644 --- a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_editor.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_editor.tsx @@ -68,7 +68,7 @@ export const MappingsEditor = React.memo(({ onUpdate, defaultValue, indexSetting return; } } else if (selectedTab === 'templates') { - const { isValid: isTemplatesFormValid } = await state.templates.form!.submit(); + const { isValid: isTemplatesFormValid } = await state.templates.submitForm!(); if (!isTemplatesFormValid) { return; diff --git a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_state.tsx b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_state.tsx index 54cdea9ff8a42..65a1aa2858d14 100644 --- a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_state.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/mappings_state.tsx @@ -149,8 +149,11 @@ export const MappingsState = React.memo(({ children, onUpdate, defaultValue }: P : Promise.resolve(true); const templatesFormValidator = - state.templates.form !== undefined - ? (await state.templates.form!.submit()).isValid + state.templates.submitForm !== undefined + ? new Promise(async resolve => { + const { isValid } = await state.templates.submitForm!(); + resolve(isValid); + }) : Promise.resolve(true); const promisesToValidate = [configurationFormValidator, templatesFormValidator]; diff --git a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/reducer.ts b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/reducer.ts index e843f4e841631..26d5b8e1edfa5 100644 --- a/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/reducer.ts +++ b/x-pack/legacy/plugins/index_management/public/app/components/mappings_editor/reducer.ts @@ -58,6 +58,11 @@ interface ConfigurationFormState extends OnFormUpdateArg submitForm?: FormHook['submit']; } +interface TemplatesFormState extends OnFormUpdateArg { + defaultValue: MappingsTemplates; + submitForm?: FormHook['submit']; +} + export interface State { isValid: boolean | undefined; configuration: ConfigurationFormState; @@ -72,12 +77,7 @@ export interface State { term: string; result: SearchResult[]; }; - templates: { - defaultValue: { - dynamic_templates: MappingsTemplates['dynamic_templates']; - }; - form?: FormHook; - } & OnFormUpdateArg; + templates: TemplatesFormState; } export type Action =