зеркало из https://github.com/mozilla/CSOL-site.git
Merge pull request #519 from mozilla/505-fix-explore-filters
Implement filtering.
This commit is contained in:
Коммит
e849180bd6
8
api.js
8
api.js
|
@ -13,7 +13,7 @@ var DEFAULT_QUERY = {
|
|||
|
||||
function middleware (method, default_query) {
|
||||
if (!_.isFunction(method))
|
||||
method = this[method];
|
||||
method = this[method];
|
||||
|
||||
if (!_.isFunction(method)) {
|
||||
throw new Error('Supplied method ' + method + ' not valid');
|
||||
|
@ -174,7 +174,7 @@ function paginate(key, dataFn) {
|
|||
if (err)
|
||||
return callback(err, data);
|
||||
|
||||
if (typeof data[key].length !== 'number')
|
||||
if (typeof data[key].length !== 'number')
|
||||
return callback(new errors.BadGateway('Unpageable data returned from upstream'), data);
|
||||
|
||||
var pages = Math.ceil(data[key].length / pageSize);
|
||||
|
@ -239,7 +239,7 @@ module.exports = function Api(origin, globalFilters, config) {
|
|||
},
|
||||
/* TODO: writable is set to true for mocking, but it would
|
||||
be nice to revisit and try to remove that line. */
|
||||
writable: true
|
||||
writable: true
|
||||
});
|
||||
}, this);
|
||||
|
||||
|
@ -273,5 +273,3 @@ module.exports = function Api(origin, globalFilters, config) {
|
|||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -126,4 +126,4 @@ module.exports = function(app) {
|
|||
next();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,6 @@ module.exports = function (app) {
|
|||
|
||||
app.get('/explore', badger.middleware('getPrograms'), function (req, res, next) {
|
||||
var data = req.remote;
|
||||
|
||||
res.render('programs/list.html', {
|
||||
filters: getFilters(req.query, ['categories', 'orgs', 'ageRanges', 'activityTypes']),
|
||||
items: data.programs,
|
||||
|
@ -385,4 +384,4 @@ module.exports = function (app) {
|
|||
app.get('/orgs/:orgName/unfavorite', function (req, res, next) {
|
||||
return res.redirect('/login', 303);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
|
|
@ -169,23 +169,6 @@ function filterBadges (data, query) {
|
|||
return data;
|
||||
}
|
||||
|
||||
function filterPrograms (data, query) {
|
||||
var category = confirmFilterValue(query.category, categories),
|
||||
org = confirmFilterValue(query.org, orgs),
|
||||
ageGroup = confirmFilterValue(query.age, ageRanges),
|
||||
activityType = confirmFilterValue(query.activity, activityTypes);
|
||||
|
||||
if (!category && !org && !ageGroup && !activityType)
|
||||
return data;
|
||||
|
||||
return applyFilter(data, {
|
||||
'categories': category,
|
||||
'issuer.shortname': org,
|
||||
'ageRange': ageGroup,
|
||||
'activityType': activityType
|
||||
});
|
||||
}
|
||||
|
||||
function getJWTToken(email) {
|
||||
var claims = {
|
||||
prn: email,
|
||||
|
@ -234,7 +217,13 @@ var openbadger = new Api(ENDPOINT, {
|
|||
|
||||
getPrograms: {
|
||||
func: function getPrograms (query, callback) {
|
||||
this.get('/programs', function(err, data) {
|
||||
var qs = {
|
||||
category: query.category,
|
||||
org: query.org,
|
||||
age: query.age,
|
||||
activity: query.activity,
|
||||
};
|
||||
this.get('/programs', {qs: qs}, function(err, data) {
|
||||
if (err)
|
||||
return callback(err, data);
|
||||
|
||||
|
@ -243,7 +232,6 @@ var openbadger = new Api(ENDPOINT, {
|
|||
});
|
||||
});
|
||||
},
|
||||
filters: filterPrograms,
|
||||
paginate: true,
|
||||
key: 'programs'
|
||||
},
|
||||
|
|
|
@ -211,10 +211,11 @@ test('getProgram', function(t) {
|
|||
});
|
||||
|
||||
test('getPrograms', function(t) {
|
||||
const CALLBACK_INDEX = 2;
|
||||
|
||||
t.test('on error', function(t) {
|
||||
var getStub = mock.expects('get');
|
||||
getStub.callsArgWith(1, 500, 'error of some sort');
|
||||
getStub.callsArgWith(CALLBACK_INDEX, 500, 'error of some sort');
|
||||
openbadger.getPrograms(DEFAULT_QUERY, function(err, data) {
|
||||
t.same(err, 500, 'error');
|
||||
t.similar(data, 'error of some sort', 'data');
|
||||
|
@ -224,7 +225,7 @@ test('getPrograms', function(t) {
|
|||
|
||||
t.test('with data', function(t) {
|
||||
var getStub = mock.expects('get');
|
||||
getStub.callsArgWith(1, null, DATA['programs']);
|
||||
getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']);
|
||||
openbadger.getPrograms(DEFAULT_QUERY, function(err, data) {
|
||||
t.notOk(err, 'no error');
|
||||
t.same(data.programs.length, 3, 'data length');
|
||||
|
@ -237,7 +238,7 @@ test('getPrograms', function(t) {
|
|||
|
||||
t.test('paginates', function(t) {
|
||||
var getStub = mock.expects('get');
|
||||
getStub.callsArgWith(1, null, DATA['programs']);
|
||||
getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']);
|
||||
openbadger.getPrograms({ pageSize: 2, page: 1 }, function(err, data) {
|
||||
t.notOk(err, 'no error');
|
||||
t.same(data.programs.length, 2, 'paginated');
|
||||
|
|
|
@ -4,4 +4,4 @@
|
|||
|
||||
{% block item_actions %}
|
||||
{{ super() }}
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
|
Загрузка…
Ссылка в новой задаче