Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. FormGroup,
  4. FormControl,
  5. ControlLabel,
  6. FormLabel,
  7. Button,
  8. Alert,
  9. Form,
  10. Grid,
  11. Row,
  12. Col,
  13. Tabs,
  14. Tab,
  15. Panel,
  16. PageHeader
  17. } from 'react-bootstrap';
  18.  
  19. import Axios from 'axios';
  20. Axios.defaults.withCredentials = true;
  21.  
  22.  
  23. class CreateForm extends React.Component {
  24. constructor(props) {
  25. super(props);
  26. this.state = {
  27. isGoing: true,
  28. numberOfGuests: 2,
  29. formTitle: "Example Form Title",
  30. formDescription: "Example Form Description",
  31. numQuestions: 1,
  32. listQuestions: [],
  33. newQuestion: "Example Question",
  34. newQuestionType: "text"
  35. };
  36. this.handleInputChange = this.handleInputChange.bind(this);
  37. this.addQuestion = this.addQuestion.bind(this);
  38. this.handelSubmit = this.handleSubmit.bind(this);
  39. this.delQuestion = this.delQuestion.bind(this);
  40. }
  41.  
  42. handleInputChange(event) {
  43. const target = event.target;
  44. const value = target.type === 'checkbox' ? target.checked : target.value;
  45. const name = target.name;
  46.  
  47. this.setState({
  48. [name]: value
  49. });
  50. }
  51.  
  52. handleSubmit(event) {
  53. const title = this.state.formTitle;
  54. const description = this.state.formDescription;
  55. const listQuestions = this.state.listQuestions;
  56.  
  57. Axios.post(window.BACKEND + '/api/create/', { formTitle: title, formDescription: description, listQuestions: listQuestions })
  58. .then(json => {
  59. json = json.data
  60. if (json.success) {
  61. alert("成功新增表單");
  62. } else {
  63. alert("尷尬...");
  64. }
  65. })
  66. }
  67.  
  68. addQuestion(event) {
  69. const target = event.target;
  70. //const value = target.type === 'checkbox' ? target.checked : target.value;
  71. const name = target.name;
  72. const listQuestions = this.state.listQuestions.slice();
  73. //alert(this.state.newQuestion, this.state.newQuestionType);
  74. listQuestions.push({ 'question_text': this.state.newQuestion, 'question_type': this.state.newQuestionType });
  75. this.setState({ listQuestions: listQuestions });
  76. }
  77.  
  78. delQuestion(event) {
  79. const target = event.target;
  80. //const value = target.type === 'checkbox' ? target.checked : target.value;
  81. const q_name = target.value;
  82. const idx = parseInt(q_name.split("_")[1])
  83. alert('刪除問題: ' + idx);
  84. const listQuestions = this.state.listQuestions.slice();
  85. listQuestions.splice(idx, 1);
  86. this.setState({ listQuestions: listQuestions });
  87.  
  88. }
  89.  
  90. render() {
  91. //const elements = ['one', 'two', 'three'];
  92. const items = []
  93. for (const [index, value] of this.state.listQuestions.entries()) {
  94. const q_name = "Q_" + index;
  95. if (value['question_type'] === "select") {
  96. items.push(
  97. <div>
  98. <Panel>
  99. <Panel.Heading><Panel.Title componentClass="h3">第{index + 1}題</Panel.Title> <Button type="button" bsStyle="danger" value={q_name} className="new" onClick={this.delQuestion}>X 刪除這題</Button></Panel.Heading>
  100. <Panel.Body>{value['question_text']}</Panel.Body>
  101. <Panel.Footer>
  102. <FormControl componentClass="select"
  103. name={q_name}
  104. //value={this.state.listAnswers[index]['answer']}
  105. onChange={this.handleInputChangeForm}>
  106. <option value="A">A</option>
  107. <option value="B">B</option>
  108. <option value="C">C</option>
  109. <option value="D">D</option>
  110. </FormControl>
  111. </Panel.Footer>
  112. </Panel>
  113. </div>
  114. );
  115. } else if (value['question_type'] === "text") {
  116. items.push(
  117.  
  118. <div>
  119. <Panel>
  120. <Panel.Heading><Panel.Title componentClass="h3">第{index + 1}題</Panel.Title> <Button type="button" bsStyle="danger" value={q_name} className="new" onClick={this.delQuestion}>X 刪除這題</Button></Panel.Heading>
  121. <Panel.Body>{value['question_text']}</Panel.Body>
  122. <Panel.Footer>
  123. <FormControl name={q_name}
  124. type="text"
  125. //value={this.state.listAnswers[index]['answer']}
  126. onChange={this.handleInputChangeForm} />
  127. </Panel.Footer>
  128. </Panel>
  129. </div>
  130.  
  131. );
  132. }
  133. }
  134.  
  135. return (
  136.  
  137. <Grid>
  138. <Row>
  139. <Col xs={12} md={12}>
  140. <Form horizontal method="post" onSubmit={this.handleSubmit}>
  141. <PageHeader>
  142. 新增問卷<small></small>
  143. </PageHeader>
  144. <Panel bsStyle="info">
  145. <Panel.Heading>
  146. <Panel.Title componentClass="h3">問卷設定</Panel.Title>
  147. </Panel.Heading>
  148. <Panel.Body>
  149.  
  150. <FormGroup controlId="">
  151. <Col xs={12} md={4}>
  152. <ControlLabel>問卷標題</ControlLabel>
  153. </Col>
  154. <Col xs={12} md={6}>
  155. <FormControl name="formTitle"
  156. type="text"
  157. value={this.state.formTitle}
  158. onChange={this.handleInputChange} />
  159. </Col>
  160. </FormGroup>
  161. <FormGroup controlId="">
  162. <Col xs={12} md={4}>
  163. <ControlLabel>問卷說明</ControlLabel>
  164. </Col>
  165. <Col xs={12} md={6}>
  166. <FormControl name="formDescription"
  167. type="text"
  168. value={this.state.formDescription}
  169. onChange={this.handleInputChange} />
  170. </Col>
  171. </FormGroup>
  172. </Panel.Body>
  173. </Panel>
  174. {items}
  175. <Panel bsStyle="warning">
  176. <Panel.Heading>
  177. <Panel.Title componentClass="h3">+新問題</Panel.Title>
  178. </Panel.Heading>
  179. <Panel.Body>
  180. <FormGroup controlId="">
  181. <Col xs={12} md={4}>
  182. <ControlLabel>敘述</ControlLabel>
  183. </Col>
  184. <Col xs={12} md={6}>
  185. <FormControl name="newQuestion"
  186. componentClass="textarea"
  187. value={this.state.newQuestion}
  188. onChange={this.handleInputChange} />
  189. </Col>
  190. </FormGroup>
  191. <FormGroup controlId="">
  192. <Col xs={12} md={4}>
  193. <ControlLabel>題型</ControlLabel>
  194. </Col>
  195. <Col xs={12} md={6}>
  196. <FormControl componentClass="select" name="newQuestionType" value={this.state.newQuestionType} onChange={this.handleInputChange}>
  197. <option value="text">簡答題</option>
  198. <option value="select">單選題</option>
  199. </FormControl>
  200. </Col>
  201. </FormGroup>
  202. <Button type="button" className="new" onClick={this.addQuestion}>新增問題</Button>
  203.  
  204. </Panel.Body>
  205. </Panel>
  206. <Button type="submit" value="submit">完成~新增問卷!</Button>
  207. </Form>
  208. </Col>
  209.  
  210. </Row>
  211. </Grid>
  212.  
  213.  
  214.  
  215. // <form method="post" onSubmit={this.handleSubmit} >
  216. // {/* <label>
  217. // Is going:
  218. // <input
  219. // name="isGoing"
  220. // type="checkbox"
  221. // checked={this.state.isGoing}
  222. // onChange={this.handleInputChange} />
  223. // </label>
  224. // <br />
  225. // <label>
  226. // Number of guests:
  227. // <input
  228. // name="numberOfGuests"
  229. // type="number"
  230. // value={this.state.numberOfGuests}
  231. // onChange={this.handleInputChange} />
  232. // </label> */}
  233. // <label>
  234. // New Question:
  235. // <input
  236. // name="formTitle"
  237. // type="text"
  238. // value={this.state.formTitle}
  239. // onChange={this.handleInputChange} />
  240. // </label>
  241.  
  242. // <label>
  243. // New Question:
  244. // <input
  245. // name="formDescription"
  246. // type="text"
  247. // value={this.state.formDescription}
  248. // onChange={this.handleInputChange} />
  249. // </label>
  250.  
  251. // <label>
  252. // New Question:
  253. // <input
  254. // name="newQuestion"
  255. // type="text"
  256. // value={this.state.newQuestion}
  257. // onChange={this.handleInputChange} />
  258. // </label>
  259. // <label>
  260. // Question Type:
  261. // <select name="newQuestionType" value={this.state.newQuestionType} onChange={this.handleInputChange}>
  262. // <option value="text">簡答題</option>
  263. // <option value="select">單選題</option>
  264. // </select>
  265. // </label>
  266. // <button type="button" className="new" onClick={this.addQuestion}>
  267. // 新增問題
  268. // </button>
  269.  
  270. // {items}
  271. // <button type="submit" value="submit">
  272. // 新增問卷
  273. // </button>
  274. // </form>
  275. );
  276. }
  277. }
  278.  
  279.  
  280.  
  281. export default CreateForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement