diff --git a/app/assets/javascripts/admin/controllers/admin_user_badges_controller.js b/app/assets/javascripts/admin/controllers/admin_user_badges_controller.js index a534195a129..af120daca11 100644 --- a/app/assets/javascripts/admin/controllers/admin_user_badges_controller.js +++ b/app/assets/javascripts/admin/controllers/admin_user_badges_controller.js @@ -13,6 +13,36 @@ Discourse.AdminUserBadgesController = Ember.ArrayController.extend({ sortProperties: ['granted_at'], sortAscending: false, + /** + Array of badges that have not been granted to this user. + + @property grantableBadges + @type {Boolean} + **/ + grantableBadges: function() { + var granted = {}; + this.get('model').forEach(function(userBadge) { + granted[userBadge.get('badge_id')] = true; + }); + + var badges = []; + this.get('badges').forEach(function(badge) { + if (!granted[badge.get('id')]) { + badges.push(badge); + } + }); + + return badges; + }.property('badges.@each', 'model.@each'), + + /** + Whether there are any badges that can be granted. + + @property noBadges + @type {Boolean} + **/ + noBadges: Em.computed.empty('grantableBadges'), + actions: { /** @@ -25,6 +55,10 @@ Discourse.AdminUserBadgesController = Ember.ArrayController.extend({ var self = this; Discourse.UserBadge.grant(badgeId, this.get('user.username')).then(function(userBadge) { self.pushObject(userBadge); + Ember.run.next(function() { + // Update the selected badge ID after the combobox has re-rendered. + self.set('selectedBadgeId', self.get('grantableBadges')[0].get('id')); + }); }, function() { // Failure bootbox.alert(I18n.t('generic_error')); diff --git a/app/assets/javascripts/admin/routes/admin_user_badges_route.js b/app/assets/javascripts/admin/routes/admin_user_badges_route.js index b1184b3b12e..de4224f523d 100644 --- a/app/assets/javascripts/admin/routes/admin_user_badges_route.js +++ b/app/assets/javascripts/admin/routes/admin_user_badges_route.js @@ -19,9 +19,10 @@ Discourse.AdminUserBadgesRoute = Discourse.Route.extend({ Discourse.Badge.findAll().then(function(badges) { controller.set('badges', badges); if (badges.length > 0) { - controller.set('selectedBadgeId', badges[0].get('id')); - } else { - controller.set('noBadges', true); + var grantableBadges = controller.get('grantableBadges'); + if (grantableBadges.length > 0) { + controller.set('selectedBadgeId', grantableBadges[0].get('id')); + } } controller.set('loading', false); }); diff --git a/app/assets/javascripts/admin/templates/user_badges.js.handlebars b/app/assets/javascripts/admin/templates/user_badges.js.handlebars index aec5a82889f..cc69a79b773 100644 --- a/app/assets/javascripts/admin/templates/user_badges.js.handlebars +++ b/app/assets/javascripts/admin/templates/user_badges.js.handlebars @@ -15,7 +15,7 @@

{{i18n admin.badges.no_badges}}

{{else}}
- {{combobox valueAttribute="id" value=controller.selectedBadgeId content=controller.badges}} + {{combobox valueAttribute="id" value=controller.selectedBadgeId content=controller.grantableBadges}} {{/if}}