From 7e2bcd7766b32d1ed773afb099c84696343f1b8d Mon Sep 17 00:00:00 2001 From: Steve Cassidy Date: Fri, 22 Sep 2023 17:46:40 +1000 Subject: [PATCH] Upload notebook form Signed-off-by: Steve Cassidy --- src/api/routes.ts | 3 +- views/notebooks.handlebars | 124 +++++++++++++++++++++++++++++++------ 2 files changed, 108 insertions(+), 19 deletions(-) diff --git a/src/api/routes.ts b/src/api/routes.ts index ed8bc74b..157e31ba 100644 --- a/src/api/routes.ts +++ b/src/api/routes.ts @@ -91,8 +91,9 @@ api.post('/notebooks/', requireAuthenticationAPI, async (req, res) => { try { const projectID = await createNotebook(projectName, uiSpec, metadata); res.json({notebook: projectID}); - } catch { + } catch (err) { res.json({error: 'there was an error creating the notebook'}); + console.log('Error creating notebook', err); res.status(500).end(); } } else { diff --git a/views/notebooks.handlebars b/views/notebooks.handlebars index 4e29d320..3cec79fd 100644 --- a/views/notebooks.handlebars +++ b/views/notebooks.handlebars @@ -6,6 +6,57 @@ +

Notebooks

+ + + {{#each notebooks}} + + + {{#if ../developer}} + + {{/if}} + + {{/each}} +
+ {{this.name}} + + +
+ + +
+

+ +

+ +
+
+
+
+ + +
Optional, if not provided the name property from the file will be used.
+
+ +
+ +
Select a notebook file in JSON format.
+
+ +
+ +
+
+
+
+
+ + + -

Notebooks

- - {{#each notebooks}} - - - {{#if ../developer}} - - {{/if}} - - {{/each}} -
- {{this.name}} - - -
- - \ No newline at end of file + function uploadNotebookHandler(event) { + event.preventDefault(); + const url = '/api/notebooks/'; + const form = event.target; + const name = form.querySelector('[name="name"]').value; + const fileInput = form.querySelector('[name="notebook"]'); + if (fileInput) { + // parse the contents of the uploaded file as JSON + const reader = new FileReader(); + const filename = fileInput.files[0]; + reader.readAsText(filename); + reader.onload = (evt) => { + try { + const data = JSON.parse(evt.target.result); + // a little bit of validation + if (!data.metadata || !data['ui-specification']) { + alert('Invalid notebook file'); + return; + } + if (!data.metadata.name) { + alert('Invalid notebook file'); + return; + } + const body = { + metadata: data.metadata, + 'ui-specification': data['ui-specification'], + name: name || data.metadata.name, + } + fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }) + .then(response => response.json()) + .then(data => { + if (data.notebook) { + window.location.reload(); + } else { + alert('Error uploading notebook'); + } + }); + } catch (e) { + alert('Invalid notebook file'); + return; + } + } + + + + } + } + document.getElementById('upload-notebook-form').onsubmit = uploadNotebookHandler; +