From 7ecc91e07f685771ece625f77cbe102faa033b8b Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 14 Nov 2018 22:47:15 -0400 Subject: Remix lists --- index.js | 102 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index 2e5202a..488ce6c 100755 --- a/index.js +++ b/index.js @@ -185,6 +185,21 @@ const templates = { text += '' return text + }, + + projectList: async (url, pathname, pageNumber) => { + const offset = (pageNumber - 1) * limit + const projects = await fetch(`${url}?limit=${limit}&offset=${offset}`).then(res => res.json()) + const projectsText = projects.map(templates.projectThumbnail).join('\n') + + return fixWS` + +

You are on page ${pageNumber}. + ${projects.length === limit && `Next`} + ${pageNumber > 1 && `Previous`}

+ ` } } @@ -358,7 +373,7 @@ const handleRequest = async (request, response) => { } } - if (compareArr(urlParts, ['projects', id => /^[0-9]*$/.test(id)])) { + if (compareArr(urlParts.slice(0, 2), ['projects', id => /^[0-9]*$/.test(id)])) { const projectID = urlParts[1] const project = await getProject(projectID, cookie.token) @@ -369,36 +384,56 @@ const handleRequest = async (request, response) => { return } - let parentProjectText = '' - if (project.remix.parent) { - const parentProject = await getProject(project.remix.parent, cookie.token) - if (parentProject.code === 'NotFound') { - parentProjectText = ` Based on an unshared project.` - } else { - parentProjectText = ` Based on ${templates.project(parentProject.title, parentProject.id)} by ${templates.user(parentProject.author.username)}.` + if (urlParts.length === 2) { + let parentProjectText = '' + if (project.remix.parent) { + const parentProject = await getProject(project.remix.parent, cookie.token) + if (parentProject.code === 'NotFound') { + parentProjectText = ` Based on an unshared project.` + } else { + parentProjectText = ` Based on ${templates.project(parentProject.title, parentProject.id)} by ${templates.user(parentProject.author.username)}.` + } } - } - await page(request, response, fixWS` -

${project.title}

-

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

-

The thumbnail for this project

- ${project.instructions ? fixWS` -

Instructions

- ${templates.longField(project.instructions)} - ` : fixWS` -

(No instructions.)

- `} - ${project.description ? fixWS` -

Notes and Credits

- ${templates.longField(project.description)} - ` : fixWS` -

(No notes and credits.)

- `} -

Download!

- `) + const remixes = await fetch(`https://api.scratch.mit.edu/projects/${projectID}/remixes?limit=5`).then(res => res.json()) + const remixesText = remixes.map(templates.projectThumbnail).join('\n') - return + await page(request, response, fixWS` +

${project.title}

+

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

+

The thumbnail for this project

+ ${project.instructions ? fixWS` +

Instructions

+ ${templates.longField(project.instructions)} + ` : fixWS` +

(No instructions.)

+ `} + ${project.description ? fixWS` +

Notes and Credits

+ ${templates.longField(project.description)} + ` : fixWS` +

(No notes and credits.)

+ `} +

Download!

+ ${remixes.length ? fixWS` +
+

Remixes

+ +

See all!

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

Remixes of ${filterHTML(project.title)}

+ ${await templates.projectList(`https://api.scratch.mit.edu/projects/${projectID}/remixes`, pathname, pageNumber)} + `) + + return + } } if (compareArr(urlParts.slice(0, 2), ['users', name => /^[a-zA-Z0-9\-_]*$/.test(name)])) { @@ -439,18 +474,9 @@ const handleRequest = async (request, response) => { return } else if (compareArr(urlParts.slice(2), ['projects'])) { - const offset = (pageNumber - 1) * limit - const projects = await fetch(`https://api.scratch.mit.edu/users/${username}/projects?limit=${limit}&offset=${offset}`).then(res => res.json()) - const projectsText = projects.map(templates.projectThumbnail).join('\n') - await page(request, response, fixWS`

${user.username}'s projects

- -

You are on page ${pageNumber}. - ${projects.length === limit && `Next`} - ${pageNumber > 1 && `Previous`}

+ ${await templates.projectList(`https://api.scratch.mit.edu/users/${username}/projects`, pathname, pageNumber)} `) return -- cgit 1.3.0-6-gf8a5