Merge pull request #2290 from vikhyat/badge-system
Incrementally load users in the badge page
This commit is contained in:
commit
d1b96d0e8b
|
@ -27,8 +27,7 @@
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{i18n admin.badges.name}}</th>
|
<th>{{i18n admin.badges.badge}}</th>
|
||||||
<th>{{i18n admin.badges.badge_type}}</th>
|
|
||||||
<th>{{i18n admin.badges.granted_by}}</th>
|
<th>{{i18n admin.badges.granted_by}}</th>
|
||||||
<th>{{i18n admin.badges.granted_at}}</th>
|
<th>{{i18n admin.badges.granted_at}}</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
|
@ -36,8 +35,7 @@
|
||||||
|
|
||||||
{{#each}}
|
{{#each}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{badge.displayName}}</td>
|
<td>{{user-badge badge=badge}}</td>
|
||||||
<td>{{badge.badge_type.name}}</td>
|
|
||||||
<td>
|
<td>
|
||||||
{{#link-to 'adminUser' badge.granted_by}}
|
{{#link-to 'adminUser' badge.granted_by}}
|
||||||
{{avatar granted_by imageSize="tiny"}}
|
{{avatar granted_by imageSize="tiny"}}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
/**
|
||||||
|
Controller for showing a particular badge.
|
||||||
|
|
||||||
|
@class BadgesShowController
|
||||||
|
@extends Discourse.ObjectController
|
||||||
|
@namespace Discourse
|
||||||
|
@module Discourse
|
||||||
|
**/
|
||||||
|
Discourse.BadgesShowController = Discourse.ObjectController.extend({
|
||||||
|
grantDates: Em.computed.mapBy('userBadges', 'grantedAt'),
|
||||||
|
minGrantedAt: Em.computed.min('grantDates'),
|
||||||
|
|
||||||
|
moreUserCount: function() {
|
||||||
|
if (this.get('userBadges')) {
|
||||||
|
return this.get('model.grant_count') - this.get('userBadges.length');
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}.property('model.grant_count', 'userBadges.length'),
|
||||||
|
|
||||||
|
showMoreUsers: Em.computed.gt('moreUserCount', 0)
|
||||||
|
});
|
|
@ -53,6 +53,10 @@ Discourse.UserBadge.reopenClass({
|
||||||
|
|
||||||
userBadges = userBadges.map(function(userBadgeJson) {
|
userBadges = userBadges.map(function(userBadgeJson) {
|
||||||
var userBadge = Discourse.UserBadge.create(userBadgeJson);
|
var userBadge = Discourse.UserBadge.create(userBadgeJson);
|
||||||
|
|
||||||
|
var grantedAtDate = Date.parse(userBadge.get('granted_at'));
|
||||||
|
userBadge.set('grantedAt', grantedAtDate);
|
||||||
|
|
||||||
userBadge.set('badge', badges[userBadge.get('badge_id')]);
|
userBadge.set('badge', badges[userBadge.get('badge_id')]);
|
||||||
if (userBadge.get('user_id')) {
|
if (userBadge.get('user_id')) {
|
||||||
userBadge.set('user', users[userBadge.get('user_id')]);
|
userBadge.set('user', users[userBadge.get('user_id')]);
|
||||||
|
@ -90,8 +94,13 @@ Discourse.UserBadge.reopenClass({
|
||||||
@param {String} badgeId
|
@param {String} badgeId
|
||||||
@returns {Promise} a promise that resolves to an array of `Discourse.UserBadge`.
|
@returns {Promise} a promise that resolves to an array of `Discourse.UserBadge`.
|
||||||
**/
|
**/
|
||||||
findByBadgeId: function(badgeId) {
|
findByBadgeId: function(badgeId, options) {
|
||||||
return Discourse.ajax("/user_badges.json?badge_id=" + badgeId).then(function(json) {
|
if (!options) { options = {}; }
|
||||||
|
var url = "/user_badges.json?badge_id=" + badgeId;
|
||||||
|
if (options.granted_before) {
|
||||||
|
url = url + "&granted_before=" + encodeURIComponent(options.granted_before);
|
||||||
|
}
|
||||||
|
return Discourse.ajax(url).then(function(json) {
|
||||||
return Discourse.UserBadge.createFromJson(json);
|
return Discourse.UserBadge.createFromJson(json);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -21,5 +21,16 @@ Discourse.BadgesShowRoute = Ember.Route.extend({
|
||||||
controller.set('userBadgesLoaded', true);
|
controller.set('userBadgesLoaded', true);
|
||||||
});
|
});
|
||||||
controller.set('model', model);
|
controller.set('model', model);
|
||||||
|
},
|
||||||
|
|
||||||
|
actions: {
|
||||||
|
loadMore: function() {
|
||||||
|
var self = this;
|
||||||
|
Discourse.UserBadge.findByBadgeId(this.currentModel.get('id'), {
|
||||||
|
granted_before: this.get('controller.minGrantedAt') / 1000
|
||||||
|
}).then(function(userBadges) {
|
||||||
|
self.get('controller.userBadges').pushObjects(userBadges);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
{{#if userBadges}}
|
{{#if userBadges}}
|
||||||
<h2>{{i18n users}}</h2>
|
<h2>{{i18n users}}</h2>
|
||||||
<br>
|
<br>
|
||||||
|
<div>
|
||||||
{{#each userBadges}}
|
{{#each userBadges}}
|
||||||
{{#link-to 'userActivity' user}}
|
{{#link-to 'userActivity' user}}
|
||||||
<div class="badge-user">
|
<div class="badge-user">
|
||||||
|
@ -27,6 +28,12 @@
|
||||||
</div>
|
</div>
|
||||||
{{/link-to}}
|
{{/link-to}}
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
</div>
|
||||||
|
{{#if showMoreUsers}}
|
||||||
|
<button class="btn" {{action loadMore}}>
|
||||||
|
{{countI18n badges.more_badges count=moreUserCount}}
|
||||||
|
</button>
|
||||||
|
{{/if}}
|
||||||
{{else}}
|
{{else}}
|
||||||
{{#unless userBadgesLoaded}}
|
{{#unless userBadgesLoaded}}
|
||||||
<div class='spinner'>{{i18n loading}}</div>
|
<div class='spinner'>{{i18n loading}}</div>
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
class UserBadgesController < ApplicationController
|
class UserBadgesController < ApplicationController
|
||||||
def index
|
def index
|
||||||
params.permit(:username)
|
params.permit(:username).permit(:granted_before)
|
||||||
|
|
||||||
if params[:username]
|
if params[:username]
|
||||||
user = fetch_user_from_params
|
user = fetch_user_from_params
|
||||||
user_badges = user.user_badges
|
user_badges = user.user_badges
|
||||||
else
|
else
|
||||||
badge = fetch_badge_from_params
|
badge = fetch_badge_from_params
|
||||||
user_badges = badge.user_badges.order('granted_at DESC').limit(200)
|
user_badges = badge.user_badges.order('granted_at DESC').limit(100)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if params[:granted_before]
|
||||||
|
user_badges = user_badges.where('granted_at < ?', Time.at(params[:granted_before].to_f))
|
||||||
|
end
|
||||||
|
|
||||||
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type)
|
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type)
|
||||||
|
|
||||||
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
|
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1717,6 +1717,7 @@ en:
|
||||||
new_badge: New Badge
|
new_badge: New Badge
|
||||||
new: New
|
new: New
|
||||||
name: Name
|
name: Name
|
||||||
|
badge: Badge
|
||||||
display_name: Display Name
|
display_name: Display Name
|
||||||
description: Description
|
description: Description
|
||||||
badge_type: Badge Type
|
badge_type: Badge Type
|
||||||
|
|
Loading…
Reference in New Issue