Skip to content

Commit

Permalink
[Mappings editor] Clean up dynamic_templates code (#54950)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Jan 16, 2020
1 parent ca91ec5 commit 46568c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ interface ConfigurationFormState extends OnFormUpdateArg<MappingsConfiguration>
submitForm?: FormHook<MappingsConfiguration>['submit'];
}

interface TemplatesFormState extends OnFormUpdateArg<MappingsTemplates> {
defaultValue: MappingsTemplates;
submitForm?: FormHook<MappingsTemplates>['submit'];
}

export interface State {
isValid: boolean | undefined;
configuration: ConfigurationFormState;
Expand All @@ -72,12 +77,7 @@ export interface State {
term: string;
result: SearchResult[];
};
templates: {
defaultValue: {
dynamic_templates: MappingsTemplates['dynamic_templates'];
};
form?: FormHook<MappingsTemplates>;
} & OnFormUpdateArg<MappingsTemplates>;
templates: TemplatesFormState;
}

export type Action =
Expand Down

0 comments on commit 46568c2

Please sign in to comment.