Skip to content

Commit dd1385c

Browse files
znarfphated
authored andcommitted
fetch from Open Collective using the latest API (#103)
1 parent 7060cf2 commit dd1385c

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

website/static/js/script.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function(){
22
var services = [
3-
'https://opencollective.com/api/groups/gulpjs/backers',
3+
'https://rest.opencollective.com/v2/gulpjs/orders/incoming?status=active,cancelled,paid&limit=1000',
44
'https://api.npmjs.org/downloads/point/last-week/gulp',
55
'https://api.npms.io/v2/search?q=keywords%3Agulpplugin',
66
].map(function (url) {
@@ -11,6 +11,21 @@
1111
return resp.json();
1212
}
1313

14+
function uniqueBySlug(array) {
15+
var predicate = function (o) {
16+
return o.fromAccount.slug;
17+
}
18+
return array.reduce(function(acc, curr) {
19+
return acc.filter(function (a) {
20+
return predicate(a) === predicate(curr);
21+
}).length ? acc : acc.push(curr) && acc
22+
}, []);
23+
}
24+
25+
function sortByTotalDonations(a, b) {
26+
return b.totalDonations.value - a.totalDonations.value;
27+
}
28+
1429
var supporters = services[0];
1530
var downloads = services[1];
1631
var plugins = services[2];
@@ -42,31 +57,41 @@
4257
}
4358
})
4459

45-
supporters.then(function (entries) {
60+
supporters.then(function (orders) {
4661
var fragment = document.createDocumentFragment();
4762
var nodes = [];
48-
var supportersToDisplay = entries.slice(0, 10);
63+
var uniqueSupporters = uniqueBySlug(orders.nodes.sort(sortByTotalDonations));
64+
var supportersToDisplay = uniqueSupporters.slice(0, 10);
4965

5066
supportersToDisplay.forEach(function(supporter) {
67+
var fromAccount = supporter.fromAccount;
68+
var totalDonations = supporter.totalDonations;
69+
70+
var name = fromAccount.name;
71+
var slug = fromAccount.slug;
72+
var website = fromAccount.website;
73+
var twitterHandle = fromAccount.twitterHandle;
74+
var imageUrl = fromAccount.imageUrl;
75+
5176
var img = new Image();
52-
img.src = supporter.avatar;
77+
img.src = imageUrl;
5378
img.width = 80;
5479
img.onload = function(e) {
5580
e.currentTarget.parentNode.classList.remove('supporter--skeleton');
5681
}
82+
img.alt = name;
5783

5884
var link = document.createElement('a');
5985
link.className = 'supporter supporter--skeleton';
6086
link.rel = 'nofollow';
61-
if (supporter.website) {
62-
link.href = supporter.website;
63-
img.alt = supporter.website;
64-
} else if (supporter.twitterHandle) {
65-
link.href = 'https://twitter.com/' + supporter.twitterHandle
66-
img.alt = supporter.twitterHandle;
87+
link.title = name + ' with $' + totalDonations.value + ' total';
88+
89+
if (website) {
90+
link.href = website;
91+
} else if (twitterHandle) {
92+
link.href = 'https://twitter.com/' + twitterHandle
6793
} else {
68-
link.href = 'https://opencollective.com/' + supporter.slug
69-
img.alt = supporter.slug;
94+
link.href = 'https://opencollective.com/' + slug
7095
}
7196

7297
link.appendChild(img);

0 commit comments

Comments
 (0)