Commit 9cbaf072 by Daniel Lee

WIP: add some TS types

parent cbbe90ee
......@@ -26,7 +26,7 @@
<form name="addMemberForm" class="gf-form-group">
<div class="gf-form">
<span class="gf-form-label width-10">Name</span>
<input type="text" bs-typeahead="ctrl.searchUsers" required ng-model="ctrl.user.name" class="gf-form-input max-width-14" >
<input type="text" bs-typeahead="ctrl.searchUsers" required ng-model="ctrl.userName" class="gf-form-input max-width-14" >
</div>
<div class="gf-form-button-row">
......
......@@ -21,7 +21,7 @@ var backendSrv = {
backendSrv: backendSrv,
$routeParams: {id: 1}
});
ctx.ctrl.user = {name: 'login - user@email.com'};
ctx.ctrl.userName = 'login - user@email.com';
}));
describe('when user is chosen to be added to user group', () => {
......
......@@ -4,10 +4,10 @@ import coreModule from 'app/core/core_module';
import _ from 'lodash';
export default class UserGroupDetailsCtrl {
userGroup: any;
userGroupMembers = [];
user: any;
usersSearchCache = [];
userGroup: UserGroup;
userGroupMembers: User[] = [];
userName = '';
usersSearchCache: User[] = [];
searchUsers: any;
constructor(private $scope, private $http, private backendSrv, private $routeParams) {
......@@ -15,17 +15,21 @@ export default class UserGroupDetailsCtrl {
this.usersSearchCache = [];
this.searchUsers = (queryStr, callback) => {
if (this.usersSearchCache.length > 0) {
callback(_.map(this.usersSearchCache, (user) => { return user.login + ' - ' + user.email; }));
callback(_.map(this.usersSearchCache, this.userKey));
return;
}
this.backendSrv.get('/api/users/search?perpage=10&page=1&query=' + queryStr).then(result => {
this.usersSearchCache = result.users;
callback(_.map(result.users, (user) => { return user.login + ' - ' + user.email; }));
callback(_.map(result.users, this.userKey));
});
};
}
private userKey(user: User) {
return user.login + ' - ' + user.email;
}
get() {
if (this.$routeParams && this.$routeParams.id) {
this.backendSrv.get(`/api/user-groups/${this.$routeParams.id}`)
......@@ -39,7 +43,7 @@ export default class UserGroupDetailsCtrl {
}
}
removeUserGroupMember(userGroupMember) {
removeUserGroupMember(userGroupMember: UserGroupMember) {
this.$scope.appEvent('confirm-modal', {
title: 'Remove Member',
text: 'Are you sure you want to remove ' + userGroupMember.name + ' from this group?',
......@@ -51,7 +55,7 @@ export default class UserGroupDetailsCtrl {
});
}
removeMemberConfirmed(userGroupMember) {
removeMemberConfirmed(userGroupMember: UserGroupMember) {
this.backendSrv.delete(`/api/user-groups/${this.$routeParams.id}/members/${userGroupMember.userId}`)
.then(this.get.bind(this));
}
......@@ -65,14 +69,31 @@ export default class UserGroupDetailsCtrl {
addMember() {
if (!this.$scope.addMemberForm.$valid) { return; }
const login = this.user.name.split(' - ')[0];
const login = this.userName.split(' - ')[0];
const memberToAdd = _.find(this.usersSearchCache, ['login', login]);
this.backendSrv.post(`/api/user-groups/${this.$routeParams.id}/members`, {userId: memberToAdd.id}).then(() => {
this.user.name = '';
this.userName = '';
this.get();
});
}
}
export interface UserGroup {
id: number;
name: string;
}
export interface User {
id: number;
name: string;
login: string;
email: string;
}
export interface UserGroupMember {
userId: number;
name: string;
}
coreModule.controller('UserGroupDetailsCtrl', UserGroupDetailsCtrl);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment