Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import axios from 'axios';
  3. import {Helmet} from "react-helmet";
  4.  
  5. //component
  6. import Product from './Product'
  7. import {Link} from 'react-router-dom';
  8.  
  9. class Store extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. post: {},
  14. products: null,
  15. postId: props.match.params.id,
  16. username: props.match.params.username,
  17. uid: null,
  18. morePost: null
  19. };
  20. // this.newPost = this.newPost.bind(this);
  21. }
  22.  
  23. componentDidMount() {
  24. this.getData('une-certaine-url').then((res) => {
  25. this.setState({
  26. uid: res.data.id,
  27. });
  28. this.getData('une-autre-url').then((res) => {
  29. this.setState({
  30. post: res.data,
  31. products: res.data.products,
  32. morePost: res.data.posts
  33. })
  34. });
  35. })
  36. }
  37.  
  38. getData = (url) => {
  39. return new Promise((resolv, reject) => {
  40. axios.get(url).then((res) => {
  41. resolv(res);
  42. }).catch((err) => {
  43. console.log(err)
  44. });
  45. });
  46. }
  47.  
  48. newPost = (product, ok) => {
  49. console.log("productssss", product.id);
  50. let url = 'une-autre-url';
  51. this.getUpdateData(url).then((res) => {
  52. console.log("its promise res", res);
  53. this.renderProduct();
  54. });
  55. }
  56.  
  57. getUpdateData = (url) => {
  58. return new Promise((resolve, reject) => {
  59. this.getData(url).then((res) => {
  60. // console.log("res", res.data.products);
  61. this.setState({
  62. post: res.data,
  63. products: res.data.products,
  64. morePost: res.data.posts
  65. }, resolve(res));
  66. })
  67. })
  68. }
  69.  
  70.  
  71. renderProduct = () => {
  72. console.log("state actuelle", this.state);
  73. if (this.state.products != null || this.state.products != undefined) {
  74. return this.state.products.map((product, index) => {
  75. return (
  76. <Product key={index} id={product.id} product={product}
  77. user={this.state.uid}/>
  78. )
  79. });
  80. }
  81. }
  82.  
  83. render() {
  84. if (this.state.morePost != null || this.state.morePost != undefined) {
  85. var renderMorePost = this.state.morePost.map((product, index) =>
  86. <Link to={`/store/${this.props.match.params.username}/${product.id}`} component={Store}
  87. className="image-square-more" onClick={this.newPost.bind(this, product)}>
  88. <img src={product.image} alt="" className="img-fluid gradient"/>
  89. </Link>
  90. );
  91. }
  92.  
  93.  
  94. return (
  95. <div>
  96. <Helmet>
  97. <meta charSet="utf-8"/>
  98. <title>{`${this.props.match.params.username} Store`}</title>
  99. <link rel="canonical" href={`http://${this.props.match.params.username}.io`}/>
  100. </Helmet>
  101. <div className="story">
  102. <div className="item-img">
  103. <img src={this.state.post.image} className="gradient" alt=""/>
  104. </div>
  105. <div className="content-product">
  106. {this.renderProduct()}
  107. </div>
  108. <div className="more-post">
  109. <div className="title">
  110. <span>Plus de posts</span>
  111. </div>
  112. <div className="d-flex justify-content-start align-items-center">
  113. {renderMorePost}
  114. </div>
  115. </div>
  116. </div>
  117. </div>
  118. );
  119. }
  120. }
  121.  
  122. export default Store;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement