From c2163a858bef1705c72ff741e5b3c8cb3a2f36e2 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 13 Mar 2019 16:45:52 +0200 Subject: [PATCH] Implement some placeholder logic for signup forms in admin --- .../SignupQuestionsWidget.tsx | 94 +++++++++++++++++-- .../SignupCreatePage/SignupCreatePage.tsx | 2 +- tsconfig.json | 2 +- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/components/SignupQuestionsWidget/SignupQuestionsWidget.tsx b/src/components/SignupQuestionsWidget/SignupQuestionsWidget.tsx index 3fb604b..0aa7010 100644 --- a/src/components/SignupQuestionsWidget/SignupQuestionsWidget.tsx +++ b/src/components/SignupQuestionsWidget/SignupQuestionsWidget.tsx @@ -12,9 +12,24 @@ export interface SignupQuestionsWidgetState {} interface Question { id: string; name: string; + type: string; options: string[]; } +interface InputProps { + index: number; + value: string | string[]; + questions: Question[]; + type: string; +} + +const types = [ + "text", + "radiobutton", +]; + +class SignupQuestionError extends Error {} + class SignupQuestionsWidget extends React.Component { onValueChange = (questions: Question[]) => { const { onChange } = this.props; @@ -23,11 +38,13 @@ class SignupQuestionsWidget extends React.Component () => { - const newQuestions = questions.concat([{ + const newRow: Question = { id: shortid.generate(), name: `Question #${questions.length + 1}`, options: [], - }]); + type: "text", + }; + const newQuestions: Question[] = questions.concat([newRow]); this.onValueChange(newQuestions); } @@ -38,12 +55,75 @@ class SignupQuestionsWidget extends React.Component { - return questions.map((q, index) => ( -
- -
+ handleTypeChange = (questions: Question[], index: number) => (event) => { + const val = event.target.value; + questions[index].type = val; + this.onValueChange(questions); + } + + handleRadiobuttonOptionsChange = (questions: Question[], index: number) => (event) => { + const val = event.target.value; + const lst = val.split(",").map(p => p.trimLeft()); + questions[index].options = lst; + this.onValueChange(questions); + } + + renderTextWidget = ({ questions, value, index }: InputProps) => ( + + ) + + renderOptionsWidgetByType = (props: InputProps) => { + const { type, value, questions, index } = props; + if (!types.includes(type)) { + throw new SignupQuestionError(`Qquestion widget type "${type}" not in types array.`); + } + + if (type === "text") { + return null; + } + + else if (type === "radiobutton") { + const lst = value as string[]; + const joinedValue = lst.join(","); + return ; + } + + + throw new SignupQuestionError(`Unrecognized question widget type "${type}"`); + } + + renderTypeSelectWidget = (props: InputProps) => { + const { questions, index, type } = props; + const options = types.map(t => ( + )); + return ( + + ); + } + + renderQuestions = (questions: Question[]) => { + return questions.map((q, index) => { + const nameWidgetProps = { value: q.name, type: "text", questions, index }; + const nameWidget = this.renderTextWidget(nameWidgetProps); + + const optionsWidgetProps = { value: q.options, type: q.type, questions, index }; + const optionsWidget = this.renderOptionsWidgetByType(optionsWidgetProps); + const typeSelectWidget = this.renderTypeSelectWidget(optionsWidgetProps); + return ( +
+ { nameWidget } + { typeSelectWidget } + { optionsWidget } +
+ ); + }); } render() { diff --git a/src/pages/SignupCreatePage/SignupCreatePage.tsx b/src/pages/SignupCreatePage/SignupCreatePage.tsx index 6d6f74c..2d264fb 100644 --- a/src/pages/SignupCreatePage/SignupCreatePage.tsx +++ b/src/pages/SignupCreatePage/SignupCreatePage.tsx @@ -4,7 +4,7 @@ import "./SignupCreatePage.scss"; import { isAuthenticated } from "../../auth"; import Form from "react-jsonschema-form"; import { Tag, getTags } from "../../models/Tag"; -import { createForm, getForm, updateForm, deserializeForm } from "../../models/SignupForm"; +import { createForm, getForm, updateForm } from "../../models/SignupForm"; import DatetimeWidget from "../../components/DatetimeWidget"; import SignupQuestionsWidget from "../../components/SignupQuestionsWidget"; diff --git a/tsconfig.json b/tsconfig.json index 371f7e5..d6dfe84 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "module": "commonjs", "target": "es5", "jsx": "react", - "lib": ["es5", "es6", "dom"], + "lib": ["es5", "es6", "es7", "dom"], "experimentalDecorators": true, "types": [ "node"