adminPhotogalaryReduxForm.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import { eventTypes } from "../../state/addEventInitialValue";
  8. class PhotogalaryReduxForm extends Component {
  9. state = {
  10. addPhotogalaryInitialValue
  11. }
  12. submit = e => {
  13. // this.props.submitHandler(addPhotogalaryInitialValue);
  14. };
  15. onChangeHandler = e => {
  16. const { name, value, id } = e.target;
  17. this.setState(prevState => {
  18. return {
  19. ...prevState,
  20. addPhotogalaryInitialValue: {
  21. ...prevState.addPhotogalaryInitialValue,
  22. [name]: value,
  23. pictures: prevState.addPhotogalaryInitialValue.pictures.map(el => (el.id === id ? { ...el, value } : el))
  24. }
  25. };
  26. });
  27. };
  28. addPicture = (e) => {
  29. this.setState(prevState => ({
  30. ...prevState,
  31. addPhotogalaryInitialValue: {
  32. ...prevState.addPhotogalaryInitialValue,
  33. pictures: prevState.addPhotogalaryInitialValue.pictures.concat({
  34. id: Math.random()
  35. .toString()
  36. .substr(2, 100),
  37. value: ""
  38. })
  39. }
  40. }));
  41. };
  42. render() {
  43. const { addPhotogalaryInitialValue } = this.state;
  44. console.log(this.state)
  45. const { handleSubmit } = this.props;
  46. return (
  47. <form className="event-form__event-form__main" onSubmit={handleSubmit(this.submit)}>
  48. <Field name="eventType" label="Event Type" required component={customSelect} onChange = {this.onChangeHandler} >
  49. {eventTypes.map(elem => <option key={elem.id} value={elem.optionName}>{elem.optionName}</option>)}
  50. </Field>
  51. <Field name="eventTytle" label="Event Title" required component={customInput} onChange = {this.onChangeHandler} />
  52. {addPhotogalaryInitialValue.pictures.map((el, i) => (
  53. <Field key={`${el.id}/${i}`} name="pictures" label="Picture" onChange = {this.onChangeHandler} className="input-box__wide" placeholder='Enter picture url' component={customInput} />
  54. ))}
  55. <button type="button" className="photogalary-form__picture-btn" onClick={this.addPicture} >Add Picture</button>
  56. <div className="event-form__control-box">
  57. <button className="event-form__submit-btn" >Add Event</button>
  58. </div>
  59. </form>
  60. );
  61. }
  62. }
  63. export default reduxForm({ form: "photogalaryForm", enableReinitialize: true })(PhotogalaryReduxForm);