Jelajahi Sumber

Dynamically change the required fields if there's a switch radio in AddCloudConnection CORWEB-25

This allows the removal of 'Azure' manual validation and adds support for future connections validation which have a similar switch radio option.
smiclea 8 tahun lalu
induk
melakukan
f1df613245

+ 40 - 13
src/components/AddCloudConnection/AddCloudConnection.js

@@ -105,18 +105,6 @@ class AddCloudConnection extends Reflux.Component {
   handleSave() {
     let valid = true
     let requiredFields = this.state.requiredFields
-    // If Azure, validate fields manually
-    if (this.state.currentCloud.name == "azure") {
-      if (this.state.currentCloudData.login_type == "user_credentials") {
-        requiredFields = ["subscription_id", "username", "password"]
-      } else if (this.state.currentCloudData.login_type == "service_principal_credentials") {
-        requiredFields = ["subscription_id", "tenant_id", "client_id", "client_secret"]
-      } else {
-        valid = false
-        NotificationActions.notify("Please choose the login type", "error")
-      }
-      this.setState({ requiredFields: requiredFields })
-    }
 
     for (let i in this.state.currentCloudData) {
       if (requiredFields.indexOf(i) > -1 && !this.state.currentCloudData[i]) {
@@ -144,6 +132,11 @@ class AddCloudConnection extends Reflux.Component {
         }
 
         let field = this.state.currentCloud.endpoint.fields.find(function findByName(f) { return f.name == this }, key);
+
+        if (!field || !field.dataType) {
+          continue;
+        }
+
         // Convert datatype
         switch (field.dataType) {
           case 'boolean':
@@ -311,8 +304,9 @@ class AddCloudConnection extends Reflux.Component {
           break
         case 'switch-radio':
           field.options.forEach(option => {
-            if (option.default && typeof currentCloudData[field.name] == "undefined") {
+            if (option.default && !currentCloudData[field.name]){
               currentCloudData[field.name] = option.value
+              this.setRadioRequiredFields(field, option.value)
               this.setState({ currentCloudData: currentCloudData })
             }
           }, this)
@@ -346,6 +340,34 @@ class AddCloudConnection extends Reflux.Component {
     }
   }
 
+  /**
+   * Dinamically change the required fields affected by the current radio selection
+   * @param field 
+   * @param currentValue 
+   */
+  setRadioRequiredFields(field, currentValue) {
+    let requiredFields = this.state.requiredFields || [];
+
+    // Remove fields set by previous radio change
+    field.options.forEach(option => {
+      option.fields.forEach(field => {
+        requiredFields = requiredFields.filter(rf => rf !== field.name)
+      })
+    })
+
+    field.options.forEach(option => {
+      if (option.value === currentValue) {
+        option.fields.forEach(optionField => {
+          if (optionField.required) {
+            requiredFields.push(optionField.name);
+          }
+        })
+      }
+    })
+
+    this.setState({ requiredFields: requiredFields });
+  }
+
   /**
    * Handles cancel edit/add endpoint
    */
@@ -365,6 +387,11 @@ class AddCloudConnection extends Reflux.Component {
     } else {
       currentCloudData[field.name] = e.target.value
     }
+
+    if (field.type === 'switch-radio') {
+      this.setRadioRequiredFields(field, e.target.value)
+    }
+
     this.setState({ currentCloudData: currentCloudData })
   }
 

+ 2 - 1
src/stores/ConnectionsStore/ConnectionsStore.js

@@ -297,7 +297,8 @@ class ConnectionsStore extends Reflux.Store
           {
             label: "User Credentials",
             value: "user_credentials",
-            fields: this.processCloud(userCredentialFields)
+            fields: this.processCloud(userCredentialFields),
+            default: true
           },
           {
             label: "Service Principal Credentials",