Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1.  
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import RRPropTypes from 'react-router-prop-types';
  5. import styles from '../styles.module.css';
  6. import ChoiceContainer from '../../containers/choice';
  7.  
  8. class ChoiceForm extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. value: undefined,
  13. type: undefined,
  14. };
  15. }
  16.  
  17. componentDidMount() {
  18. // get the id from the route params
  19. const { fetchChoice, match: { params: { id } } } = this.props;
  20. if (id) fetchChoice(id);
  21. }
  22.  
  23. handleInputChange = (event) => {
  24. // pull the name of the input and value of input out of the event object
  25. const { target: { name, value } } = event;
  26. // update the state to a key of the name of the input and value of the value of the input
  27. // ex: type: 'incorrect'
  28. this.setState({
  29. [name]: value,
  30. });
  31. }
  32.  
  33. save = async (event) => {
  34. // don't actually submit the form through the browser
  35. event.preventDefault();
  36. const {
  37. choice: { id }, saveChoice, history, location,
  38. } = this.props;
  39.  
  40. const { value, type = 'correct' } = this.state;
  41. // get the query params from the url
  42. const queryParams = new URLSearchParams(location.search);
  43. // get the quizId from query params
  44. const questionId = queryParams.get('questionId');
  45. await saveChoice({
  46. id, questionId, value, type,
  47. });
  48. history.push(`/questions/${questionId}`);
  49. }
  50.  
  51. render() {
  52. const {
  53. choice: { id, value: defaultValue = '', type: defaultType = 'correct' },
  54. } = this.props;
  55. const {
  56. value = defaultValue,
  57. type = defaultType,
  58. } = this.state;
  59. return (
  60. <>
  61. <h1 className={styles.heading}>{id ? 'Edit Choice' : 'New Choice'}</h1>
  62. <form method="POST" className={styles.form} onSubmit={this.save}>
  63. <label className={styles.form__label} htmlFor="value">
  64. <span>value</span>
  65. <input
  66. type="text"
  67. name="value"
  68. defaultValue={value}
  69. className={styles.form__input}
  70. id="value"
  71. onChange={this.handleInputChange}
  72. />
  73. </label>
  74. <label className={styles.form__label} htmlFor="correct">
  75. <span className={styles.form__labelInline}>Is this choice correct?</span>
  76. <input
  77. type="radio"
  78. name="type"
  79. value="correct"
  80. checked={type === 'correct'}
  81. className={styles.form__input__radio}
  82. id="correct"
  83. onChange={this.handleInputChange}
  84. />
  85. <span>yes</span>
  86. <label className={styles.form__labelInline} htmlFor="incorrect">
  87. <input
  88. type="radio"
  89. name="type"
  90. value="incorrect"
  91. checked={type === 'incorrect'}
  92. className={styles.form__input__radio}
  93. id="incorrect"
  94. onChange={this.handleInputChange}
  95. />
  96. <span>no</span>
  97. </label>
  98. </label>
  99. <button type="submit" className={styles.button}>Save</button>
  100. </form>
  101. </>
  102. );
  103. }
  104. }
  105. ChoiceForm.propTypes = {
  106. choice: PropTypes.shape({
  107. id: PropTypes.string,
  108. value: PropTypes.string,
  109. type: PropTypes.string,
  110. }),
  111. saveChoice: PropTypes.func.isRequired,
  112. fetchChoice: PropTypes.func.isRequired,
  113. history: RRPropTypes.history.isRequired,
  114. location: RRPropTypes.location.isRequired,
  115. match: RRPropTypes.match.isRequired,
  116. };
  117. ChoiceForm.defaultProps = {
  118. choice: {},
  119. };
  120. export default ChoiceContainer(ChoiceForm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement