From cbc846b1be3d8da9e11bd260073549e731849758 Mon Sep 17 00:00:00 2001
From: ks129 <45097959+ks129@users.noreply.github.com>
Date: Mon, 18 Jan 2021 20:20:47 +0200
Subject: Simplify required checking onBlur function

---
 src/components/Question.tsx | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

(limited to 'src/components/Question.tsx')

diff --git a/src/components/Question.tsx b/src/components/Question.tsx
index 45a030b..12cde5d 100644
--- a/src/components/Question.tsx
+++ b/src/components/Question.tsx
@@ -49,35 +49,33 @@ class RenderedQuestion extends React.Component<QuestionProp> {
 
     blurHandler(event: FocusEvent<HTMLInputElement | HTMLTextAreaElement | HTMLDivElement>): void {
         if (this.props.question.required) {
+            let invalid = false;
             switch (this.props.question.type) {
                 case QuestionType.ShortText:
                     if (event.target.value === "") {
-                        this.setPublicState("error", "Field must be filled.");
-                        this.setPublicState("valid", false);
-                    } else {
-                        this.setPublicState("valid", true);
-                        this.setPublicState("error", "");
+                        invalid = true;
                     }
                     break;
                 
                 case QuestionType.TextArea:
                     if (event.target.value === "") {
-                        this.setPublicState("error", "Field must be filled.");
-                        this.setPublicState("valid", false);
-                    } else {
-                        this.setPublicState("valid", true);
-                        this.setPublicState("error", "");
+                        invalid = true;
                     }
                     break;
                 
                 case QuestionType.Select:
                     if (!this.props.public_state.get("value")) {
-                        this.setPublicState("error", "Field must be filled.");
-                        this.setPublicState("valid", false);
-                    } else {
-                        this.setPublicState("error", "");
-                        this.setPublicState("valid", true);
+                        invalid = true;
                     }
+                    break;
+            }
+
+            if (invalid) {
+                this.setPublicState("error", "Field must be filled.");
+                this.setPublicState("valid", false);
+            } else {
+                this.setPublicState("error", "");
+                this.setPublicState("valid", true);
             }
         }
     }
-- 
cgit v1.2.3

