From c750261b12144e3448b36be64e48695403411867 Mon Sep 17 00:00:00 2001
From: MrFry <mrfry@airmail.cc>
Date: Mon, 27 Jan 2020 09:46:09 +0100
Subject: [PATCH] Safe ID getting, question xhttp get refactor

---
 stable.user.js | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/stable.user.js b/stable.user.js
index dc09b10..948ca67 100755
--- a/stable.user.js
+++ b/stable.user.js
@@ -62,7 +62,7 @@
 
   // forcing pages for testing. unless you test, do not set these to true!
   // only one of these should be true for testing
-  const forceTestPage = true
+  const forceTestPage = false
   const forceResultPage = false
   const forceDefaultPage = false
   const logElementGetting = false
@@ -963,6 +963,7 @@
   function ShowSaveQuizDialog (sendResult, sentData, newQuestions) {
     // FIXME: normal string building with localisation :/
     var msg = ''
+    // TODO: '0 új kérdés'
     if (sendResult) {
       msg = 'Kérdések elküldve, katt az elküldött adatokért. ' + newQuestions + ' új kérdés'
     } else {
@@ -1605,10 +1606,10 @@
     }
   }
 
-  function SafeGetElementById (id, todo) {
+  function SafeGetElementById (id, next) {
     let element = document.getElementById(id)
     if (element) {
-      todo(element)
+      next(element)
     } else {
       Log(`Unable to safe get element by id: ${id}`)
     }
@@ -1662,7 +1663,20 @@
         url: url,
         onload: function (response) {
           try {
-            resolve(JSON.parse(response.responseText))
+            let res = JSON.parse(response.responseText)
+            // FIXME: check if res is a valid answer array
+            // res.json({
+            //   result: r,
+            //   success: true
+            // })
+            // ERROR:
+            // res.json({
+            //   message: `Invalid question :(`,
+            //   result: [],
+            //   recievedData: JSON.stringify(req.query),
+            //   success: false
+            // })
+            resolve(res.result)
           } catch (e) {
             reject(new Error('json parse error'))
           }