« get me outta code hell

Studio page - scratchrlol - Simple HTML-based Scratch client
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-11-15 00:40:13 -0400
committerFlorrie <towerofnix@gmail.com>2018-11-15 00:40:13 -0400
commite7f9e1c465407179837e490b3c1b61ac8d6aa8f7 (patch)
tree1678f246b5a8ed39d8bcd487d81f1f7804e63c24
parentd8c9bde1a563f3cae8ee93cfa0174d621109a7a6 (diff)
Studio page
-rwxr-xr-xindex.js43
1 files changed, 42 insertions, 1 deletions
diff --git a/index.js b/index.js
index ef79d57..4818521 100755
--- a/index.js
+++ b/index.js
@@ -70,6 +70,11 @@ const getProject = function(projectID, token) {
     .then(res => res.json())
 }
 
+const getStudio = function(projectID) {
+  return fetch(`https://api.scratch.mit.edu/studios/${projectID}`)
+    .then(res => res.json())
+}
+
 const getUser = function(username) {
   return fetch(`https://api.scratch.mit.edu/users/${username}`)
     .then(res => res.json())
@@ -390,7 +395,7 @@ const handleRequest = async (request, response) => {
       return page(request, response, fixWS`
         <h1>${filterHTML(project.title)}</h1>
         <p>Created by ${templates.user(project.author.username)}.${parentProjectText}</p>
-        <p><img src="${project.image}" alt="The thumbnail for this project"></p>
+        <p><img src="${project.image}" alt="This project's thumbnail"></p>
         ${project.instructions ? fixWS`
           <h2>Instructions</h2>
           ${templates.longField(project.instructions)}
@@ -421,6 +426,42 @@ const handleRequest = async (request, response) => {
     }
   }
 
+  if (compareArr(urlParts.slice(0, 2), ['studios', id => /^[0-9]*$/.test(id)])) {
+    const studioID = urlParts[1]
+
+    const studio = await getStudio(studioID)
+    if (studio.code === 'NotFound') {
+      response.statusCode = 404
+      return page(request, response, fixWS`
+        404. Sorry, that studo doesn't exist.
+      `, 'Studio Not Found')
+    }
+
+    if (urlParts.length === 2) {
+      const projects = await fetch(`https://api.scratch.mit.edu/studios/${studioID}/projects?limit=5`).then(res => res.json())
+      const projectsText = projects.map(templates.projectThumbnail).join('\n')
+
+      return page(request, response, fixWS`
+        <h1>${filterHTML(studio.title)}</h1>
+        <p><img src="${studio.image}" alt="This studio's thumbnail"></p>
+        <h2>Description</h2>
+        ${templates.longField(studio.description)}
+        <h2>Projects</h2>
+        ${projects.length ? fixWS`
+          <ul>
+            ${projectsText}
+          </ul>
+          <p><a href="/studios/${studioID}/projects">See all!</a></p>
+        ` : `<p>This studio doesn't have any projects yet!</p>`}
+      `)
+    } else if (compareArr(urlParts.slice(2), ['projects'])) {
+      return page(request, response, fixWS`
+        <h1>Projects in ${filterHTML(studio.title)}</h1>
+        ${await templates.projectList(`https://api.scratch.mit.edu/studios/${studioID}/projects`, pathname, pageNumber)}
+      `, `Projects in ${studio.title}`)
+    }
+  }
+
   if (compareArr(urlParts.slice(0, 2), ['users', name => /^[a-zA-Z0-9\-_]*$/.test(name)])) {
     const username = urlParts[1]
     const user = await getUser(username)