1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-06-29 23:45:58 -04:00

adjust /api/projects to allow custom number of results (max 24)

This commit is contained in:
2021-10-09 08:50:04 -04:00
parent f3d18eefc3
commit fafb4ab4a7
7 changed files with 54 additions and 47 deletions
+18 -20
View File
@@ -1,7 +1,10 @@
import fetch from "cross-fetch";
import urlParse from "url-parse";
import numeral from "numeral";
import canonicalUrl from "get-canonical-url";
import urlParse from "url-parse";
// API endpoint
const HITS_ENDPOINT = "/api/hits/";
// don't continue if there isn't a span#meta-hits element on this page
const wrapper = document.getElementById("meta-hits");
@@ -24,29 +27,24 @@ if (wrapper && canonical) {
// get path and strip beginning and ending forward slash
const slug = urlParse(canonical).pathname.replace(/^\/|\/$/g, "");
fetch(`/api/hits/?slug=${encodeURIComponent(slug)}`)
fetch(`${HITS_ENDPOINT}?slug=${encodeURIComponent(slug)}`)
.then((response) => response.json())
.then((data) => {
if (data.hits) {
// pretty number and units
const hitsComma = numeral(data.hits).format("0,0");
const hitsPlural = data.hits === 1 ? "hit" : "hits";
wrapper.title = hitsComma + " " + hitsPlural;
// pretty number and units
const hitsComma = numeral(data.hits).format("0,0");
const hitsPlural = data.hits === 1 ? "view" : "views";
wrapper.title = `${hitsComma} ${hitsPlural}`;
// finally inject the hits...
const counter = document.getElementById("meta-hits-counter");
if (counter) {
counter.appendChild(document.createTextNode(hitsComma));
}
// finally inject the hits...
const counter = document.getElementById("meta-hits-counter");
if (counter) {
counter.appendChild(document.createTextNode(hitsComma));
}
// ...and hide the loading spinner
const spinner = document.getElementById("meta-hits-loading");
if (spinner) {
spinner.style.display = "none";
}
} else {
// something went horribly wrong, initiate coverup
wrapper.style.display = "none";
// ...and hide the loading spinner
const spinner = document.getElementById("meta-hits-loading");
if (spinner) {
spinner.style.display = "none";
}
})
.catch(() => {
+5 -2
View File
@@ -6,10 +6,12 @@ import numeral from "numeral";
import { format, formatDistanceToNowStrict, parseJSON } from "date-fns";
import twemoji from "twemoji";
// API endpoint (sort by stars, limit to 12)
const PROJECTS_ENDPOINT = "/api/projects/?top&limit=12";
// don't continue if there isn't a span#meta-hits element on this page
// TODO: be better.
const wrapper = document.getElementById("github-cards");
const spinner = document.getElementById("loading-spinner");
if (wrapper) {
// this is a total sh*tshow, but safer than setting one big string via innerHTML :)
@@ -69,7 +71,7 @@ if (wrapper) {
</div>
`;
fetch("/api/projects/?top")
fetch(PROJECTS_ENDPOINT)
.then((response) => response.json())
.then((data) => {
data.forEach((repo) => {
@@ -80,6 +82,7 @@ if (wrapper) {
});
// we're done, hide the loading spinner
const spinner = document.getElementById("loading-spinner");
if (spinner) {
spinner.style.display = "none";
}