Browse Source

complete questionnaire

master
DemiSel 2 years ago
parent
commit
7fa7ab5494

+ 31
- 0
backend/src/main/java/api/JsonKeys.java View File

1
+package api;
2
+
3
+import com.github.cliftonlabs.json_simple.JsonKey;
4
+
5
+public enum JsonKeys implements JsonKey {
6
+    KEY_QUESTION("question"),
7
+    KEY_QUESTIONS("questions"),
8
+    KEY_ANSWER("answer"),
9
+    KEY_ANSWERS("answers"),
10
+    KEY_DESCRIPTION("description"),
11
+    KEY_NAME("name"),
12
+    KEY_ID("id");
13
+
14
+
15
+    private final Object value;
16
+
17
+    JsonKeys(Object iValue)
18
+    {
19
+        value = iValue;
20
+    }
21
+
22
+    @Override
23
+    public String getKey() {
24
+        return (String) value;
25
+    }
26
+
27
+    @Override
28
+    public Object getValue() {
29
+        return value;
30
+    }
31
+}

+ 15
- 6
backend/src/main/java/api/ResponseHandler.java View File

6
 import com.sun.net.httpserver.HttpHandler;
6
 import com.sun.net.httpserver.HttpHandler;
7
 import controller.ModelController;
7
 import controller.ModelController;
8
 import controller.QuestionnaireController;
8
 import controller.QuestionnaireController;
9
+import controller.SessionController;
9
 
10
 
10
 import java.io.IOException;
11
 import java.io.IOException;
11
 import java.io.OutputStream;
12
 import java.io.OutputStream;
24
             // parse request body : either empty or a JSON Object
25
             // parse request body : either empty or a JSON Object
25
             // ---------------------------------
26
             // ---------------------------------
26
             if (iBody.length() > 0) {
27
             if (iBody.length() > 0) {
27
-                Jsoner.deserialize(iBody, vRequestJson);
28
+                vRequestJson = Jsoner.deserialize(iBody, vRequestJson);
28
             }
29
             }
29
 
30
 
30
             // ---------------------------------
31
             // ---------------------------------
34
                 case "GET":
35
                 case "GET":
35
                     switch (iPath.length) {
36
                     switch (iPath.length) {
36
                         case 0:
37
                         case 0:
37
-                            vResponseObject = iController.getAllEntries(vRequestJson); break;
38
+                            vResponseObject = iController.get(vRequestJson); break;
38
                         case 1:
39
                         case 1:
39
                             vResponseObject = iController.getEntriesForId(iPath[0], vRequestJson); break;
40
                             vResponseObject = iController.getEntriesForId(iPath[0], vRequestJson); break;
40
                         default: throw new Exception(String.format("GET with %d subdirectories not implemented", iPath.length));
41
                         default: throw new Exception(String.format("GET with %d subdirectories not implemented", iPath.length));
61
             }
62
             }
62
         }catch(Exception vex)
63
         }catch(Exception vex)
63
         {
64
         {
65
+            vex.printStackTrace();
64
             vResponseObject.put("error",vex.getMessage());
66
             vResponseObject.put("error",vex.getMessage());
65
         }
67
         }
66
 
68
 
75
         String[] vRequestURI = iExchange.getRequestURI().getPath().split("/");
77
         String[] vRequestURI = iExchange.getRequestURI().getPath().split("/");
76
         String vRequestMethod = iExchange.getRequestMethod();
78
         String vRequestMethod = iExchange.getRequestMethod();
77
         String vRequestBody = new String(iExchange.getRequestBody().readAllBytes());
79
         String vRequestBody = new String(iExchange.getRequestBody().readAllBytes());
80
+        iExchange.getResponseHeaders().set("Content-Type","application/json; charset=utf-8");
81
+        iExchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
82
+        iExchange.getResponseHeaders().set("Access-Control-Allow-Methods", "*");
83
+        iExchange.getResponseHeaders().set("Access-Control-Allow-Headers", "*");
78
 
84
 
79
         try {
85
         try {
80
             if(vRequestURI.length < 1)
86
             if(vRequestURI.length < 1)
84
                 case "questionnaire":
90
                 case "questionnaire":
85
                     vController = new QuestionnaireController();
91
                     vController = new QuestionnaireController();
86
                     break;
92
                     break;
93
+                case "session":
94
+                    vController = new SessionController();
95
+                    break;
87
                 default:
96
                 default:
88
                     throw new Exception(String.format("Unknown path '%s'", vRequestURI[1]));
97
                     throw new Exception(String.format("Unknown path '%s'", vRequestURI[1]));
89
             }
98
             }
90
 
99
 
91
             vRequestAnswerBytes = getResponse(vRequestMethod, Arrays.copyOfRange(vRequestURI,2, vRequestURI.length), vRequestBody, vController).toJson().getBytes(StandardCharsets.UTF_8);
100
             vRequestAnswerBytes = getResponse(vRequestMethod, Arrays.copyOfRange(vRequestURI,2, vRequestURI.length), vRequestBody, vController).toJson().getBytes(StandardCharsets.UTF_8);
92
-            iExchange.getResponseHeaders().set("Content-Type","application/json; charset=utf-8");
93
-            iExchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
94
             iExchange.sendResponseHeaders(200, vRequestAnswerBytes.length);
101
             iExchange.sendResponseHeaders(200, vRequestAnswerBytes.length);
95
         }
102
         }
96
         catch(Exception vex){
103
         catch(Exception vex){
104
+            JsonObject vErrorJsonObect = new JsonObject();
105
+            vErrorJsonObect.put("error", vex.getMessage());
106
+            vRequestAnswerBytes = vErrorJsonObect.toJson().getBytes(StandardCharsets.UTF_8);
97
             vex.printStackTrace();
107
             vex.printStackTrace();
98
-            iExchange.sendResponseHeaders(405, vex.getMessage().getBytes(StandardCharsets.UTF_8).length);
99
-            vRequestAnswerBytes = vex.getMessage().getBytes(StandardCharsets.UTF_8);
108
+            iExchange.sendResponseHeaders(500, vRequestAnswerBytes.length);
100
             }
109
             }
101
 
110
 
102
         vOutputStream.write(vRequestAnswerBytes);
111
         vOutputStream.write(vRequestAnswerBytes);

+ 1
- 1
backend/src/main/java/controller/ModelController.java View File

4
 
4
 
5
 public abstract class ModelController {
5
 public abstract class ModelController {
6
 
6
 
7
-    public JsonObject getAllEntries(JsonObject iBody) throws Exception{
7
+    public JsonObject get(JsonObject iBody) throws Exception{
8
         throw new Exception("Not implemented");
8
         throw new Exception("Not implemented");
9
     }
9
     }
10
 
10
 

+ 1
- 1
backend/src/main/java/controller/QuestionnaireController.java View File

5
 
5
 
6
 public class QuestionnaireController extends ModelController {
6
 public class QuestionnaireController extends ModelController {
7
 
7
 
8
-    public JsonObject getAllEntries(JsonObject iJsonObject)
8
+    public JsonObject get(JsonObject iJsonObject)
9
     {
9
     {
10
         return Questionnaire.getTheOnlyExistingQuestionnaire().asJsonObject();
10
         return Questionnaire.getTheOnlyExistingQuestionnaire().asJsonObject();
11
     }
11
     }

+ 38
- 0
backend/src/main/java/controller/SessionController.java View File

1
+package controller;
2
+
3
+import com.github.cliftonlabs.json_simple.JsonKey;
4
+import com.github.cliftonlabs.json_simple.JsonObject;
5
+import model.questionnaire.Answer;
6
+import model.questionnaire.Question;
7
+import model.questionnaire.Session;
8
+import java.util.UUID;
9
+
10
+import static api.JsonKeys.KEY_ANSWER;
11
+import static api.JsonKeys.KEY_QUESTION;
12
+
13
+public class SessionController extends ModelController {
14
+
15
+    public JsonObject get(JsonObject iJsonObject) {
16
+        return new Session().asJsonObject();
17
+    }
18
+
19
+    public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
20
+        Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
21
+        if(null == vSession)
22
+            throw new Exception(String.format("No session for UUID %s", iID));
23
+
24
+        String vQuestionId = iBody.getString(KEY_QUESTION);
25
+        String vAnswerId   = iBody.getString(KEY_ANSWER);
26
+        Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
27
+        if(null == vQuestion)
28
+            throw new Exception(String.format("No question for UUID %s",vQuestionId));
29
+        Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
30
+        if(null == vAnswer)
31
+            throw new Exception(String.format("No answer at index %d", vAnswerId));
32
+
33
+        vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
34
+        System.out.println(vSession.asJsonObject().toString());
35
+        return new JsonObject();
36
+    }
37
+
38
+}

+ 8
- 5
backend/src/main/java/model/questionnaire/Answer.java View File

10
 import java.nio.charset.StandardCharsets;
10
 import java.nio.charset.StandardCharsets;
11
 import java.util.UUID;
11
 import java.util.UUID;
12
 
12
 
13
+import static api.JsonKeys.KEY_DESCRIPTION;
14
+import static api.JsonKeys.KEY_ID;
15
+
13
 public class Answer implements Jsonable {
16
 public class Answer implements Jsonable {
14
 
17
 
15
-    private UUID    fUUID;
18
+    private UUID fId;
16
     private float   fWeight;
19
     private float   fWeight;
17
     private Trait   fTrait;
20
     private Trait   fTrait;
18
     private String  fDescription;
21
     private String  fDescription;
20
 
23
 
21
     public Answer(String iDescription, Trait iTrait, float iWeight)
24
     public Answer(String iDescription, Trait iTrait, float iWeight)
22
     {
25
     {
23
-        fUUID           = UUID.randomUUID();    // I feel like I should check this is truly unique among answers
26
+        fId = UUID.randomUUID();    // I feel like I should check this is truly unique among answers
24
         fWeight         = iWeight;
27
         fWeight         = iWeight;
25
         fTrait          = iTrait;
28
         fTrait          = iTrait;
26
         fDescription    = iDescription;
29
         fDescription    = iDescription;
28
 
31
 
29
     public Trait getTrait() { return fTrait; }
32
     public Trait getTrait() { return fTrait; }
30
     public float getWeight() { return fWeight; }
33
     public float getWeight() { return fWeight; }
31
-
34
+    public UUID getId() { return fId; }
32
 
35
 
33
     @Override
36
     @Override
34
     public String toJson() {
37
     public String toJson() {
45
     @Override
48
     @Override
46
     public void toJson(Writer writable) throws IOException {
49
     public void toJson(Writer writable) throws IOException {
47
         final JsonObject vResult = new JsonObject();
50
         final JsonObject vResult = new JsonObject();
48
-        vResult.put("description", fDescription);
49
-        vResult.put("id", fUUID.toString());
51
+        vResult.put(KEY_DESCRIPTION, fDescription);
52
+        vResult.put(KEY_ID, fId.toString());
50
         writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
53
         writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
51
     }
54
     }
52
 }
55
 }

+ 25
- 2
backend/src/main/java/model/questionnaire/Question.java View File

7
 import java.io.StringWriter;
7
 import java.io.StringWriter;
8
 import java.io.Writer;
8
 import java.io.Writer;
9
 import java.nio.charset.StandardCharsets;
9
 import java.nio.charset.StandardCharsets;
10
+import java.util.HashMap;
10
 import java.util.List;
11
 import java.util.List;
12
+import java.util.UUID;
13
+
14
+import static api.JsonKeys.*;
11
 
15
 
12
 public class Question implements Jsonable {
16
 public class Question implements Jsonable {
13
 
17
 
18
+    private static final HashMap<UUID, Question> sQuestions = new HashMap<>();
19
+    private UUID fId;
14
     private List<Answer> fAnswers;
20
     private List<Answer> fAnswers;
15
     private String fName;
21
     private String fName;
16
 
22
 
17
     public Question(String iName, List<Answer> iAnswers)
23
     public Question(String iName, List<Answer> iAnswers)
18
     {
24
     {
25
+        fId = UUID.randomUUID();
19
         fName = iName;
26
         fName = iName;
20
         fAnswers = iAnswers;
27
         fAnswers = iAnswers;
28
+        sQuestions.put(fId, this);
29
+    }
30
+
31
+
32
+    public static Question getForUUID(UUID iId) {
33
+        return sQuestions.get(iId);
21
     }
34
     }
22
 
35
 
36
+
37
+    public Answer getAnswerForUUID(UUID iId) {
38
+        return fAnswers.stream().filter( answer -> answer.getId().equals(iId)).findFirst().get();
39
+    }
40
+
41
+    // ------------------------------------------------------------------
42
+    // Jsonable Implementation
43
+    // ------------------------------------------------------------------
44
+
23
     @Override
45
     @Override
24
     public String toJson() {
46
     public String toJson() {
25
         final StringWriter writable = new StringWriter();
47
         final StringWriter writable = new StringWriter();
35
     @Override
57
     @Override
36
     public void toJson(Writer writable) throws IOException {
58
     public void toJson(Writer writable) throws IOException {
37
         JsonObject vResult = new JsonObject();
59
         JsonObject vResult = new JsonObject();
38
-        vResult.put("name", fName);
39
-        vResult.put("answers", fAnswers);
60
+        vResult.put(KEY_NAME, fName);
61
+        vResult.put(KEY_ANSWERS, fAnswers);
62
+        vResult.put(KEY_ID, fId.toString());
40
         writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
63
         writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
41
     }
64
     }
42
 }
65
 }

+ 5
- 2
backend/src/main/java/model/questionnaire/Questionnaire.java View File

5
 import java.util.ArrayList;
5
 import java.util.ArrayList;
6
 import java.util.List;
6
 import java.util.List;
7
 
7
 
8
+import static api.JsonKeys.KEY_NAME;
9
+import static api.JsonKeys.KEY_QUESTIONS;
10
+
8
 public class Questionnaire{
11
 public class Questionnaire{
9
 
12
 
10
     private static List<Questionnaire> sQuestionnaires = new ArrayList<>();
13
     private static List<Questionnaire> sQuestionnaires = new ArrayList<>();
24
 
27
 
25
     public JsonObject asJsonObject(){
28
     public JsonObject asJsonObject(){
26
         JsonObject vResult = new JsonObject();
29
         JsonObject vResult = new JsonObject();
27
-        vResult.put("name", fName);
28
-        vResult.put("questions",fQuestions);
30
+        vResult.put(KEY_NAME, fName);
31
+        vResult.put(KEY_QUESTIONS,fQuestions);
29
         return vResult;
32
         return vResult;
30
     }
33
     }
31
 }
34
 }

+ 48
- 3
backend/src/main/java/model/questionnaire/Session.java View File

1
 package model.questionnaire;
1
 package model.questionnaire;
2
 
2
 
3
+import com.github.cliftonlabs.json_simple.JsonObject;
4
+import com.github.cliftonlabs.json_simple.Jsonable;
3
 import model.personnality.Personality;
5
 import model.personnality.Personality;
4
 
6
 
7
+import java.io.IOException;
8
+import java.io.StringWriter;
9
+import java.io.Writer;
10
+import java.nio.charset.StandardCharsets;
11
+import java.util.HashMap;
5
 import java.util.UUID;
12
 import java.util.UUID;
6
 
13
 
7
-public class Session {
14
+import static api.JsonKeys.KEY_ID;
8
 
15
 
16
+public class Session implements Jsonable {
17
+
18
+    private static HashMap<UUID, Session> sSessions = new HashMap<UUID, Session>();
9
     private UUID fId;
19
     private UUID fId;
10
     private SessionAnswers fAnswers;
20
     private SessionAnswers fAnswers;
11
     private Personality    fPersonality;
21
     private Personality    fPersonality;
12
 
22
 
13
-    public Session(UUID iId){
14
-        fId = iId;
23
+    public Session(){
24
+        fId = UUID.randomUUID();
15
         fAnswers = new SessionAnswers();
25
         fAnswers = new SessionAnswers();
16
         fPersonality = new Personality();
26
         fPersonality = new Personality();
27
+        sSessions.put(fId, this);
17
     }
28
     }
18
 
29
 
30
+
19
     public SessionAnswers getSessionAnswers(){ return fAnswers; }
31
     public SessionAnswers getSessionAnswers(){ return fAnswers; }
20
 
32
 
33
+    public static Session getSessionForUUID(UUID iId){
34
+        return sSessions.get(iId);
35
+    }
36
+
37
+
38
+    // ------------------------------------------------------------------
39
+    // Jsonable Implementation
40
+    // ------------------------------------------------------------------
41
+    public JsonObject asJsonObject() {
42
+        JsonObject vResult = new JsonObject();
43
+        vResult.put(KEY_ID, fId.toString());
44
+
45
+        return vResult;
46
+    }
47
+
48
+
49
+    @Override
50
+    public String toJson() {
51
+        final StringWriter writable = new StringWriter();
52
+        try{
53
+            this.toJson(writable);
54
+        }catch(final IOException caught)
55
+        {
56
+            caught.printStackTrace();
57
+        }
58
+        return writable.toString();
59
+    }
60
+
61
+
62
+    @Override
63
+    public void toJson(Writer writable) throws IOException {
64
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
65
+    }
21
 
66
 
22
 }
67
 }

+ 1
- 0
backend/src/main/java/model/questionnaire/SessionAnswers.java View File

14
     private HashMap<Trait, Float> fTraits;
14
     private HashMap<Trait, Float> fTraits;
15
 
15
 
16
     public SessionAnswers() {
16
     public SessionAnswers() {
17
+        fTraits = new HashMap<>();
17
         fAnswers = new HashMap<>();
18
         fAnswers = new HashMap<>();
18
     }
19
     }
19
 
20
 

+ 30
- 3
frontend/src/App.js View File

1
 import './App.css';
1
 import './App.css';
2
 import React from 'react';
2
 import React from 'react';
3
 import Questionnaire from './component/questionnaire';
3
 import Questionnaire from './component/questionnaire';
4
+import url from './server';
5
+
4
 
6
 
5
 function App() {
7
 function App() {
6
   const [questionnaireStarted, onStartQuestionnaire] = React.useState(false);
8
   const [questionnaireStarted, onStartQuestionnaire] = React.useState(false);
9
+  const [sessionIdReceived, onSessionIdReceived] = React.useState(null);
10
+  const [sessionIdRequested, onSessionIdRequested] = React.useState(false);
11
+  const [errorOccured, onErrorOccured] = React.useState(null);
12
+  //-------------------------------
13
+  // Fetch session id
14
+  //-------------------------------
15
+  if(null === sessionIdReceived && !sessionIdRequested)
16
+  {
17
+    onSessionIdRequested(true);
18
+    fetch(url+"/session")
19
+        .then(response => response.json())
20
+        .then(result => {
21
+            if('error' in result)
22
+              onErrorOccured(result.error);
23
+            else
24
+              onSessionIdReceived(result.id);
25
+        })
26
+        .catch( e => {
27
+            console.log(e);
28
+        });
29
+  }
7
 
30
 
8
   return (
31
   return (
9
     <div>
32
     <div>
11
         Are you an introvert or an extrovert ?
34
         Are you an introvert or an extrovert ?
12
       </h1>
35
       </h1>
13
       <div>
36
       <div>
14
-        Take this ✨ totally legit test ✨ and find out !
37
+        Take this <i>✨ totally legit personality test ✨</i> and find out !
15
       </div>
38
       </div>
16
       {
39
       {
17
-        !questionnaireStarted && 
40
+        null === sessionIdReceived &&
41
+        <div> {null === errorOccured ? "Contacting server..." : "A server error occured 😨 : "+errorOccured} </div>
42
+      }
43
+      {
44
+        !questionnaireStarted && null!=sessionIdReceived &&
18
         <div
45
         <div
19
         onClick={() => onStartQuestionnaire(true)}>
46
         onClick={() => onStartQuestionnaire(true)}>
20
           Start
47
           Start
21
         </div>
48
         </div>
22
       }
49
       }
23
       { questionnaireStarted &&
50
       { questionnaireStarted &&
24
-        <Questionnaire/>}
51
+        <Questionnaire sessionId={sessionIdReceived}/>}
25
     </div>
52
     </div>
26
   );
53
   );
27
 }
54
 }

+ 31
- 7
frontend/src/component/question.js View File

1
 import React from 'react';
1
 import React from 'react';
2
 import url from '../server';
2
 import url from '../server';
3
+import Questionnaire from './questionnaire';
3
 
4
 
4
 function Question(props){
5
 function Question(props){
5
-    let question = props.questions[props.index];
6
-    const [selectedAnswer, onSelectAnswer] = React.useState(question.answer);
6
+    let current_question = props.questions[props.index];
7
+    const [selectedAnswer, onSelectAnswer] = React.useState(current_question.answer);
7
     let question_answers = [];
8
     let question_answers = [];
8
 
9
 
9
     //-------------------------------
10
     //-------------------------------
10
     // Build question form
11
     // Build question form
11
     //-------------------------------
12
     //-------------------------------
12
-    question.answers.forEach(answer => {
13
+    current_question.answers.forEach(answer => {
13
         let dom_id = "answer"+answer.id
14
         let dom_id = "answer"+answer.id
14
         question_answers.push(
15
         question_answers.push(
15
             <div key={dom_id}>
16
             <div key={dom_id}>
21
     
22
     
22
     return (
23
     return (
23
     <div>
24
     <div>
24
-        <div> {question.name} </div>
25
+        <div> {current_question.name} </div>
25
         <div onChange={(value) => onSelectAnswer(value.target.value)}>
26
         <div onChange={(value) => onSelectAnswer(value.target.value)}>
26
         {question_answers}
27
         {question_answers}
27
         </div>
28
         </div>
28
         <div
29
         <div
29
             onClick={() => {
30
             onClick={() => {
31
+                //-------------------------------
32
+                // POST question answer
33
+                //-------------------------------
34
+                fetch(url+"/session/"+props.sessionId, {
35
+                    method:'PUT',
36
+                    body: JSON.stringify({
37
+                        question: current_question.id,
38
+                        answer: selectedAnswer
39
+                    }),
40
+                    headers: {"Content-type": "application/json; charset=UTF-8"}
41
+                }) 
42
+                .then(response => response.json())
43
+                .then(result => {
44
+                    if(props.index === props.questions.length - 1)
45
+                    {
46
+                        //-------------------------------
47
+                        // Tell the (parent) questionnaire that the results should be available
48
+                        //-------------------------------
49
+                        props.questionnaireCompleted(true);
50
+                    }
51
+                })
52
+                .catch( e => console.error(e))
53
+
30
                 if(props.index === props.questions.length - 1)
54
                 if(props.index === props.questions.length - 1)
31
                 {
55
                 {
32
-                    question.answer = selectedAnswer;
56
+                    current_question.answer = selectedAnswer;
33
                 }
57
                 }
34
                 else
58
                 else
35
                 {
59
                 {
36
-                    question.answer = selectedAnswer;
60
+                    current_question.answer = selectedAnswer;
37
                     props.moveToQuestion(props.index + 1);
61
                     props.moveToQuestion(props.index + 1);
38
                 }
62
                 }
39
             }}>
63
             }}>
40
-                {props.index < props.questions.length -1 ? "Validate" : "Get Result" }
64
+                {props.index < props.questions.length -1 ? "Validate" : "Get Results" }
41
         </div>
65
         </div>
42
     </div>
66
     </div>
43
     );
67
     );

+ 14
- 3
frontend/src/component/questionnaire.js View File

1
 import React from 'react';
1
 import React from 'react';
2
 import url from '../server';
2
 import url from '../server';
3
 import Question from './question';
3
 import Question from './question';
4
+import QuestionnaireResult from './questionnaire_result';
4
 
5
 
5
-function Questionnaire(){
6
+function Questionnaire(props){
6
     const [currentIndex, onMoveToIndex] = React.useState(0);
7
     const [currentIndex, onMoveToIndex] = React.useState(0);
7
     const [questionnaireDataFetched, onQuestionnaireDataFetched] = React.useState(null);
8
     const [questionnaireDataFetched, onQuestionnaireDataFetched] = React.useState(null);
9
+    const [resultsAvailable, onQuestionnaireCompleted] = React.useState(false);
8
 
10
 
9
     //-------------------------------
11
     //-------------------------------
10
     // Fetch questionnaire
12
     // Fetch questionnaire
14
         fetch(url+"/questionnaire")
16
         fetch(url+"/questionnaire")
15
             .then(response => response.json())
17
             .then(response => response.json())
16
             .then(result => {
18
             .then(result => {
19
+                if('error' in result)
20
+                    console.error(result);
17
                 onQuestionnaireDataFetched(result);
21
                 onQuestionnaireDataFetched(result);
18
             })
22
             })
19
             .catch( e => {
23
             .catch( e => {
20
                 console.log(e);
24
                 console.log(e);
21
-            })
25
+            });
22
     }
26
     }
23
 
27
 
28
+    if(!resultsAvailable)
24
     return (<React.Fragment>
29
     return (<React.Fragment>
25
         {
30
         {
26
             null === questionnaireDataFetched ?
31
             null === questionnaireDataFetched ?
28
             :
33
             :
29
             <Question
34
             <Question
30
                 key={"question_"+currentIndex}   //force throw away to ease radio buttons management 
35
                 key={"question_"+currentIndex}   //force throw away to ease radio buttons management 
36
+                sessionId={props.sessionId}
31
                 questions={questionnaireDataFetched.questions}
37
                 questions={questionnaireDataFetched.questions}
32
                 index={currentIndex}
38
                 index={currentIndex}
33
-                moveToQuestion={onMoveToIndex}/>
39
+                moveToQuestion={onMoveToIndex}
40
+                questionnaireCompleted={onQuestionnaireCompleted}/>
34
         }
41
         }
35
         <div>
42
         <div>
36
         {
43
         {
43
         </div>
50
         </div>
44
         </React.Fragment>
51
         </React.Fragment>
45
     )
52
     )
53
+    else
54
+    return(
55
+        <QuestionnaireResult/>
56
+    )
46
 }
57
 }
47
 
58
 
48
 export default Questionnaire;
59
 export default Questionnaire;

+ 18
- 0
frontend/src/component/questionnaire_result.js View File

1
+import React from 'react';
2
+import url from '../server';
3
+
4
+function QuestionnaireResult(props){
5
+    const [resultsFetched, onResultsFetched] = React.useState(null);
6
+
7
+    if(null === resultsFetched)
8
+    {
9
+        
10
+    }
11
+    return(
12
+        <div>
13
+            Results
14
+        </div>
15
+    )
16
+}
17
+
18
+export default QuestionnaireResult;

Loading…
Cancel
Save