فهرست منبع

Handle properly data types when saving endpoints

Alessandro Pilotti 8 سال پیش
والد
کامیت
262a2ed566
2فایلهای تغییر یافته به همراه44 افزوده شده و 9 حذف شده
  1. 36 5
      src/components/AddCloudConnection/AddCloudConnection.js
  2. 8 4
      src/stores/ConnectionsStore/ConnectionsStore.js

+ 36 - 5
src/components/AddCloudConnection/AddCloudConnection.js

@@ -138,6 +138,31 @@ class AddCloudConnection extends Reflux.Component {
         if (credentials[key].label) {
         if (credentials[key].label) {
           credentials[key] = credentials[key].value
           credentials[key] = credentials[key].value
         }
         }
+
+        let field = this.state.currentCloud.endpoint.fields.find(function findByName(f) { return f.name == this }, key);
+        // Convert datatype
+        switch (field.dataType) {
+          case 'boolean':
+            credentials[key] = (credentials[key] === true ||
+              ((typeof credentials[key] === 'string' || credentials[key] instanceof String) &&
+               credentials[key].toLowerCase() == "true"));
+            break;
+          case 'integer':
+            let value = parseInt(credentials[key], 10);
+            if (value.toString() != credentials[key]) {
+              valid = false;
+              NotificationActions.notify('"' + key + '" needs to be an integer', 'error');
+            }
+            credentials[key] = value;
+            break;
+          default:
+            // retain original value
+            break;
+        }
+      }
+
+      if (!valid) {
+        return;
       }
       }
 
 
       // If Azure, explicitly setting the fields right
       // If Azure, explicitly setting the fields right
@@ -228,7 +253,12 @@ class AddCloudConnection extends Reflux.Component {
     cloud.endpoint.fields.forEach(field => {
     cloud.endpoint.fields.forEach(field => {
       if (typeof currentCloudData[field.name] == "undefined") {
       if (typeof currentCloudData[field.name] == "undefined") {
         if (field.type == "dropdown") {
         if (field.type == "dropdown") {
-          currentCloudData[field.name] = field.options[0]
+          let defaultOption = field.options.find(function isDefaultOption(option) { return option.default; })
+          if (defaultOption) {
+            currentCloudData[field.name] = defaultOption.value;
+          } else {
+            currentCloudData[field.name] = null;
+          }
         } else {
         } else {
           currentCloudData[field.name] = ""
           currentCloudData[field.name] = ""
         }
         }
@@ -275,7 +305,7 @@ class AddCloudConnection extends Reflux.Component {
         case 'dropdown':
         case 'dropdown':
           field.options.forEach(option => {
           field.options.forEach(option => {
             if (option.default === true && typeof currentCloudData[field.name] == "undefined") {
             if (option.default === true && typeof currentCloudData[field.name] == "undefined") {
-              currentCloudData[field.name] = option
+              currentCloudData[field.name] = option.value
               this.setState({ currentCloudData: currentCloudData })
               this.setState({ currentCloudData: currentCloudData })
             }
             }
           }, this)
           }, this)
@@ -336,7 +366,7 @@ class AddCloudConnection extends Reflux.Component {
   handleCloudFieldChange(e, field) {
   handleCloudFieldChange(e, field) {
     let currentCloudData = this.state.currentCloudData
     let currentCloudData = this.state.currentCloudData
     if (field.type == 'dropdown') {
     if (field.type == 'dropdown') {
-      currentCloudData[field.name] = e
+      currentCloudData[field.name] = e.value
     } else {
     } else {
       currentCloudData[field.name] = e.target.value
       currentCloudData[field.name] = e.target.value
     }
     }
@@ -417,8 +447,9 @@ class AddCloudConnection extends Reflux.Component {
             <Dropdown
             <Dropdown
               options={field.options}
               options={field.options}
               onChange={(e) => this.handleCloudFieldChange(e, field)}
               onChange={(e) => this.handleCloudFieldChange(e, field)}
-              placeholder={field.label + (field.required ? " *" : "")}
-              value={this.state.currentCloudData[field.name]}
+              placeholder="Choose a value"
+              value={field.options.find(function findOption(option) { return option.value == this},
+                     this.state.currentCloudData[field.name])}
             />
             />
           </div>
           </div>
         )
         )

+ 8 - 4
src/stores/ConnectionsStore/ConnectionsStore.js

@@ -353,8 +353,9 @@ class ConnectionsStore extends Reflux.Store
           case "boolean":
           case "boolean":
             field.type = "dropdown"
             field.type = "dropdown"
             field.options = [
             field.options = [
-              {label: field.label + ": Yes", value: "true", default: cloudData.properties[propName].default == "true"},
-              {label: field.label + ": No", value: "false", default: cloudData.properties[propName].default == "false"}
+              // Values need to be strings, due to a limitation in react-dropdown
+              {label: "Yes", value: "true", default: cloudData.properties[propName].default == true},
+              {label: "No", value: "false", default: cloudData.properties[propName].default == false}
             ]
             ]
 
 
             break
             break
@@ -368,9 +369,10 @@ class ConnectionsStore extends Reflux.Store
               field.type = "dropdown"
               field.type = "dropdown"
               field.options = []
               field.options = []
               for (let i = cloudData.properties[propName].minimum; i <= cloudData.properties[propName].maximum; i++) {
               for (let i = cloudData.properties[propName].minimum; i <= cloudData.properties[propName].maximum; i++) {
+                // Values need to be strings, due to a limitation in react-dropdown
                 field.options.push({
                 field.options.push({
-                    label: field.label + ": " + i,
-                    value: i,
+                    label: i.toString(),
+                    value: i.toString(),
                     default: cloudData.properties[propName].default === i
                     default: cloudData.properties[propName].default === i
                   },
                   },
                 )
                 )
@@ -381,6 +383,8 @@ class ConnectionsStore extends Reflux.Store
             break
             break
         }
         }
 
 
+        field.dataType = cloudData.properties[propName].type;
+
         if (field.name == 'username') {
         if (field.name == 'username') {
           field.required = true
           field.required = true
           sortedFields[0] = field
           sortedFields[0] = field