diff --git a/truthinquiry/routes/routes_admin.py b/truthinquiry/routes/routes_admin.py index a48ec4b..e20db2b 100644 --- a/truthinquiry/routes/routes_admin.py +++ b/truthinquiry/routes/routes_admin.py @@ -25,6 +25,7 @@ def npc(npc_id): npc_answers.append(answer_list) npc_dict = { + "id": npc_obj.NPC_ID, "name": npc_obj.LOCALE.TEXTS[0].TEXT, "img": npc_obj.NPC_ID, "answers": npc_answers, diff --git a/truthinquiry/routes/routes_api_admin.py b/truthinquiry/routes/routes_api_admin.py index 42a29db..87a052a 100644 --- a/truthinquiry/routes/routes_api_admin.py +++ b/truthinquiry/routes/routes_api_admin.py @@ -121,4 +121,28 @@ def set_places(): db.session.commit() + return {"error": 0} + +@routes_api_admin.route("/setNpc", methods=["GET", "POST"]) +def set_npc(): + input_lang = flask.request.json["lang"] + input_npc = flask.request.json["npc"] + + if input_npc["id"] == None: + npc_obj = Npc(None, None) + db.session.add(npc_obj) + else: + npc_obj = db.session.get(Npc, input_npc["id"]) + + npc_obj.LOCALE.get_text(input_lang, True).TEXT = input_npc["name"] + + for answer_type, input_answer_type in zip(npc_obj.ANSWERS, input_npc["allAnswers"]): + for text in answer_type.LOCALE.get_texts(input_lang): + db.session.delete(text) + for input_answer in input_answer_type["answers"]: + answer_type.LOCALE.TEXTS.append(Text(None, None, input_lang, input_answer["text"])) + + + db.session.commit() + return {"error": 0} \ No newline at end of file diff --git a/truthinquiry/static/js/admin.js b/truthinquiry/static/js/admin.js index 1923bd7..d8e30cb 100644 --- a/truthinquiry/static/js/admin.js +++ b/truthinquiry/static/js/admin.js @@ -92,3 +92,27 @@ function changeLang(){ } +//functions for npc.html + +function saveFormNpc(){ + let data = {} + + data["id"] = npc.querySelector("#npc_id").value; + data["name"] = npc.querySelector("#npc_name").value; + + let allAnswersJson = []; + data["allAnswers"] = allAnswersJson; + + for(let answerTypeNode of npc.querySelectorAll(".answerType")){ + let answersJson = []; + let answerTypeJson = {"answers": answersJson}; + allAnswersJson.push(answerTypeJson); + + for(let answerNode of answerTypeNode.querySelectorAll("input")){ + answersJson.push({"text": answerNode.value}) + } + } + + makeAPIRequest("admin/setNpc", {"npc": data, "lang": "FR"}, {"content": "json"}) +} + diff --git a/truthinquiry/templates/admin/npc.html b/truthinquiry/templates/admin/npc.html index edb233f..3f04ba7 100644 --- a/truthinquiry/templates/admin/npc.html +++ b/truthinquiry/templates/admin/npc.html @@ -4,26 +4,32 @@ NPC + go Back
-
- Npc name: - - -
- -
-

Answers:

- {%for answer_type in npc.get("answers") or []%} -
- {%for answer in answer_type%} - +
+
+ Npc name: + + + +
+ +
+

Answers:

+ {%for answer_type in npc.get("answers") or []%} +
+ {%for answer in answer_type%} + + {%endfor%} +
{%endfor%}
- {%endfor%}
+ +