From 4b80722dc2f67bb5a6cbca55a4adf2b4da84e5a7 Mon Sep 17 00:00:00 2001 From: kakudo Date: Sun, 24 Oct 2021 00:28:47 +0900 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E8=A6=8F=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E4=BD=9C=E6=88=90=E3=83=95=E3=82=A9=E3=83=BC=E3=83=A0?= =?UTF-8?q?=E3=82=92=E6=9B=B8=E3=81=8D=E7=9B=B4=E3=81=97=E3=81=A6=E3=80=81?= =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E9=81=B7=E7=A7=BB=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/css/index.css | 29 +++++++++++++++++++++++++++++ static/js/index.js | 41 ++++++++++++++++++++++++++--------------- templates/index.html | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 86 insertions(+), 26 deletions(-) diff --git a/static/css/index.css b/static/css/index.css index e69de29..26d981f 100644 --- a/static/css/index.css +++ b/static/css/index.css @@ -0,0 +1,29 @@ +div.new-topic-form { + padding: 0 0.5rem; +} + +form.new-topic-form input[type="text"] { + margin: 0.5rem 0 0; + padding: 0.5rem; + width: 100%; + border: 1px solid #000; +} + +form.new-topic-form .position { + padding: 0.5rem 0 0; + display: flex; + align-items: center; + justify-content: space-around; +} + +form.new-topic-form input[type="radio"].new-topic-form-position { + appearance: auto; +} + +form.new-topic-form button.new-topic-form-submit-button { + margin: 0.5rem 0 0; + padding: 0.5rem; + width: 100%; + border: 1px solid #0004; + cursor: pointer; +} diff --git a/static/js/index.js b/static/js/index.js index adaa8c1..45d1ac9 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -1,21 +1,32 @@ -const form = document.getElementById("form"); -const button = document.getElementById("button"); +const newTopicFormSubmitButton = document.querySelector("button.new-topic-form-submit-button"); -button.addEventListener("click", (event) => { - const data = new FormData(form); - let obj = {}; - obj.parent_comment_id = 0; - obj.title = data.get("title"); - obj.text = data.get("text"); - obj.attribute = Number(data.get("sanpi")); +newTopicFormSubmitButton.addEventListener("click", () => { + const newTopicForm = document.querySelector("form.new-topic-form"); + let formData = new FormData(newTopicForm); + let requestData = {}; + requestData.parent_comment_id = 0; + requestData.title = formData.get("title"); + requestData.text = formData.get("content"); + if (formData.get("position") === "agree") { + requestData.attribute = 1; + } + if (formData.get("position") === "neutral") { + requestData.attribute = 0; + } + if (formData.get("position") === "disagree") { + requestData.attribute = -1; + } fetch("/comment", { method: "POST", headers: { "Content-Type": "application/json; charset=UTF-8" }, - body: JSON.stringify(obj) - }) - .then((res) => { return res.text(); }) - .then((data) => { console.log(data) }) - .catch((err) => { console.log(err); }); -}, false); + body: JSON.stringify(requestData) + }).then((response) => { + return response.json(); + }).then((responseJSON) => { + location.pathname = `/comment/${responseJSON.comment_id}`; + }).catch((error) => { + console.error(error); + }); +}); diff --git a/templates/index.html b/templates/index.html index e78f199..f1ab5bf 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,19 +4,39 @@ Document + + -
- - - - - - - - - -
+
+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ +
+
\ No newline at end of file