Просмотр исходного кода

Fill wizard data current value with default value

When using the wizard, after the source or destination schema is loaded,
or after the source or destination options are loaded, set the current
value of the fields to their corresponding default values, if those
fields don't already have a value.

In the case of fields with enums, don't set their value to the default
value if the default value can't be found in the list.
Sergiu Miclea 6 лет назад
Родитель
Сommit
3aa32a5051
2 измененных файлов с 58 добавлено и 11 удалено
  1. 23 11
      src/components/pages/WizardPage/WizardPage.jsx
  2. 35 0
      src/stores/WizardStore.js

+ 23 - 11
src/components/pages/WizardPage/WizardPage.jsx

@@ -220,12 +220,15 @@ class WizardPage extends React.Component<Props, State> {
       optionsType: 'source',
       optionsType: 'source',
       useCache: true,
       useCache: true,
     })
     })
-    source && providerStore.getOptionsValues({
+    wizardStore.fillWithDefaultValues('source', providerStore.sourceSchema)
+
+    await providerStore.getOptionsValues({
       optionsType: 'source',
       optionsType: 'source',
       endpointId: source.id,
       endpointId: source.id,
       providerName: source.type,
       providerName: source.type,
       useCache: true,
       useCache: true,
     })
     })
+    wizardStore.fillWithDefaultValues('source', providerStore.sourceSchema)
   }
   }
 
 
   async handleTargetEndpointChange(target: EndpointType) {
   async handleTargetEndpointChange(target: EndpointType) {
@@ -241,13 +244,15 @@ class WizardPage extends React.Component<Props, State> {
       optionsType: 'destination',
       optionsType: 'destination',
       useCache: true,
       useCache: true,
     })
     })
+    wizardStore.fillWithDefaultValues('destination', providerStore.destinationSchema)
     // Preload destination options values
     // Preload destination options values
-    providerStore.getOptionsValues({
+    await providerStore.getOptionsValues({
       optionsType: 'destination',
       optionsType: 'destination',
       endpointId: target.id,
       endpointId: target.id,
       providerName: target.type,
       providerName: target.type,
       useCache: true,
       useCache: true,
     })
     })
+    wizardStore.fillWithDefaultValues('destination', providerStore.destinationSchema)
   }
   }
 
 
   handleAddEndpoint(newEndpointType: string, newEndpointFromSource: boolean) {
   handleAddEndpoint(newEndpointType: string, newEndpointFromSource: boolean) {
@@ -358,11 +363,16 @@ class WizardPage extends React.Component<Props, State> {
       providerName: endpoint.type,
       providerName: endpoint.type,
       optionsType,
       optionsType,
     })
     })
+    let getSchema = () => optionsType === 'source' ? providerStore.sourceSchema : providerStore.destinationSchema
+    wizardStore.fillWithDefaultValues(optionsType, getSchema())
+
     await providerStore.getOptionsValues({
     await providerStore.getOptionsValues({
       optionsType,
       optionsType,
       endpointId: endpoint.id,
       endpointId: endpoint.id,
       providerName: endpoint.type,
       providerName: endpoint.type,
     })
     })
+    wizardStore.fillWithDefaultValues(optionsType, getSchema())
+
     await this.loadExtraOptions(undefined, optionsType, false)
     await this.loadExtraOptions(undefined, optionsType, false)
   }
   }
 
 
@@ -374,14 +384,15 @@ class WizardPage extends React.Component<Props, State> {
     }
     }
   }
   }
 
 
-  loadExtraOptions(field?: Field, type: 'source' | 'destination', useCache: boolean = true) {
+  async loadExtraOptions(field?: Field, type: 'source' | 'destination', useCache: boolean = true) {
     let endpoint = type === 'source' ? wizardStore.data.source : wizardStore.data.target
     let endpoint = type === 'source' ? wizardStore.data.source : wizardStore.data.target
     if (!endpoint) {
     if (!endpoint) {
       return
       return
     }
     }
+    const getSchema = () => type === 'source' ? providerStore.sourceSchema : providerStore.destinationSchema
     let envData = getFieldChangeOptions({
     let envData = getFieldChangeOptions({
       providerName: endpoint.type,
       providerName: endpoint.type,
-      schema: type === 'source' ? providerStore.sourceSchema : providerStore.destinationSchema,
+      schema: getSchema(),
       data: type === 'source' ? wizardStore.data.sourceOptions : wizardStore.data.destOptions,
       data: type === 'source' ? wizardStore.data.sourceOptions : wizardStore.data.destOptions,
       field,
       field,
       type,
       type,
@@ -389,26 +400,25 @@ class WizardPage extends React.Component<Props, State> {
     if (!envData) {
     if (!envData) {
       return
       return
     }
     }
-    providerStore.getOptionsValues({
+    await providerStore.getOptionsValues({
       optionsType: type,
       optionsType: type,
       endpointId: endpoint.id,
       endpointId: endpoint.id,
       providerName: endpoint.type,
       providerName: endpoint.type,
       envData,
       envData,
       useCache,
       useCache,
     })
     })
+    wizardStore.fillWithDefaultValues(type, getSchema())
   }
   }
 
 
   async loadDataForPage(page: WizardPageType) {
   async loadDataForPage(page: WizardPageType) {
     const loadOptions = async (endpoint: EndpointType, optionsType: 'source' | 'destination') => {
     const loadOptions = async (endpoint: EndpointType, optionsType: 'source' | 'destination') => {
-      let schema = optionsType === 'source' ? providerStore.sourceSchema : providerStore.destinationSchema
-      if (schema.length > 0) {
-        return
-      }
       await providerStore.loadOptionsSchema({
       await providerStore.loadOptionsSchema({
         providerName: endpoint.type,
         providerName: endpoint.type,
         optionsType,
         optionsType,
         useCache: true,
         useCache: true,
       })
       })
+      let getSchema = () => optionsType === 'source' ? providerStore.sourceSchema : providerStore.destinationSchema
+      wizardStore.fillWithDefaultValues(optionsType, getSchema())
 
 
       await providerStore.getOptionsValues({
       await providerStore.getOptionsValues({
         optionsType,
         optionsType,
@@ -416,6 +426,8 @@ class WizardPage extends React.Component<Props, State> {
         providerName: endpoint.type,
         providerName: endpoint.type,
         useCache: true,
         useCache: true,
       })
       })
+      wizardStore.fillWithDefaultValues(optionsType, getSchema())
+
       await this.loadExtraOptions(undefined, optionsType)
       await this.loadExtraOptions(undefined, optionsType)
     }
     }
 
 
@@ -548,11 +560,11 @@ class WizardPage extends React.Component<Props, State> {
     let state = this.state.nextButtonDisabled
     let state = this.state.nextButtonDisabled
 
 
     if (wizardStore.currentPage.id === 'dest-options') {
     if (wizardStore.currentPage.id === 'dest-options') {
-      return providerStore.destinationSchemaLoading || state
+      return providerStore.destinationSchemaLoading || providerStore.destinationOptionsPrimaryLoading || providerStore.destinationOptionsSecondaryLoading || state
     }
     }
 
 
     if (wizardStore.currentPage.id === 'source-options') {
     if (wizardStore.currentPage.id === 'source-options') {
-      return providerStore.sourceSchemaLoading || state
+      return providerStore.sourceSchemaLoading || providerStore.sourceOptionsPrimaryLoading || providerStore.sourceOptionsSecondaryLoading || state
     }
     }
 
 
     return state
     return state

+ 35 - 0
src/stores/WizardStore.js

@@ -68,6 +68,41 @@ class WizardStore {
     this.data = { ...this.data, ...data }
     this.data = { ...this.data, ...data }
   }
   }
 
 
+  @action fillWithDefaultValues(direction: 'source' | 'destination', schema: Field[]) {
+    let data: { [string]: any } = (direction === 'source' ? this.data.sourceOptions : this.data.destOptions) || {}
+
+    schema.forEach(field => {
+      if (data[field.name] !== undefined) {
+        return
+      }
+      let fieldDefault = field.default
+      if (fieldDefault == null) {
+        return
+      }
+      if (field.enum) {
+        let isDefaultInEnum = field.enum.find(enumItem => {
+          if (fieldDefault.id != null) {
+            return enumItem.id != null ? enumItem.id === fieldDefault.id : enumItem === fieldDefault.id
+          }
+          return enumItem.id != null ? (enumItem.id === fieldDefault || enumItem.name === fieldDefault) : enumItem === fieldDefault
+        })
+
+        // Don't use the default if it can't be found in the enum list.
+        if (isDefaultInEnum) {
+          data[field.name] = field.default
+        }
+      } else {
+        data[field.name] = field.default
+      }
+    })
+
+    if (direction === 'source') {
+      this.data.sourceOptions = data
+    } else {
+      this.data.destOptions = data
+    }
+  }
+
   @action toggleInstanceSelection(instance: Instance) {
   @action toggleInstanceSelection(instance: Instance) {
     if (!this.data.selectedInstances) {
     if (!this.data.selectedInstances) {
       this.data.selectedInstances = [instance]
       this.data.selectedInstances = [instance]