diff options
author | 2021-01-19 15:35:07 +0200 | |
---|---|---|
committer | 2021-01-19 15:35:07 +0200 | |
commit | db9bc59c8b92141e1783629a345a6f7220417123 (patch) | |
tree | 9824b25214ecc4cf0c9fbb1deee24fc70ac054b1 /src | |
parent | Implement form submitting (diff) |
Un-refactor map to array for questions
Diffstat (limited to 'src')
-rw-r--r-- | src/pages/FormPage.tsx | 18 |
1 files 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 <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}/> |