From 7db4972038dd1c27a75d50cd539f7d3c79e51bee Mon Sep 17 00:00:00 2001
From: ks129 <45097959+ks129@users.noreply.github.com>
Date: Tue, 19 Jan 2021 15:29:49 +0200
Subject: Implement form submitting

---
 src/pages/FormPage.tsx | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

(limited to 'src/pages/FormPage.tsx')

diff --git a/src/pages/FormPage.tsx b/src/pages/FormPage.tsx
index 06d20d6..dc023a6 100644
--- a/src/pages/FormPage.tsx
+++ b/src/pages/FormPage.tsx
@@ -13,7 +13,8 @@ import ScrollToTop from "../components/ScrollToTop";
 import { Form, FormFeatures, getForm } from "../api/forms";
 import colors from "../colors";
 import { unselectable }  from "../commonStyles";
-import { Question } from "../api/question";
+import { Question, QuestionType } from "../api/question";
+import ApiClient from "../api/client";
 
 
 interface PathParams {
@@ -173,7 +174,7 @@ function FormPage(): JSX.Element {
         questionsMap.set(question.id, <RenderedQuestion ref={createRef<RenderedQuestion>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>);
     });
 
-    function handleSubmit(event: SyntheticEvent) {
+    async function handleSubmit(event: SyntheticEvent) {
         event.preventDefault();
         // Client-side required validation
         const invalidFieldIds: string[] = [];
@@ -198,15 +199,41 @@ function FormPage(): JSX.Element {
             return;
         }
 
+        const answers = {};
         questionsMap.forEach(prop => {
             const question = prop.props.question;
+            const options: string | string[] = prop.props.question.data["options"];
 
             // TODO: Parse input from each question, and submit
             switch (question.type) {
+                case QuestionType.Section:
+                    answers[question.id] = false;
+                    break;
+
+                case QuestionType.Code:
+                    answers[question.id] = "";
+                    break;
+
+                case QuestionType.Checkbox:
+                    if (typeof options !== "string") {
+                        const keys: Map<string, string> = new Map();
+                        options.forEach((val, index) => {
+                            keys.set(val, `${("000" + index).slice(-4)}. ${val}`);
+                        });
+                        const pairs = {};
+                        keys.forEach((val, key) => {
+                            pairs[key] = !!prop.props.public_state.get(val);
+                        });
+                        answers[question.id] = pairs;
+                    }
+                    break;
+
                 default:
-                    console.log(question.id, prop.props.public_state);
+                    answers[question.id] = prop.props.public_state.get("value");
             }
         });
+
+        await ApiClient.post(`forms/submit/${id}`, {response: answers});
     }
 
     const open: boolean = form.features.includes(FormFeatures.Open);
-- 
cgit v1.2.3

