aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/FormPage.tsx
blob: b966e84309fe9211885f8858fcfa248fe9f9ddde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** @jsx jsx */
import { jsx, css } from "@emotion/react";
import { Link } from "react-router-dom";

import React, { SyntheticEvent, useEffect, useState } from "react";
import { useParams } from "react-router";

import HeaderBar from "../components/HeaderBar";
import RenderedQuestion from "../components/Question";
import Loading from "../components/Loading";
import ScrollToTop from "../components/ScrollToTop";

import { Form, FormFeatures, getForm } from "../api/forms";


interface PathParams {
    id: string
}

function FormPage(): JSX.Element {
    const { id } = useParams<PathParams>();

    const [form, setForm] = useState<Form>();

    useEffect(() => {
        getForm(id).then(form => {
            setForm(form);
        });
    }, []);

    if (!form) {
        return <Loading/>;
    }

    const questions = form.questions.map((question, index) => {
        return <RenderedQuestion question={question} public_state={new Map()} key={index}/>;
    });

    function handleSubmit(event: SyntheticEvent) {
        questions.forEach(prop => {
            const question = prop.props.question;

            // TODO: Parse input from each question, and submit
            switch (question.type) {
            default:
                console.log(question.id, prop.props.public_state);
            }
        });

        event.preventDefault();
    }

    const open: boolean = form.features.includes(FormFeatures.Open);

    let closed_header = null;
    let submit = null;

    if (open) {
        submit = (
            <div className="submit_form">
                <button form="form" type="submit">Submit</button>
            </div>
        );
    } else {
        closed_header = (
            <div className="closed_header">
                <div>This form is now closed. You will not be able to submit your response.</div>
            </div>
        );
    }


    return (
        <div>
            <HeaderBar title={form.name} description={form.description} key={2}/>
            <div css={css`${require("./css/FormPage.css")};`}>
                <form id="form" onSubmit={handleSubmit} className="unselectable">
                    { closed_header }
                    {questions}
                </form>
                <div className="nav unselectable">
                    <div className={ "nav_buttons" + (open ? "" : " closed") }>
                        <Link to="/" className="return_home">Return Home</Link>
                    </div>
                    <br className="nav_separator"/>
                    { submit }
                </div>
            </div>
            <div css={css`margin-bottom: 10rem`}/>
            <ScrollToTop/>
        </div>
    );
}

export default FormPage;