Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Link } from 'react-router';
  4. import { FormControl } from 'react-bootstrap';
  5.  
  6. import api from '../../api';
  7.  
  8. class RecipeDetialEdit extends Component {
  9. constructor(props) {
  10. super(props);
  11.  
  12. this.state = {
  13. directionsEdit: '',
  14. title: '',
  15. };
  16. this.handleTextChange = this.handleTextChange.bind(this);
  17. }
  18.  
  19. componentDidUpdate(prevProps, prevState) {
  20. if (prevProps.recipeDetailData !== this.props.recipeDetailData) {
  21. this.setState({
  22. directionsEdit: this.props.recipeDetailData.directions,
  23. title: this.props.recipeDetailData.title,
  24. });
  25. //console.log(this.props.recipeDetailData.directions);
  26. }
  27. }
  28.  
  29. handleTextChange(event) {
  30. this.setState({
  31. directionsEdit: event.target.value,
  32. });
  33. }
  34.  
  35. handleTitleChange(event) {
  36. this.setState({
  37. title: event.target.value,
  38. });
  39. }
  40.  
  41. saveForm(directions, title, preparationTime, servingCount, slug, _id, sideDish) {
  42. console.log(_id);
  43. api
  44. .post('/recipes/5a8156f1abdff8001a01369e', {
  45. _id,
  46. directions,
  47. if(sideDish){sideDish},
  48. preparationTime,
  49. servingCount,
  50. title,
  51. slug,
  52. //__v: 0,
  53. })
  54. //`/if(slug){window.location.href=`/recipe/${slug}`;}
  55. //.then(
  56. //response => {if(slug){console.log(slug)};}
  57. //);
  58. //if(slug){window.location.href=`/recipe/${slug}`}
  59. }
  60.  
  61. renderSideDish() {
  62. const { recipeDetailData } = this.props;
  63. const { sideDish } = recipeDetailData;
  64.  
  65. return (
  66. sideDish && (
  67. <span>
  68. <i className="fa fa-cutlery" /> Side Dish {sideDish}
  69. </span>
  70. )
  71. );
  72. }
  73.  
  74. render() {
  75. const { directionsEdit } = this.state;
  76. const { isLoading, recipeDetailData } = this.props;
  77. const { title, preparationTime, directions, servingCount, slug, _id, sideDish } =
  78. recipeDetailData || {};
  79.  
  80. if (isLoading) {
  81. return <div> Loading ...</div>;
  82. }
  83.  
  84. return (
  85. <div className="row">
  86. <input
  87. type="text"
  88. value={this.state.title}
  89. onChange={this.handleTitleChange}
  90. />
  91. <div>
  92. <span>
  93. <i className="fa fa-clock-o" />Preparation Time: {preparationTime}{' '}
  94. min
  95. </span>{' '}
  96. {this.renderSideDish()} {''}
  97. <span>
  98. <i className="fa fa-rocket" />Serving counts: {servingCount}
  99. </span>
  100. </div>
  101. <div>
  102. <FormControl
  103. componentClass="textarea"
  104. value={this.state.directionsEdit}
  105. onChange={this.handleTextChange}
  106. style={{ height: '250px' }}
  107. />
  108. </div>
  109. <button onClick={this.saveForm(directionsEdit, title, preparationTime, servingCount, slug, _id, sideDish)}>Save</button>
  110. </div>
  111. );
  112. }
  113. }
  114.  
  115. RecipeDetialEdit.propTypes = {
  116. isLoading: PropTypes.bool.isRequired,
  117. RecipeDetailData: PropTypes.object,
  118. };
  119.  
  120. export default RecipeDetialEdit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement