From 9a0264b7fcd75574a5cccc93c269d44cc2f57430 Mon Sep 17 00:00:00 2001 From: Saba Date: Sat, 27 Nov 2021 20:36:03 -0500 Subject: [PATCH] Add a dummy POST config endpoint, integrate with editable UI --- src/main.py | 7 +++---- views/scripts/config.js | 35 +++++++++++++---------------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/main.py b/src/main.py index e71871f0..372fbbee 100644 --- a/src/main.py +++ b/src/main.py @@ -37,10 +37,9 @@ def config(): return config @app.post('/config') -def config(): - print("hello posted config") - print(config) - return config +async def config(updated_config: Request): + data = await updated_config.json() + return data @app.get('/search') def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None): diff --git a/views/scripts/config.js b/views/scripts/config.js index 53046a5c..e34fdf57 100644 --- a/views/scripts/config.js +++ b/views/scripts/config.js @@ -16,10 +16,15 @@ fetch("/config") configForm.addEventListener("submit", (event) => { event.preventDefault(); - console.log("submitted!"); - console.log(event); - console.log(configForm.children); - console.log(configForm.childNodes); + const response = fetch("/config", { + method: "POST", + credentials: "same-origin", + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(rawConfig) + }).then(response => response.json()) + .then((data) => console.log(data)); }); }); @@ -29,24 +34,21 @@ function processChildren(element, data) { child.id = key; child.className = "config-element"; child.appendChild(document.createTextNode(key + ": ")); - if (data[key] === Object(data[key])) { + if (data[key] === Object(data[key]) && !Array.isArray(data[key])) { processChildren(child, data[key]); } else { var value = document.createElement("span"); value.id = key+"-value"; - value.textContent = data[key]; + value.textContent = !data[key] ? "🖊️" : data[key]; makeElementEditable(value, data, key); child.appendChild(value); } element.appendChild(child); - // data[key] = "wassup?"; } - console.log(data); - console.log(rawConfig); } function makeElementEditable(original, data, key) { - original.addEventListener("click", (event) => { + original.addEventListener("click", () => { var inputNewText = document.createElement("input"); inputNewText.type = "text"; inputNewText.className = "config-element-edit"; @@ -59,22 +61,11 @@ function makeElementEditable(original, data, key) { function fixInputOnFocusOut(original, data, key) { original.addEventListener("blur", () => { - console.log(original); var value = document.createElement("span"); value.id = original.id; value.textContent = original.value; data[key] = value.textContent; - console.log(data); - console.log(rawConfig); - makeElementEditable(value); + makeElementEditable(value, data, key); original.parentNode.replaceChild(value, original); }) } - -function handleSubmit() { - submitButton.addEventListener("click", (event) => { - var submitButton = document.createElement("button"); - submitButton.type = "submit"; - }); - configForm.appendChild(submitButton); -} \ No newline at end of file