Implement some placeholder logic for signup forms in admin
This commit is contained in:
@@ -12,9 +12,24 @@ export interface SignupQuestionsWidgetState {}
|
|||||||
interface Question {
|
interface Question {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
type: string;
|
||||||
options: 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<SignupQuestionsWidgetProps, SignupQuestionsWidgetState> {
|
class SignupQuestionsWidget extends React.Component<SignupQuestionsWidgetProps, SignupQuestionsWidgetState> {
|
||||||
onValueChange = (questions: Question[]) => {
|
onValueChange = (questions: Question[]) => {
|
||||||
const { onChange } = this.props;
|
const { onChange } = this.props;
|
||||||
@@ -23,11 +38,13 @@ class SignupQuestionsWidget extends React.Component<SignupQuestionsWidgetProps,
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleNewRowClick = (questions) => () => {
|
handleNewRowClick = (questions) => () => {
|
||||||
const newQuestions = questions.concat([{
|
const newRow: Question = {
|
||||||
id: shortid.generate(),
|
id: shortid.generate(),
|
||||||
name: `Question #${questions.length + 1}`,
|
name: `Question #${questions.length + 1}`,
|
||||||
options: [],
|
options: [],
|
||||||
}]);
|
type: "text",
|
||||||
|
};
|
||||||
|
const newQuestions: Question[] = questions.concat([newRow]);
|
||||||
|
|
||||||
this.onValueChange(newQuestions);
|
this.onValueChange(newQuestions);
|
||||||
}
|
}
|
||||||
@@ -38,12 +55,75 @@ class SignupQuestionsWidget extends React.Component<SignupQuestionsWidgetProps,
|
|||||||
this.onValueChange(questions);
|
this.onValueChange(questions);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderQuestions = (questions: Question[]) => {
|
handleTypeChange = (questions: Question[], index: number) => (event) => {
|
||||||
return questions.map((q, index) => (
|
const val = event.target.value;
|
||||||
<div key={q.id} className="signup-questions-widget-row">
|
questions[index].type = val;
|
||||||
<input type="text" value={ q.name } onChange={this.handleNameInputChange(questions, index)} />
|
this.onValueChange(questions);
|
||||||
</div>
|
}
|
||||||
|
|
||||||
|
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) => (
|
||||||
|
<input type="text" value={value} onChange={this.handleNameInputChange(questions, index)} />
|
||||||
|
)
|
||||||
|
|
||||||
|
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 <input
|
||||||
|
type="text"
|
||||||
|
placeholder="Yes,no,maybe"
|
||||||
|
value={joinedValue}
|
||||||
|
onChange={this.handleRadiobuttonOptionsChange(questions, index)} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
throw new SignupQuestionError(`Unrecognized question widget type "${type}"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderTypeSelectWidget = (props: InputProps) => {
|
||||||
|
const { questions, index, type } = props;
|
||||||
|
const options = types.map(t => (
|
||||||
|
<option key={t} value={t}>{t}</option>
|
||||||
));
|
));
|
||||||
|
return (
|
||||||
|
<select onChange={this.handleTypeChange(questions, index)} value={type} name="type">
|
||||||
|
{ options }
|
||||||
|
</select>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<div key={q.id} className="signup-questions-widget-row">
|
||||||
|
{ nameWidget }
|
||||||
|
{ typeSelectWidget }
|
||||||
|
{ optionsWidget }
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "./SignupCreatePage.scss";
|
|||||||
import { isAuthenticated } from "../../auth";
|
import { isAuthenticated } from "../../auth";
|
||||||
import Form from "react-jsonschema-form";
|
import Form from "react-jsonschema-form";
|
||||||
import { Tag, getTags } from "../../models/Tag";
|
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 DatetimeWidget from "../../components/DatetimeWidget";
|
||||||
import SignupQuestionsWidget from "../../components/SignupQuestionsWidget";
|
import SignupQuestionsWidget from "../../components/SignupQuestionsWidget";
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"target": "es5",
|
"target": "es5",
|
||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"lib": ["es5", "es6", "dom"],
|
"lib": ["es5", "es6", "es7", "dom"],
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"types": [
|
"types": [
|
||||||
"node"
|
"node"
|
||||||
|
|||||||
Reference in New Issue
Block a user