Преглед изворни кода

Lots of progress on account management

Torkel Ödegaard пре 11 година
родитељ
комит
5dfeddf583
3 измењених фајлова са 107 додато и 22 уклоњено
  1. 38 18
      src/app/controllers/pro/accountCtrl.js
  2. 22 4
      src/app/partials/pro/account.html
  3. 47 0
      src/app/services/pro/backendSrv.js

+ 38 - 18
src/app/controllers/pro/accountCtrl.js

@@ -1,43 +1,63 @@
 define([
 define([
   'angular',
   'angular',
+  'lodash',
+  'services/pro/backendSrv',
 ],
 ],
-function (angular) {
+function (angular, _) {
   'use strict';
   'use strict';
 
 
   var module = angular.module('grafana.controllers');
   var module = angular.module('grafana.controllers');
 
 
-  module.controller('AccountCtrl', function($scope, $http, alertSrv) {
+  module.controller('AccountCtrl', function($scope, $http, backendSrv) {
 
 
     $scope.collaborator = {};
     $scope.collaborator = {};
 
 
     $scope.init = function() {
     $scope.init = function() {
-      $scope.getAccountInfo();
+      $scope.getAccount();
+      $scope.getOtherAccounts();
     };
     };
 
 
-    $scope.getAccountInfo = function() {
-      $http.get('/api/account').then(function(result) {
-        $scope.account = result.data;
-        console.log("value", result.data);
-      }, function(err) {
+    $scope.getAccount = function() {
+      backendSrv.get('/api/account/').then(function(account) {
+        $scope.account = account;
+        $scope.collaborators = account.collaborators;
+      });
+    };
 
 
+    $scope.getOtherAccounts = function() {
+      backendSrv.get('/api/account/others').then(function(otherAccounts) {
+        $scope.otherAccounts = otherAccounts;
       });
       });
     };
     };
 
 
+    $scope.setUsingAccount = function(otherAccount) {
+      backendSrv.request({
+        method: 'POST',
+        url: '/api/account/using/' + otherAccount.id,
+        desc: 'Change active account',
+      }).then($scope.getOtherAccounts);
+    };
+
+    $scope.removeCollaborator = function(collaborator) {
+      backendSrv.request({
+        method: 'POST',
+        url: '/api/account/collaborators/remove',
+        data: { accountId: collaborator.accountId },
+        desc: 'Remove collaborator',
+      }).then($scope.getAccount);
+    };
+
     $scope.addCollaborator = function() {
     $scope.addCollaborator = function() {
       if (!$scope.addCollaboratorForm.$valid) {
       if (!$scope.addCollaboratorForm.$valid) {
         return;
         return;
       }
       }
 
 
-      $http.post('/api/account/collaborators/add', $scope.collaborator).then(function() {
-        alertSrv.set('Collaborator added', '', 'success', 3000);
-      }, function(err) {
-        if (err.data && err.data.status) {
-          alertSrv.set('Could not add collaborator', err.data.status, 'warning', 10000);
-        }
-        else if (err.statusText) {
-          alertSrv.set('Could not add collaborator', err.data.status, 'warning', 10000);
-        }
-      });
+      backendSrv.request({
+        method: 'POST',
+        url: '/api/account/collaborators/add',
+        data: $scope.collaborator,
+        desc: 'Add collaborator'
+      }).then($scope.getAccount);
     };
     };
 
 
     $scope.init();
     $scope.init();

+ 22 - 4
src/app/partials/pro/account.html

@@ -51,18 +51,28 @@
 			</div>
 			</div>
 
 
 		</div>
 		</div>
-		<div class="section">
 
 
+		<div class="section">
 			<div class="dashboard-editor-header">
 			<div class="dashboard-editor-header">
 				<div class="dashboard-editor-title">
 				<div class="dashboard-editor-title">
 					<i class="icon icon-eye-open"></i>
 					<i class="icon icon-eye-open"></i>
 					Active account
 					Active account
 				</div>
 				</div>
 			</div>
 			</div>
+			<br>
 
 
 			<table class="grafana-options-table">
 			<table class="grafana-options-table">
-				<tr ng-repeat="account in accounts">
-					<td>{{Name}}<td>
+				<tr ng-repeat="other in otherAccounts">
+					<td>name: {{other.name}}</td>
+					<td>role: {{other.role}}</td>
+					<td ng-show="other.isUsing">
+						currently using this account
+					</td>
+					<td ng-show="!other.isUsing">
+						<a ng-click="setUsingAccount(other)" class="btn btn-success btn-mini">
+							Select
+						</a>
+					</td>
 				</tr>
 				</tr>
 			</table>
 			</table>
 
 
@@ -94,7 +104,15 @@
 		<div class="editor-row row">
 		<div class="editor-row row">
 			<table class="grafana-options-table span5">
 			<table class="grafana-options-table span5">
 				<tr ng-repeat="collaborator in account.collaborators">
 				<tr ng-repeat="collaborator in account.collaborators">
-					<td>{{collaborator.accountId}}<td>
+					<td>{{collaborator.email}}</td>
+					<td>
+						{{collaborator.role}}
+					</td>
+					<td style="width: 1%">
+						<a ng-click="removeCollaborator(collaborator)" class="btn btn-danger btn-mini">
+							<i class="icon-remove"></i>
+						</a>
+					</td>
 				</tr>
 				</tr>
 			</table>
 			</table>
 		</div>
 		</div>

+ 47 - 0
src/app/services/pro/backendSrv.js

@@ -0,0 +1,47 @@
+define([
+  'angular',
+  'lodash',
+],
+function (angular, _) {
+  'use strict';
+
+  var module = angular.module('grafana.services');
+
+  module.service('backendSrv', function($http, alertSrv) {
+
+    this.get = function(url) {
+      return this.request({ method: 'GET', url: url });
+    };
+
+    this.request = function(options) {
+      var httpOptions = {
+        url: options.url,
+        method: options.method,
+        data: options.data
+      };
+
+      return $http(httpOptions).then(function(results) {
+        if (options.method !== 'GET') {
+          alertSrv.set(options.desc + ' OK ', '', 'success', 3000);
+        }
+        return results.data;
+      }, function(err) {
+        var data = err.data || { message: 'Unexpected error' };
+
+        if (_.isString(data)) {
+          data = { message: data };
+        }
+
+        data.severity = 'error';
+
+        if (err.status < 500) {
+          data.severity = "warning";
+        }
+
+        alertSrv.set(options.desc + ' failed', data.message, data.severity, 10000);
+      });
+    };
+
+
+  });
+});