|
3 | 3 | * or more contributor license agreements. Licensed under the Elastic License; |
4 | 4 | * you may not use this file except in compliance with the Elastic License. |
5 | 5 | */ |
6 | | -import React, { useState, useEffect } from 'react'; |
| 6 | +import React, { useState, useEffect, useMemo, useCallback } from 'react'; |
7 | 7 | import { useRouteMatch, useHistory } from 'react-router-dom'; |
8 | 8 | import { i18n } from '@kbn/i18n'; |
9 | 9 | import { FormattedMessage } from '@kbn/i18n/react'; |
@@ -95,32 +95,46 @@ export const CreateDatasourcePage: React.FunctionComponent = () => { |
95 | 95 | // Datasource validation state |
96 | 96 | const [validationResults, setValidationResults] = useState<DatasourceValidationResults>(); |
97 | 97 |
|
| 98 | + // Form state |
| 99 | + const [formState, setFormState] = useState<DatasourceFormState>('INVALID'); |
| 100 | + |
98 | 101 | // Update package info method |
99 | | - const updatePackageInfo = (updatedPackageInfo: PackageInfo | undefined) => { |
100 | | - if (updatedPackageInfo) { |
101 | | - setPackageInfo(updatedPackageInfo); |
102 | | - setFormState('VALID'); |
103 | | - } else { |
104 | | - setFormState('INVALID'); |
105 | | - setPackageInfo(undefined); |
106 | | - } |
| 102 | + const updatePackageInfo = useCallback( |
| 103 | + (updatedPackageInfo: PackageInfo | undefined) => { |
| 104 | + if (updatedPackageInfo) { |
| 105 | + setPackageInfo(updatedPackageInfo); |
| 106 | + if (agentConfig) { |
| 107 | + setFormState('VALID'); |
| 108 | + } |
| 109 | + } else { |
| 110 | + setFormState('INVALID'); |
| 111 | + setPackageInfo(undefined); |
| 112 | + } |
107 | 113 |
|
108 | | - // eslint-disable-next-line no-console |
109 | | - console.debug('Package info updated', updatedPackageInfo); |
110 | | - }; |
| 114 | + // eslint-disable-next-line no-console |
| 115 | + console.debug('Package info updated', updatedPackageInfo); |
| 116 | + }, |
| 117 | + [agentConfig, setPackageInfo, setFormState] |
| 118 | + ); |
111 | 119 |
|
112 | 120 | // Update agent config method |
113 | | - const updateAgentConfig = (updatedAgentConfig: AgentConfig | undefined) => { |
114 | | - if (updatedAgentConfig) { |
115 | | - setAgentConfig(updatedAgentConfig); |
116 | | - } else { |
117 | | - setFormState('INVALID'); |
118 | | - setAgentConfig(undefined); |
119 | | - } |
| 121 | + const updateAgentConfig = useCallback( |
| 122 | + (updatedAgentConfig: AgentConfig | undefined) => { |
| 123 | + if (updatedAgentConfig) { |
| 124 | + setAgentConfig(updatedAgentConfig); |
| 125 | + if (packageInfo) { |
| 126 | + setFormState('VALID'); |
| 127 | + } |
| 128 | + } else { |
| 129 | + setFormState('INVALID'); |
| 130 | + setAgentConfig(undefined); |
| 131 | + } |
120 | 132 |
|
121 | | - // eslint-disable-next-line no-console |
122 | | - console.debug('Agent config updated', updatedAgentConfig); |
123 | | - }; |
| 133 | + // eslint-disable-next-line no-console |
| 134 | + console.debug('Agent config updated', updatedAgentConfig); |
| 135 | + }, |
| 136 | + [packageInfo, setAgentConfig, setFormState] |
| 137 | + ); |
124 | 138 |
|
125 | 139 | const hasErrors = validationResults ? validationHasErrors(validationResults) : false; |
126 | 140 |
|
@@ -163,7 +177,6 @@ export const CreateDatasourcePage: React.FunctionComponent = () => { |
163 | 177 | : getHref('integration_details', { pkgkey }); |
164 | 178 |
|
165 | 179 | // Save datasource |
166 | | - const [formState, setFormState] = useState<DatasourceFormState>('INVALID'); |
167 | 180 | const saveDatasource = async () => { |
168 | 181 | setFormState('LOADING'); |
169 | 182 | const result = await sendCreateDatasource(datasource); |
@@ -215,33 +228,43 @@ export const CreateDatasourcePage: React.FunctionComponent = () => { |
215 | 228 | packageInfo, |
216 | 229 | }; |
217 | 230 |
|
| 231 | + const stepSelectConfig = useMemo( |
| 232 | + () => ( |
| 233 | + <StepSelectConfig |
| 234 | + pkgkey={pkgkey} |
| 235 | + updatePackageInfo={updatePackageInfo} |
| 236 | + agentConfig={agentConfig} |
| 237 | + updateAgentConfig={updateAgentConfig} |
| 238 | + /> |
| 239 | + ), |
| 240 | + [pkgkey, updatePackageInfo, agentConfig, updateAgentConfig] |
| 241 | + ); |
| 242 | + |
| 243 | + const stepSelectPackage = useMemo( |
| 244 | + () => ( |
| 245 | + <StepSelectPackage |
| 246 | + agentConfigId={configId} |
| 247 | + updateAgentConfig={updateAgentConfig} |
| 248 | + packageInfo={packageInfo} |
| 249 | + updatePackageInfo={updatePackageInfo} |
| 250 | + /> |
| 251 | + ), |
| 252 | + [configId, updateAgentConfig, packageInfo, updatePackageInfo] |
| 253 | + ); |
| 254 | + |
218 | 255 | const steps: EuiStepProps[] = [ |
219 | 256 | from === 'package' |
220 | 257 | ? { |
221 | 258 | title: i18n.translate('xpack.ingestManager.createDatasource.stepSelectAgentConfigTitle', { |
222 | 259 | defaultMessage: 'Select an agent configuration', |
223 | 260 | }), |
224 | | - children: ( |
225 | | - <StepSelectConfig |
226 | | - pkgkey={pkgkey} |
227 | | - updatePackageInfo={updatePackageInfo} |
228 | | - agentConfig={agentConfig} |
229 | | - updateAgentConfig={updateAgentConfig} |
230 | | - /> |
231 | | - ), |
| 261 | + children: stepSelectConfig, |
232 | 262 | } |
233 | 263 | : { |
234 | 264 | title: i18n.translate('xpack.ingestManager.createDatasource.stepSelectPackageTitle', { |
235 | 265 | defaultMessage: 'Select an integration', |
236 | 266 | }), |
237 | | - children: ( |
238 | | - <StepSelectPackage |
239 | | - agentConfigId={configId} |
240 | | - updateAgentConfig={updateAgentConfig} |
241 | | - packageInfo={packageInfo} |
242 | | - updatePackageInfo={updatePackageInfo} |
243 | | - /> |
244 | | - ), |
| 267 | + children: stepSelectPackage, |
245 | 268 | }, |
246 | 269 | { |
247 | 270 | title: i18n.translate('xpack.ingestManager.createDatasource.stepDefineDatasourceTitle', { |
|
0 commit comments