-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mattfreire
committed
Dec 12, 2018
1 parent
58823b3
commit 16d0635
Showing
11 changed files
with
340 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React from "react"; | ||
import { connect } from "react-redux"; | ||
import { Form, Input, Icon, Button, Divider } from "antd"; | ||
import QuestionForm from "./QuestionForm"; | ||
import Hoc from "../hoc/hoc"; | ||
import { createASNT } from "../store/actions/assignments"; | ||
|
||
const FormItem = Form.Item; | ||
|
||
class AssignmentCreate extends React.Component { | ||
state = { | ||
formCount: 1 | ||
}; | ||
|
||
remove = () => { | ||
const { formCount } = this.state; | ||
this.setState({ | ||
formCount: formCount - 1 | ||
}); | ||
}; | ||
|
||
add = () => { | ||
const { formCount } = this.state; | ||
this.setState({ | ||
formCount: formCount + 1 | ||
}); | ||
}; | ||
|
||
handleSubmit = e => { | ||
e.preventDefault(); | ||
this.props.form.validateFields((err, values) => { | ||
if (!err) { | ||
console.log("Received values of form: ", values); | ||
const questions = []; | ||
for (let i = 0; i < values.questions.length; i += 1) { | ||
questions.push({ | ||
title: values.question[i], | ||
choices: values.questions[i].choices.filter(el => el !== null), | ||
answer: values.answers[i] | ||
}); | ||
} | ||
const asnt = { | ||
teacher: this.props.username, | ||
title: values.title, | ||
questions | ||
}; | ||
this.props.createASNT(this.props.token, asnt); | ||
} | ||
}); | ||
}; | ||
|
||
render() { | ||
const { getFieldDecorator } = this.props.form; | ||
const questions = []; | ||
for (let i = 0; i < this.state.formCount; i += 1) { | ||
questions.push( | ||
<Hoc key={i}> | ||
{questions.length > 0 ? ( | ||
<Icon | ||
className="dynamic-delete-button" | ||
type="minus-circle-o" | ||
disabled={questions.length === 0} | ||
onClick={() => this.remove()} | ||
/> | ||
) : null} | ||
<QuestionForm id={i} {...this.props} /> | ||
<Divider /> | ||
</Hoc> | ||
); | ||
} | ||
return ( | ||
<Form onSubmit={this.handleSubmit}> | ||
<h1>Create an assignment</h1> | ||
<FormItem label={"Title: "}> | ||
{getFieldDecorator(`title`, { | ||
validateTrigger: ["onChange", "onBlur"], | ||
rules: [ | ||
{ | ||
required: true, | ||
message: "Please input a title" | ||
} | ||
] | ||
})(<Input placeholder="Add a title" />)} | ||
</FormItem> | ||
{questions} | ||
<FormItem> | ||
<Button type="secondary" onClick={this.add}> | ||
<Icon type="plus" /> Add question | ||
</Button> | ||
</FormItem> | ||
<FormItem> | ||
<Button type="primary" htmlType="submit"> | ||
Submit | ||
</Button> | ||
</FormItem> | ||
</Form> | ||
); | ||
} | ||
} | ||
|
||
const WrappedAssignmentCreate = Form.create()(AssignmentCreate); | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
token: state.auth.token, | ||
username: state.auth.username, | ||
loading: state.assignments.loading | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
createASNT: (token, asnt) => dispatch(createASNT(token, asnt)) | ||
}; | ||
}; | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(WrappedAssignmentCreate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from "react"; | ||
import { Form, Input, Icon, Button } from "antd"; | ||
import Hoc from "../hoc/hoc"; | ||
|
||
const FormItem = Form.Item; | ||
|
||
let id = 0; | ||
|
||
class QuestionForm extends React.Component { | ||
remove = k => { | ||
const { form } = this.props; | ||
const keys = form.getFieldValue("keys"); | ||
if (keys.length === 1) return; | ||
form.setFieldsValue({ | ||
keys: keys.filter(key => key !== k) | ||
}); | ||
}; | ||
|
||
add = () => { | ||
const { form } = this.props; | ||
const keys = form.getFieldValue("keys"); | ||
const nextKeys = keys.concat(++id); | ||
form.setFieldsValue({ | ||
keys: nextKeys | ||
}); | ||
}; | ||
|
||
render() { | ||
const { getFieldDecorator, getFieldValue } = this.props.form; | ||
getFieldDecorator("keys", { initialValue: [] }); | ||
const keys = getFieldValue("keys"); | ||
const formItems = keys.map((k, index) => ( | ||
<FormItem label={index === 0 ? "Choices" : ""} key={k}> | ||
{getFieldDecorator(`questions[${this.props.id}]choices[${k}]`, { | ||
validateTrigger: ["onChange", "onBlur"], | ||
rules: [ | ||
{ | ||
required: true, | ||
whitespace: true, | ||
message: "Please input a choice to the question" | ||
} | ||
] | ||
})(<Input placeholder="Answer choice" />)} | ||
{keys.length > 1 ? ( | ||
<Icon | ||
className="dynamic-delete-button" | ||
type="minus-circle-o" | ||
disabled={keys.length === 1} | ||
onClick={() => this.remove(k)} | ||
/> | ||
) : null} | ||
</FormItem> | ||
)); | ||
return ( | ||
<Hoc> | ||
<FormItem label="Question: "> | ||
{getFieldDecorator(`question[${this.props.id}]`, { | ||
validateTrigger: ["onChange", "onBlur"], | ||
rules: [ | ||
{ | ||
required: true, | ||
message: "Please input a question" | ||
} | ||
] | ||
})(<Input placeholder="Add a question" />)} | ||
</FormItem> | ||
<FormItem label="Answer: "> | ||
{getFieldDecorator(`answers[${this.props.id}]`, { | ||
validateTrigger: ["onChange", "onBlur"], | ||
rules: [ | ||
{ | ||
required: true, | ||
message: "Please input an answer to this question" | ||
} | ||
] | ||
})(<Input placeholder="What is the answer?" />)} | ||
</FormItem> | ||
{formItems} | ||
<FormItem> | ||
<Button type="dashed" onClick={this.add} style={{ width: "60%" }}> | ||
<Icon type="plus" /> Add an answer choice | ||
</Button> | ||
</FormItem> | ||
</Hoc> | ||
); | ||
} | ||
} | ||
|
||
export default QuestionForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Explain this => data['teacher']