From e7f9e1c465407179837e490b3c1b61ac8d6aa8f7 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 15 Nov 2018 00:40:13 -0400 Subject: Studio page --- index.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'index.js') 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`

${filterHTML(project.title)}

Created by ${templates.user(project.author.username)}.${parentProjectText}

-

The thumbnail for this project

+

This project's thumbnail

${project.instructions ? fixWS`

Instructions

${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` +

${filterHTML(studio.title)}

+

This studio's thumbnail

+

Description

+ ${templates.longField(studio.description)} +

Projects

+ ${projects.length ? fixWS` + +

See all!

+ ` : `

This studio doesn't have any projects yet!

`} + `) + } else if (compareArr(urlParts.slice(2), ['projects'])) { + return page(request, response, fixWS` +

Projects in ${filterHTML(studio.title)}

+ ${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) -- cgit 1.3.0-6-gf8a5