Guest User

Untitled

a guest
Oct 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { BrowserRouter as Router, Route } from 'react-router-dom';
  3.  
  4. import Item from './components/Item';
  5. import fakeData from './fake_data/fakeData';
  6.  
  7. class App extends Component {
  8. render() {
  9. return (
  10. <Router>
  11. <div id="wrapper">
  12. <Route exact={true} path="/" render={() => (
  13. <List />
  14. )} />
  15. </div>
  16. </Router>
  17. );
  18. }
  19. }
  20.  
  21. class List extends Component {
  22. constructor() {
  23. super();
  24. this.state = {
  25. items: fakeData.products,
  26. categories: fakeData.categories,
  27. currentFilter: fakeData.categories[0]
  28. };
  29. }
  30.  
  31. getState() {
  32. const stateObj = Object.assign({}, this.state);
  33. return stateObj;
  34. }
  35.  
  36. filterItems(itemsObj) {
  37. const stateObj = this.getState();
  38. const filter = stateObj.currentFilter;
  39.  
  40. if(filter) {
  41. const filterId = filter.id;
  42.  
  43. const filteredItems = itemsObj.filter(thisItem => {
  44. const category = thisItem.category;
  45. if(this.stringMatch(category, filterId))
  46. return thisItem;
  47. return false;
  48. });
  49.  
  50. return filteredItems;
  51. }
  52.  
  53. return itemsObj;
  54. }
  55.  
  56. // This will be used for the store search by substituting stringMatch with stringSearch where appropriate
  57. stringSearch(searchString, thisString) {
  58. if(thisString.indexOf(searchString) === 0)
  59. return true;
  60.  
  61. return false;
  62. }
  63.  
  64. stringMatch(searchString, thisString) {
  65. if(thisString === searchString)
  66. return true;
  67.  
  68. return false;
  69. }
  70.  
  71. getItems() {
  72. const itemsObj = this.getState().items;
  73. const filteredItems = this.filterItems(itemsObj);
  74.  
  75. const renderObj = filteredItems.map((thisItem, thisIndex) => {
  76. return (
  77. <Item
  78. key={thisIndex}
  79. id={thisItem.id}
  80. name={thisItem.name}
  81. short_description={thisItem.short_description}
  82. description={thisItem.description}
  83. price={thisItem.price}
  84. image={thisItem.image}
  85. category={thisItem.category}
  86. />
  87. );
  88. });
  89.  
  90. return renderObj;
  91. }
  92.  
  93. render() {
  94. const itemsObj = this.getItems();
  95. const filterName = this.state.currentFilter.name;
  96. const heading = filterName ? filterName : '';
  97.  
  98. return (
  99. <div className="o-item-listing">
  100. <div className="o-item-listing__title">
  101. {heading}
  102. </div>
  103. <div className="o-item-listing__content">
  104. {itemsObj}
  105. </div>
  106. </div>
  107. );
  108. }
  109. }
  110.  
  111. export default App;
Add Comment
Please, Sign In to add comment