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(-) 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 ; } - const questionsMap: Map = new Map(); - form.questions.map((question, index) => { - questionsMap.set(question.id, ()} scroll_ref={createRef()} question={question} public_state={new Map()} key={index + Date.now()}/>); + const questions = form.questions.map((question, index) => { + return ()} scroll_ref={createRef()} 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 =
This form is now closed. You will not be able to submit your response.
; } - const questions: JSX.Element[] = []; - questionsMap.forEach(val => questions.push(val)); - return (
-- cgit v1.2.3