aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLarsVomMars2020-10-10 12:05:53 +0200
committerLarsVomMars2020-10-10 13:17:58 +0200
commitfdb9bba6c88a4fd8981c30bf8ea06aa0709db45e (patch)
treec8f0729351ebeb74622c2537e4e70075afc6bbfd
parent4e9154b12ea3ddd9da32d2a4d1ebc30f13678c59 (diff)
Dynamic question loading
-rw-r--r--profile/index.js12
-rw-r--r--profile/public/index.html6
-rw-r--r--profile/public/script.js14
-rw-r--r--profile/public/style.css39
4 files changed, 63 insertions, 8 deletions
diff --git a/profile/index.js b/profile/index.js
index 4bfc1f7..2f1cb50 100644
--- a/profile/index.js
+++ b/profile/index.js
@@ -2,14 +2,16 @@ const express = require("express");
const db = require("../db");
const app = express.Router();
-app.use("/", express.static(__dirname + "/public/"));
+app.use("/", express.static(__dirname + "/public"));
// Basic API
app.get("/api/user", async (req, res) => {});
app.get("/api/questions", async (req, res) => {
const questions = await db.query("SELECT id, question FROM profile_questions");
- const answers = await db.query("SELECT answer, question_id FROM profile_answers WHERE user_id = ?", [req.session.uid]);
+ const answers = await db.query("SELECT answer, question_id FROM profile_answers WHERE user_id = ?", [
+ req.session.uid,
+ ]);
for (const answer of answers) {
const qid = questions.findIndex((question) => question.id === answer.question_id);
@@ -18,7 +20,9 @@ app.get("/api/questions", async (req, res) => {
res.json(questions);
});
-app.post("/api/add", async (req, res) => {});
+app.post("/api/add", async (req, res) => {
+ await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)");
+});
app.put("/api/update", async (req, res) => {});
@@ -31,4 +35,4 @@ app.put("/api/comment", async (req, res) => {});
app.delete("/api/comment", async (req, res) => {});
-module.exports = app; \ No newline at end of file
+module.exports = app;
diff --git a/profile/public/index.html b/profile/public/index.html
index 30f9de8..5fcee74 100644
--- a/profile/public/index.html
+++ b/profile/public/index.html
@@ -21,14 +21,12 @@
<fieldset>
<legend>Steckbrief</legend>
- <!-- TODO: Insert stuff lel -->
-
<!-- TODO: Consider autosave -->
<button type="submit" class="pure-button pure-button-primary">Wohooo</button>
</fieldset>
</form>
</main>
- <script src="js/script.js"></script>
+ <script src="script.js"></script>
</body>
-</html> \ No newline at end of file
+</html>
diff --git a/profile/public/script.js b/profile/public/script.js
index e69de29..7c72b69 100644
--- a/profile/public/script.js
+++ b/profile/public/script.js
@@ -0,0 +1,14 @@
+const fs = document.querySelector("fieldset");
+
+function appendQuestions(question) {
+ const field = document.createElement("input");
+ field.name = question.id;
+ field.value = question.answer ?? "";
+ field.placeholder = question.question;
+ fs.insertBefore(field, fs.querySelector("button"));
+}
+
+fetch("api/questions")
+ .then((response) => response.json())
+ .then((response) => response.forEach(appendQuestions))
+ .catch(console.error);
diff --git a/profile/public/style.css b/profile/public/style.css
index e69de29..2e7b945 100644
--- a/profile/public/style.css
+++ b/profile/public/style.css
@@ -0,0 +1,39 @@
+html,
+body {
+ padding: 0;
+ margin: 0;
+ height: 100%;
+ width: 100%;
+ background-color: #eec0c6;
+ background-image: linear-gradient(315deg, #eec0c6 0%, #7ee8fa 74%);
+}
+
+div {
+ background: white;
+}
+
+main {
+ position: absolute;
+ max-height: 80%;
+ overflow-y: auto;
+ width: 30%;
+ left: 50%;
+ top: 50%;
+ -webkit-transform: translate(-50%, -50%);
+ transform: translate(-50%, -50%);
+ padding: 20px;
+ border-radius: 10px;
+ background: white;
+}
+
+input,
+button,
+select {
+ width: 100%;
+}
+
+@media only screen and (max-width: 600px) {
+ main {
+ width: calc(100% - 50px);
+ }
+}