« get me outta code hell

Explore projects, not users - scratchrlol - Simple HTML-based Scratch client
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-11-19 23:37:27 -0400
committerFlorrie <towerofnix@gmail.com>2018-11-19 23:37:27 -0400
commita51366e6048426203bd4777ce352e1af5ff24a98 (patch)
tree7ab845c7c8161f4d5588f361c710814303215884
parent5c8ce1db7004f8b44762a497438def8ea3c87fb5 (diff)
Explore projects, not users
-rwxr-xr-xindex.js49
1 files changed, 22 insertions, 27 deletions
diff --git a/index.js b/index.js
index 27552c9..001ce39 100755
--- a/index.js
+++ b/index.js
@@ -308,26 +308,21 @@ const getData = function(request) {
   })
 }
 
-const explore = {
-  users: async () => {
-    const getUrl = mode => {
-      const limit = Math.ceil(Math.random() * 40)
-      const offset = Math.floor(Math.random() * 400)
-      return `https://api.scratch.mit.edu/explore/projects?mode=${mode}&limit=${limit}&offset=${offset}`
-    }
+const getExploreProjects = async () => {
+  const getUrl = mode => {
+    const limit = Math.ceil(Math.random() * 40)
+    const offset = Math.floor(Math.random() * 400)
+    return `https://api.scratch.mit.edu/explore/projects?mode=${mode}&limit=${limit}&offset=${offset}`
+  }
 
-    const fetchResults = await Promise.all(['recent', 'trending', 'popular'].map(m => fetch(getUrl(m)).then(res => res.json())))
-    const projects = fetchResults.reduce((acc, arr) => acc.concat(arr), [])
+  const fetchResults = await Promise.all(['recent', 'trending', 'popular'].map(m => fetch(getUrl(m)).then(res => res.json())))
+  let projects = fetchResults.reduce((acc, arr) => acc.concat(arr), [])
 
-    // Dumb "remove duplicates" code incoming!
-    let authors = projects.map(project => project.author)
-    const seenUsernames = []
-    authors = authors.filter(
-      ({ username: un }) => seenUsernames.includes(un) ? false : (seenUsernames.push(un), true))
+  // Dumb "remove duplicates" code incoming!
+  const seenIDs = []
+  projects = projects.filter(({ id }) => seenIDs.includes(id) ? false : (seenIDs.push(id), true))
 
-    const selected = shuffle(projects).slice(0, 15)
-    return selected.map(project => project.author)
-  }
+  return shuffle(projects).slice(0, 12)
 }
 
 const handleRequest = async (request, response) => {
@@ -593,17 +588,17 @@ const handleRequest = async (request, response) => {
     }
   }
 
-  if (urlParts[0] === 'explore') {
-    if (compareArr(urlParts.slice(1), ['users'])) {
-      const users = await explore.users()
+  if (compareArr(urlParts, ['explore'])) {
+    const projects = await getExploreProjects()
 
-      return page(request, response, fixWS`
-        <h1>Explore Users</h1>
-        <p>Here are some randomly picked users to check out:</p>
-        <p>${users.map(u => templates.user(u.username, u.id)).join(', ')}</p>
-        <p>(Users are randomly selected from the authors of a variety of recent and trending projects.)</p>
-      `)
-    }
+    return page(request, response, fixWS`
+      <h1>Explore</h1>
+      <p>Here are some randomly picked projects to check out:</p>
+      <ul class="thumb-list">
+        ${projects.map(templates.projectThumbnail).join('\n')}
+      </ul>
+      <p>(Results are randomly selected from a variety of recent and trending projects.)</p>
+    `)
   }
 
   if (compareArr(urlParts, ['style.css'])) {