adminPhotogalary.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, { Component, Fragment } from "react";
  2. import "./adminPhotogalary.scss";
  3. import { form } from "../../state/photogalaryFormData";
  4. import Input from "../input/input";
  5. import Select from "../select/select";
  6. import Button from "../button/button";
  7. import Header from '../adminHeader/adminHeader';
  8. export default class PhotogalaryForm extends Component {
  9. state = {
  10. form,
  11. validForm: false
  12. }
  13. validator = (rules, value) => {
  14. const { requred, minL } = rules;
  15. let valid = true;
  16. if (requred) {
  17. valid = value.trim() === "" && valid;
  18. }
  19. if (minL) {
  20. valid = value.trim().length < minL && valid;
  21. }
  22. return valid;
  23. };
  24. submit = e => {
  25. e.preventDefault();
  26. const values = Object.keys(this.state.form).reduce((prev, elem) => {
  27. return { ...prev, [elem]: this.state.form[elem].value };
  28. }, {});
  29. this.props.submitHandler(values);
  30. };
  31. onChangeHandler = e => {
  32. const { name, value, id } = e.target;
  33. this.setState(prevState => {
  34. const values = Object.keys(prevState.form).reduce((prev, elem) => {
  35. if (elem === name) return prev.concat(value);
  36. return prev.concat(prevState.form[elem].value);
  37. }, []);
  38. return {
  39. ...prevState,
  40. form: {
  41. ...prevState.form,
  42. [name]: {
  43. ...prevState.form[name],
  44. value,
  45. touch: true,
  46. // fail: this.validator(prevState.form[name].validation, value)
  47. },
  48. pictures: prevState.form.pictures.map(el => (el.id === +id ? { ...el, value } : el))
  49. },
  50. validForm: values.some(value => value)
  51. };
  52. });
  53. };
  54. addPicture = (e) => {
  55. this.setState(prevState => ({
  56. ...prevState,
  57. form: {
  58. ...prevState.form,
  59. pictures: prevState.form.pictures.concat({
  60. id: +Math.random()
  61. .toString()
  62. .substr(2, 100),
  63. type: "input",
  64. name: "pictures",
  65. label: "Picture",
  66. placeholder: 'Enter picture url',
  67. value: ""
  68. })
  69. // ...prevState.form.pictures,
  70. // value: prevState.form.pictures.value.concat(e.value)
  71. // id: Math.random()
  72. // .toString()
  73. // .substr(2, 100),
  74. // value: prevState.form.pictures.value.concat({
  75. // id: Math.random()
  76. // .toString()
  77. // .substr(2, 100),
  78. // url: ""
  79. // )
  80. // }
  81. }
  82. }));
  83. };
  84. render() {
  85. const { form, validForm } = this.state;
  86. console.log(this.state)
  87. const { error } = this.props;
  88. const disPictureAdd = form.pictures.every(el => el.value)
  89. const disPictureRm = form.pictures.length === 1;
  90. return (
  91. <>
  92. <Header />
  93. <div className="photogalary-form">
  94. <div className="photogalary-form__content">
  95. <h2 className="photogalary-form__form-title">ADD EVENT PHOTOGALARY</h2>
  96. <form onSubmit={this.submit} className="photogalary-form__photogalary-form">
  97. <div className="photogalary-form__photogalary-form__selects">
  98. {Object.keys(form.selects).map((input_name, index) => (
  99. <Select
  100. key={form.selects[input_name].id}
  101. id={form.selects[input_name].id}
  102. name={form.selects[input_name].name}
  103. // value={form.selects[input_name].value}
  104. fail={form.selects[input_name].fail}
  105. touch={form.selects[input_name].touch}
  106. label={form.selects[input_name].label}
  107. placeholder={form.selects[input_name].placeholder}
  108. onChange={this.onChangeHandler}
  109. >
  110. {form.selects[input_name].options.map(elem =>
  111. <option key={elem} value={elem}> {elem} </option>)}
  112. </Select>
  113. )
  114. )}
  115. </div>
  116. {form.pictures.map((el, i) => (
  117. <div key={`${el.id}/${i}`}>
  118. {console.log('elem', el)}
  119. <Input
  120. className = "photogalary-form__picture-input"
  121. id={el.id}
  122. name={el.name}
  123. value={el.value}
  124. label={el.label}
  125. // key={form[input_name].id}
  126. // id={form[input_name].id}
  127. // name={form[input_name].name}
  128. // value={el.url}
  129. // fail={form[input_name].fail}
  130. // touch={form[input_name].touch}
  131. // label={form[input_name].label}
  132. placeholder={el.placeholder}
  133. onChange={this.onChangeHandler}
  134. />
  135. {/* {!disPictureRm && <Button text="-" onClick={rmHobby.bind(null, el.id)} />} */}
  136. </div>
  137. ))}
  138. <Button className="photogalary-form__picture-btn" text="Add Picture" onClick={this.addPicture} disabled={!disPictureAdd} />
  139. {error && <p className="photogalary-form__error-photogalary-text">{error}</p>}
  140. <div className="photogalary-form__control-box">
  141. <Button disabled={!validForm} className="photogalary-form__submit-btn" type="submit" text="Add Photogalary" />
  142. </div>
  143. </form>
  144. </div>
  145. </div>
  146. </>
  147. );
  148. }
  149. }