Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. import React from "react";
  2. import FinalList from "./FinalList.js";
  3. import "./App.css";
  4. import Product from "./Product.js";
  5. import { thisTypeAnnotation } from "@babel/types";
  6.  
  7. const items = [
  8. {
  9. id: 1,
  10. name: "banana",
  11. symbol: "d"
  12. },
  13. {
  14. id: 2,
  15. name: "orange",
  16. symbol: "d"
  17. },
  18. {
  19. id: 3,
  20. name: "bread",
  21. symbol: "d"
  22. },
  23. {
  24. id: 4,
  25. name: "water",
  26. symbol: "d"
  27. },
  28. {
  29. id: 5,
  30. name: "cheese",
  31. symbol: "d"
  32. },
  33. {
  34. id: 6,
  35. name: "butter",
  36. symbol: "d"
  37. }
  38. ];
  39.  
  40. class App extends React.Component {
  41. constructor(props) {
  42. super(props);
  43. this.state = {
  44. selected: [],
  45. toSelect: [],
  46. selectedList: []
  47. };
  48.  
  49. this.nextHandleClick = this.nextHandleClick.bind(this);
  50. this.buttonSELOnClick = this.buttonSELOnClick.bind(this);
  51. this.buttonToSELOnClick = this.buttonToSELOnClick.bind(this);
  52. }
  53.  
  54. componentDidMount() {
  55. this.setState({
  56. toSelect: items
  57. });
  58. console.log(this.state);
  59. }
  60.  
  61. buttonSELOnClick(id) {
  62. const item = items.find(item => item.id == id);
  63. const newStateSelect = this.state.selected.filter(item => item.id != id);
  64. const newStateToSelect = this.state.toSelect.concat(item);
  65.  
  66. this.setState({
  67. selected: newStateSelect,
  68. toSelect: newStateToSelect,
  69. selectedList: newStateSelect
  70. });
  71. }
  72.  
  73. buttonToSELOnClick(id) {
  74. const item = items.find(item => item.id == id);
  75. const newStateSelect = this.state.selected.concat(item);
  76. const newStateToSelect = this.state.toSelect.filter(item => item.id != id);
  77.  
  78. this.setState({
  79. selected: newStateSelect,
  80. toSelect: newStateToSelect,
  81. selectedList: newStateSelect
  82. });
  83. }
  84.  
  85. nextHandleClick = () => {
  86. this.state.selectedList.map(product => {
  87. console.log(product.name);
  88. return <FinalList title={this.shoplistName} name={product.name} />;
  89. });
  90. };
  91.  
  92. render() {
  93. console.log(this.state);
  94. return (
  95. <div className="App">
  96. <div className="wrap">
  97. <div className="selectedProducts">
  98. {this.state.selected.map(product => {
  99. return (
  100. <Product
  101. className="minusButton"
  102. name={product.name}
  103. key={`selected-${product.id}`}
  104. id={product.id}
  105. symbol="-"
  106. onClick={this.buttonSELOnClick}
  107. />
  108. );
  109. })}
  110. </div>
  111. <div className="toSelectProducts">
  112. {this.state.toSelect.map(product => {
  113. return (
  114. <Product
  115. className="plusButton"
  116. name={product.name}
  117. key={`toSelect-${product.id}`}
  118. id={product.id}
  119. symbol="+"
  120. onClick={this.buttonToSELOnClick}
  121. />
  122. );
  123. })}
  124. </div>
  125. <div>
  126. <input
  127. placeholder="Enter shoplist name"
  128. ref={c => (this.shoplistName = c)}
  129. />
  130. <button onClick={this.nextHandleClick()}>Next</button>
  131. </div>
  132. </div>
  133. </div>
  134. );
  135. }
  136. }
  137.  
  138. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement