From a797064fb83fde0450973062374e67f66dc75bb8 Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 11:20:36 -0500 Subject: [PATCH 1/9] add pagination support for fetching contributors from Github --- js/contributors.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index 21cefc5..da247eb 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -1,18 +1,25 @@ -function getContributors() { +function getContributors(page) { if (window.jQuery) { - const container = $('#contributors'); - jQuery.ajax('https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors', {}) + const container = $("#contributors"); + jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}`, {}) .done(function (data) { + if (data.length === 0) { + return; + } + const contributorList = []; data.forEach((contributor, idx) => { - contributorList.push(`${contributor.login}`); + contributorList.push( + `${contributor.login}` + ); if (idx < data.length - 1) { - contributorList.push(', '); + contributorList.push(", "); } }); - container.append(contributorList.join('')); + container.append(contributorList.join("")); + getContributors(page + 1); }); } } -$(document).ready(getContributors); +$(document).ready(getContributors(1)); From 0f308fa78478d760188f5f6989e0da64a53594ee Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 11:23:14 -0500 Subject: [PATCH 2/9] update code style --- js/contributors.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index da247eb..6b22954 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -1,6 +1,6 @@ function getContributors(page) { if (window.jQuery) { - const container = $("#contributors"); + const container = $('#contributors'); jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}`, {}) .done(function (data) { if (data.length === 0) { @@ -13,10 +13,10 @@ function getContributors(page) { `${contributor.login}` ); if (idx < data.length - 1) { - contributorList.push(", "); + contributorList.push(', '); } }); - container.append(contributorList.join("")); + container.append(contributorList.join('')); getContributors(page + 1); }); } From e14c3ce8cf67b4a0d43b9e8a875255ae41b8d334 Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 11:23:46 -0500 Subject: [PATCH 3/9] more code style --- js/contributors.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index 6b22954..53746a3 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -9,9 +9,7 @@ function getContributors(page) { const contributorList = []; data.forEach((contributor, idx) => { - contributorList.push( - `${contributor.login}` - ); + contributorList.push(`${contributor.login}`); if (idx < data.length - 1) { contributorList.push(', '); } From da8c3fc7d0f5234bd4b6406453b6f5f2c250478e Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 11:29:26 -0500 Subject: [PATCH 4/9] fix commas --- js/contributors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/contributors.js b/js/contributors.js index 53746a3..1def644 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -10,7 +10,7 @@ function getContributors(page) { const contributorList = []; data.forEach((contributor, idx) => { contributorList.push(`${contributor.login}`); - if (idx < data.length - 1) { + if (idx < data.length - 1 || page > 1) { contributorList.push(', '); } }); From 025d08a397666980a390a32aab9bcc3cc54b0438 Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 11:30:57 -0500 Subject: [PATCH 5/9] fix comma issue --- js/contributors.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/contributors.js b/js/contributors.js index 1def644..362a047 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -9,8 +9,12 @@ function getContributors(page) { const contributorList = []; data.forEach((contributor, idx) => { + if (idx === 0 && page > 1) { + contributorList.push(', '); + } + contributorList.push(`${contributor.login}`); - if (idx < data.length - 1 || page > 1) { + if (idx < data.length - 1) { contributorList.push(', '); } }); From 31d5ad89746135884205002d6bbfbb9b63282812 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 30 Apr 2020 13:16:22 -0500 Subject: [PATCH 6/9] Use per_page on GH API call Co-authored-by: rex.tsou --- js/contributors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/contributors.js b/js/contributors.js index 362a047..24ebd43 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -1,7 +1,7 @@ function getContributors(page) { if (window.jQuery) { const container = $('#contributors'); - jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}`, {}) + jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}&per_page=100`, {}) .done(function (data) { if (data.length === 0) { return; From fac4c47066dd05eca400285bc0b7d6e1cd099cf4 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 30 Apr 2020 13:16:52 -0500 Subject: [PATCH 7/9] Update contributors short-circuit logic Co-authored-by: rex.tsou --- js/contributors.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/contributors.js b/js/contributors.js index 24ebd43..28039c5 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -19,6 +19,8 @@ function getContributors(page) { } }); container.append(contributorList.join('')); + // If the length of the data is < 100, we know we are processing the last page of data. + if (data.length < 100) return; getContributors(page + 1); }); } From e339246b57e4ba5a84c9dc114960edbc2b1455a3 Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 13:17:36 -0500 Subject: [PATCH 8/9] remove unnecessary page check --- js/contributors.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index 28039c5..1a4f7a7 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -3,10 +3,6 @@ function getContributors(page) { const container = $('#contributors'); jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}&per_page=100`, {}) .done(function (data) { - if (data.length === 0) { - return; - } - const contributorList = []; data.forEach((contributor, idx) => { if (idx === 0 && page > 1) { From db1aaea08133f0165f4103bc037638a9d48b9f68 Mon Sep 17 00:00:00 2001 From: James Glenn Date: Thu, 30 Apr 2020 13:48:30 -0500 Subject: [PATCH 9/9] pull out 100 per page into its own const --- js/contributors.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index 1a4f7a7..8961f1e 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -1,7 +1,8 @@ function getContributors(page) { + const PER_PAGE = 100 if (window.jQuery) { const container = $('#contributors'); - jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}&per_page=100`, {}) + jQuery.ajax(`https://api.github.com/repos/mikebryant/ac-nh-turnip-prices/contributors?page=${page}&per_page=${PER_PAGE}`, {}) .done(function (data) { const contributorList = []; data.forEach((contributor, idx) => { @@ -15,8 +16,8 @@ function getContributors(page) { } }); container.append(contributorList.join('')); - // If the length of the data is < 100, we know we are processing the last page of data. - if (data.length < 100) return; + // If the length of the data is < PER_PAGE, we know we are processing the last page of data. + if (data.length < PER_PAGE) return; getContributors(page + 1); }); }