Lots of progress on account management

This commit is contained in:
Torkel Ödegaard 2014-09-21 15:02:06 +02:00
parent e0dc530e94
commit 5dfeddf583
3 changed files with 108 additions and 23 deletions

View File

@ -1,43 +1,63 @@
define([
'angular',
'lodash',
'services/pro/backendSrv',
],
function (angular) {
function (angular, _) {
'use strict';
var module = angular.module('grafana.controllers');
module.controller('AccountCtrl', function($scope, $http, alertSrv) {
module.controller('AccountCtrl', function($scope, $http, backendSrv) {
$scope.collaborator = {};
$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() {
if (!$scope.addCollaboratorForm.$valid) {
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();

View File

@ -51,18 +51,28 @@
</div>
</div>
<div class="section">
<div class="section">
<div class="dashboard-editor-header">
<div class="dashboard-editor-title">
<i class="icon icon-eye-open"></i>
Active account
</div>
</div>
<br>
<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>
</table>
@ -94,7 +104,15 @@
<div class="editor-row row">
<table class="grafana-options-table span5">
<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>
</table>
</div>

View File

@ -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);
});
};
});
});