From db9bc59c8b92141e1783629a345a6f7220417123 Mon Sep 17 00:00:00 2001
From: ks129 <45097959+ks129@users.noreply.github.com>
Date: Tue, 19 Jan 2021 15:35:07 +0200
Subject: Un-refactor map to array for questions

---
 src/pages/FormPage.tsx | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

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

diff --git a/src/pages/FormPage.tsx b/src/pages/FormPage.tsx
index dc023a6..b4a8903 100644
--- a/src/pages/FormPage.tsx
+++ b/src/pages/FormPage.tsx
@@ -169,16 +169,15 @@ function FormPage(): JSX.Element {
         return <Loading/>;
     }
 
-    const questionsMap: Map<string, JSX.Element> = new Map();
-    form.questions.map((question, index) => {
-        questionsMap.set(question.id, <RenderedQuestion ref={createRef<RenderedQuestion>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>);
+    const questions = form.questions.map((question, index) => {
+        return <RenderedQuestion ref={createRef<RenderedQuestion>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>;
     });
 
     async function handleSubmit(event: SyntheticEvent) {
         event.preventDefault();
         // Client-side required validation
-        const invalidFieldIds: string[] = [];
-        questionsMap.forEach((prop, id) => {
+        const invalidFieldIds: number[] = [];
+        questions.forEach((prop, i) => {
             const question: Question = prop.props.question;
             if (!question.required) {
                 return;
@@ -187,12 +186,12 @@ function FormPage(): JSX.Element {
             prop.ref.current.validateField();
             // In case when field is invalid, add this to invalid fields list.
             if (prop.props.public_state.get("valid") === false) {
-                invalidFieldIds.push(id);
+                invalidFieldIds.push(i);
             }
         });
 
         if (invalidFieldIds.length) {
-            const firstErrored = questionsMap.get(invalidFieldIds[0]);
+            const firstErrored = questions[invalidFieldIds[0]];
             if (firstErrored !== undefined) {
                 firstErrored.props.scroll_ref.current.scrollIntoView({ behavior: "smooth", block: "center" });
             }
@@ -200,7 +199,7 @@ function FormPage(): JSX.Element {
         }
 
         const answers = {};
-        questionsMap.forEach(prop => {
+        questions.forEach(prop => {
             const question = prop.props.question;
             const options: string | string[] = prop.props.question.data["options"];
 
@@ -243,9 +242,6 @@ function FormPage(): JSX.Element {
         closed_header = <div css={closedHeaderStyles}>This form is now closed. You will not be able to submit your response.</div>;
     }
 
-    const questions: JSX.Element[] = [];
-    questionsMap.forEach(val => questions.push(val));
-
     return (
         <div>
             <HeaderBar title={form.name} description={form.description}/>
-- 
cgit v1.2.3

