adminPhotogalaryReduxForm.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { Component } from "react";
  2. import { reduxForm, Field } from "redux-form";
  3. import "./adminPhotogalary.scss";
  4. import { addPhotogalaryInitialValue } from "../../state/photogalaryFormData";
  5. import { customInput } from "../customFields/customInput/customInput";
  6. import { customSelect } from "../customFields/customSelect/customSelect";
  7. class PhotogalaryReduxForm extends Component {
  8. state = {
  9. addPhotogalaryInitialValue,
  10. filteredEventList: []
  11. }
  12. submit = values => {
  13. const pictures = this.state.addPhotogalaryInitialValue.pictures.reduce((prev, elem) => {
  14. return prev.concat(elem.value);
  15. }, []);
  16. const submitValues = {
  17. ...this.state.addPhotogalaryInitialValue,
  18. pictures: pictures,
  19. }
  20. this.props.submitHandler(submitValues);
  21. this.props.reset()
  22. };
  23. onChangeTypeHandler = e => {
  24. const { name, value } = e.target;
  25. this.setState(prevState => {
  26. return {
  27. ...prevState,
  28. addPhotogalaryInitialValue: {
  29. ...prevState.addPhotogalaryInitialValue,
  30. [name]: value
  31. },
  32. filteredEventList: [{_id: 1, title: "Select event"}].concat(
  33. this.props.eventList.filter(el => el.eventType === value)
  34. )
  35. };
  36. });
  37. };
  38. onChangeTitleHandler = e => {
  39. const { name, value } = e.target;
  40. this.setState(prevState => {
  41. return {
  42. ...prevState,
  43. addPhotogalaryInitialValue: {
  44. ...prevState.addPhotogalaryInitialValue,
  45. [name]: value
  46. }
  47. };
  48. });
  49. };
  50. onChangePicturesHandler = e => {
  51. const { value, id } = e.target;
  52. this.setState(prevState => {
  53. return {
  54. ...prevState,
  55. addPhotogalaryInitialValue: {
  56. ...prevState.addPhotogalaryInitialValue,
  57. pictures: prevState.addPhotogalaryInitialValue.pictures.map(
  58. el => (el.id === id ? { ...el, value } : el)
  59. )
  60. }
  61. };
  62. });
  63. };
  64. addPicture = (e) => {
  65. this.setState(prevState => {
  66. return {
  67. ...prevState,
  68. addPhotogalaryInitialValue: {
  69. ...prevState.addPhotogalaryInitialValue,
  70. pictures: prevState.addPhotogalaryInitialValue.pictures.concat({
  71. id: Math.random()
  72. .toString()
  73. .substr(2, 100),
  74. name: `picture_${Math.random()
  75. .toString()
  76. .substr(2, 100)}`,
  77. value: ""
  78. })
  79. }
  80. }
  81. });
  82. };
  83. render() {
  84. const { filteredEventList, addPhotogalaryInitialValue } = this.state;
  85. const {
  86. handleSubmit,
  87. eventTypes,
  88. } = this.props;
  89. return (
  90. <form className="event-form__event-form__main" onSubmit={handleSubmit(this.submit)}>
  91. <Field
  92. name="eventType"
  93. label="Event Type"
  94. required
  95. component={customSelect}
  96. onChange={this.onChangeTypeHandler}
  97. className="form-block__select -wide"
  98. >
  99. {eventTypes.map(elem => <option key={elem.id} value={elem.optionName}>{elem.optionName}</option>)}
  100. </Field>
  101. <Field
  102. name="eventTitle"
  103. label="Event Title"
  104. required
  105. component={customSelect}
  106. onChange={this.onChangeTitleHandler}
  107. className="form-block__select -wide"
  108. >
  109. {filteredEventList.map(elem => <option key={elem._id} value={elem.title}>{elem.title}</option>)}
  110. </Field>
  111. {addPhotogalaryInitialValue.pictures.map((el, i) => (
  112. <Field
  113. key={`${el.id}/${i}`}
  114. name={el.name}
  115. id={el.id}
  116. label="Picture"
  117. onChange={this.onChangePicturesHandler}
  118. className="input-box -wide"
  119. placeholder='Enter picture url'
  120. component={customInput}
  121. />
  122. ))}
  123. <button type="button" className="photogalary-form__picture-btn" onClick={this.addPicture} >Add Picture</button>
  124. <div className="event-form__control-box">
  125. <button className="event-form__submit-btn" >Add Photogalary</button>
  126. </div>
  127. </form>
  128. );
  129. }
  130. }
  131. export default reduxForm({ form: "photogalaryForm", enableReinitialize: true })(PhotogalaryReduxForm);