Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(en): merge gulp into docschina/cn @ 0ffbe8be #5

Open
wants to merge 19 commits into
base: cn
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Utilize OneGraph to join GitHub Sponsors & OpenCollective
phated committed Jul 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8274253df2a2536f7a588add7801bd37cb304b24
104 changes: 41 additions & 63 deletions src/theme/BackerBanner/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,15 @@
// TODO: This file needs a massive rewrite for GitHub Sponsors
import React, { useState, useEffect } from 'react';
import classnames from 'classnames';
import shuffle from 'lodash.shuffle';
import ExternalLink from '@theme/ExternalLink';

import styles from './banner.module.scss';

const collectiveURL ='https://rest.opencollective.com/v2/gulpjs/orders/incoming?status=active,paid&limit=1000';
const sponsorsURL = 'https://serve.onegraph.com/graphql?app_id=c8251aa1-22ab-4dca-9e57-e7c335ddcd7c';

function uniqueBySlug(array) {
const predicate = function (o) {
return o.fromAccount.slug;
}
return array.reduce(function(acc, curr) {
return acc.filter(function (a) {
return predicate(a) === predicate(curr);
}).length ? acc : acc.push(curr) && acc
}, []);
}

const specialFilter = [
'addyosmani',
];

const thirtyTwoDaysAgo = Date.now() - 2.765e+9;

function withinMonth(createdAt) {
const datePaid = new Date(createdAt);
return datePaid >= thirtyTwoDaysAgo;
}

function recentNonCompanies(backer) {
if (backer.fromAccount && specialFilter.includes(backer.fromAccount.slug)) {
return false;
}

if (backer.fromAccount && backer.fromAccount.type === 'ORGANIZATION') {
return false;
}

if (backer.tier && backer.tier.slug === 'company') {
return false;
}

if (backer.frequency === 'MONTHLY' && backer.status === 'ACTIVE') {
return true;
}

if (backer.frequency === 'ONETIME' && backer.status === 'PAID' && withinMonth(backer.createdAt)) {
function between5And250(backer) {
const amount = backer.tier.amountDonated;
if (amount >= 500 && amount < 25000) {
return true;
}

@@ -59,38 +21,54 @@ function selectRandom(backers) {
}

async function getBackers() {
const response = await fetch(collectiveURL);
const orders = await response.json();
const response = await fetch(sponsorsURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
doc_id: 'fe685787-b348-42a4-960c-2322add1e11b',
}),
});

const allBackers = orders.nodes.filter(recentNonCompanies);
// TODO: Handle errors
const { data, errors } = await response.json();

const uniqueBackers = uniqueBySlug(allBackers);
const backersToDisplay = selectRandom(uniqueBackers)
let ghBackers = data.gitHub.organization.sponsors.nodes;
let ocBackers = data.openCollective.organization.sponsors.nodes;
let allBackers = [].concat(ghBackers, ocBackers);

return backersToDisplay.map(function(backer) {
const fromAccount = backer.fromAccount;
const totalDonations = backer.totalDonations;
const validBackers = allBackers.filter(between5And250);

const name = fromAccount.name;
const slug = fromAccount.slug;
const website = fromAccount.website;
const twitterHandle = fromAccount.twitterHandle;
const imageUrl = fromAccount.imageUrl;
const backersToDisplay = selectRandom(validBackers)

return backersToDisplay.map(function (backer) {
const {
name,
openCollectiveHandle,
twitterHandle,
githubHandle,
avatarUrl
} = backer.sponsor;
// It is in US cents
const monthlyAmount = (backer.tier.amountDonated / 100);

let href;
if (website) {
href = website;
if (githubHandle) {
href = `https://github.com/${githubHandle}`;
} else if (twitterHandle) {
href = 'https://twitter.com/' + twitterHandle
href = `https://twitter.com/${twitterHandle}`;
} else {
href = 'https://opencollective.com/' + slug
href = `https://opencollective.com/${openCollectiveHandle}`
}

let usersName = name || githubHandle || twitterHandle || openCollectiveHandle || '';

return {
key: slug,
src: imageUrl,
alt: name,
title: `Thank you ${name} for $${totalDonations.value}!`,
key: href,
src: avatarUrl,
alt: usersName,
title: `Thank you ${usersName} for the $${monthlyAmount}/month!`,
href: href,
};
});