Sfoglia il codice sorgente

feat: prevent calling WS with empty params

Prevent the web-service to be called with zero valid arguments
jmacura 5 anni fa
parent
commit
bc86ceee43

+ 19 - 1
src/adjuster/adjuster.component.js

@@ -15,8 +15,26 @@ export const AdjusterComponent = {
     angular.extend($scope, {
       //HsCore,
       PraAdjusterService,
+      noDataSelected,
     });
 
-    $scope.$emit('scope_loaded', 'adjuster');
+    //$scope.$emit('scope_loaded', 'adjuster');
+
+    function noDataSelected() {
+      if (PraAdjusterService.factors.length === 0) {
+        return true;
+      }
+      let datasetsEffectivelyTurnedOn = [];
+      for (const factor of PraAdjusterService.factors) {
+        if (factor.weight === 0) {
+          continue;
+        }
+        datasetsEffectivelyTurnedOn = [
+          ...datasetsEffectivelyTurnedOn,
+          ...factor.datasets.filter((ds) => ds.included),
+        ];
+      }
+      return datasetsEffectivelyTurnedOn.length === 0;
+    }
   },
 };

+ 2 - 1
src/adjuster/adjuster.directive.html

@@ -3,7 +3,8 @@
   <div class="card-body">
     <div class="p-2 center-block">
       <button type="button" class="btn btn-primary" ng-click="PraAdjusterService.apply()"
-        ng-disabled="PraAdjusterService.isClusteringInProcess()">Calculate clusters</button>
+        ng-disabled="PraAdjusterService.isClusteringInProcess() || noDataSelected()">Calculate clusters</button>
+      <div class="text-warning pt-2" ng-show="noDataSelected()">Select at least one dataset and set at least one factor's weight to a non-zero value.</div>
     </div>
     <div ng-repeat="factor in PraAdjusterService.factors">
       <div class="d-flex flex-row">

+ 7 - 7
src/adjuster/adjuster.service.js

@@ -1,6 +1,6 @@
 // import attractivity from '../Attractivity.json';
 import nuts from '../nuts.js';
-import {factors} from './factors.js';
+//import {factors} from './factors.js';
 
 export class AdjusterService {
   constructor(HsCore, HsUtilsService, $rootScope, $http, $location) {
@@ -11,9 +11,9 @@ export class AdjusterService {
     this.$http = $http;
     this.serviceBaseUrl =
       $location.host() === 'localhost'
-        ? 'https://jmacura.ml/ws/'
+        ? 'https://jmacura.ml/ws/' // 'http://localhost:3000/'
         : 'https://publish.lesprojekt.cz/nodejs/';
-    this.factors = factors;
+    this.factors = [];
     this.clusters = [];
     this.method = 'haclust';
     this._clusteringInProcess = true;
@@ -28,19 +28,19 @@ export class AdjusterService {
     const f = () => {
       this._clusteringInProcess = true;
       this.$http({
-        method: 'get',
+        method: 'post',
         url: this.serviceBaseUrl + 'clusters',
-        /*data: {
+        data: {
           factors: this.factors.map((f) => {
             return {
-              factor: f.factor,
+              factor: f.name,
               weight: f.weight,
               datasets: f.datasets
                 .filter((ds) => ds.included)
                 .map((ds) => ds.name),
             };
           }),
-        },*/
+        },
       })
         .then((response) => {
           const clusterData = response.data.response;