import * as React from "react"; import Helmet from "react-helmet"; import "./SignUpPage.scss"; import { getForm, SignupForm } from "../../models/SignupForm"; import PageSection from "../../components/PageSection"; import { ColorEnum } from "../../index"; import { Question, optionTypes } from "../../components/SignupQuestionsWidget"; export interface SignUpPageProps { match: { params: { id: number; }, }; } export interface SignUpPageState { signUpForm: SignupForm | null; } class SignUpPage extends React.Component { constructor(props) { super(props); const { staticContext } = props; if (staticContext) { /* The static context is an object that manages promises when rendering on the server. If staticContext exists, that means we have to store all promises in it. Otherwise, operate normally. See server/index.ts. */ if (staticContext.resolutions.getSignUpForm) { const signUpForm = staticContext.resolutions.getSignUpForm as SignupForm; this.state = { signUpForm, }; } else { this.state = { signUpForm: null, }; const promiseSignUpForm = this.fetchSignUp(); staticContext.promises.getSignUpForm = promiseSignUpForm; } } else { this.state = { signUpForm: null, }; this.fetchSignUp(); } } fetchSignUp = (): Promise => { const { match } = this.props; const { id } = match.params; const formPromise = getForm(id); formPromise.then(signUpForm => { this.setState({ signUpForm, }); }); return formPromise; } renderAnswerInput = (question: Question) => { if (!optionTypes.includes(question.type)) { throw new Error("Unknown question type"); } if (question.type === "text") { return ; } if (question.type === "radiobutton") { const options = question.options; return options.map((opt) => ( {opt} )); } } renderQuestion = (question: Question) => { return (
{question.name} {this.renderAnswerInput(question)}
); } renderForm() { const { signUpForm } = this.state; const questions: Question[] = JSON.parse(signUpForm.questions); const content = questions.map(this.renderQuestion); return (

Title: {signUpForm.title}

{content}
); } render() { const { match } = this.props; const { id } = match.params; const { signUpForm } = this.state; const content = signUpForm !== null ? this.renderForm() :
Loading...
; return (
{content}
); } } export default SignUpPage;