Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import React from "react";
  2. import { connect } from "react-redux";
  3. import { dispatchOnly, getData, getProducts } from "../../../actions/action.js";
  4. import { commonStyle } from "../../../common/StylesSheet.js";
  5. import SortFilter from "./components/SortFilter/SortFilter.js";
  6. import Products from "./components/Products/Products.js";
  7.  
  8. import { View } from "react-native";
  9.  
  10. /**
  11. * Modified by MaRoCa on 02/05/2020.
  12. */
  13.  
  14. class ProductListScreen extends React.Component {
  15. state = {
  16. selected_brand: "",
  17. limit: 20,
  18. page: 1,
  19. search_value: "",
  20. filter_by: {},
  21. sort_by: "",
  22. refreshCart: false,
  23. searchField: ''
  24.  
  25. };
  26.  
  27. componentDidMount() {
  28. const json = { reducer_type: "FETCHING_DATA", params: {} };
  29. this.props.dispatch(json);
  30. this.getInventory();
  31. }
  32.  
  33. getInventory = () => {
  34. const category = this.props.category;
  35. const json = {
  36. reducer_type: "FETCH_PRODUCT",
  37. path: "husky/getstoreinventory",
  38. params: {
  39. category: category,
  40. brand: this.state.selected_brand,
  41.  
  42. }
  43. };
  44.  
  45. this.props.getInventory(json);
  46. };
  47.  
  48. getBrands = category => {
  49. const json = {
  50. reducer_type: "FETCH",
  51. path: "husky/getbrands",
  52. params: {
  53. category: category,
  54.  
  55. }
  56. };
  57. this.props.getBrands(json);
  58. };
  59.  
  60. handleLoadMore = () => {
  61. let { page } = this.state;
  62. page += 1;
  63. this.setState(
  64. {
  65. page: page
  66. },
  67. () => {
  68. this.getInventory();
  69. }
  70. );
  71. };
  72.  
  73. search = () => {
  74. this.state.selected_brand = "";
  75. this.state.limit = 50;
  76. this.showLoading();
  77. this.getInventory();
  78. };
  79.  
  80. showLoading = () => {
  81. const json = { reducer_type: "FETCHING_DATA", params: {} };
  82. this.props.dispatch(json);
  83. };
  84.  
  85. getQuantity = items => {
  86. const cart_items = this.props.cart;
  87. let data = [];
  88. items = items ? items : [];
  89. items.map(item => {
  90. const tmp = cart_items.filter(cart_item => cart_item.sku === item.sku);
  91. if (tmp.length > 0) {
  92. item.quantity = tmp[0].quantity;
  93. }
  94. data.push(item);
  95. });
  96. return data;
  97. };
  98.  
  99. updateSortFilter = (value, type) => {
  100. this.setState(
  101. {
  102. [type]: value
  103. },
  104. () => {
  105. this.getInventory();
  106. }
  107. );
  108. };
  109.  
  110. extractProduct = () => {
  111. const category = this.props.category.toLowerCase().replace(/\s+/g, "");
  112. const data = this.getQuantity(
  113. this.props.products[category === "" ? "allproducts" : category]
  114. );
  115. console.log('data',data)
  116. const filteredProducts = data.filter(value=>{
  117. return value.description.toLowerCase().includes(this.state.searchField.toLowerCase())
  118. })
  119. return filteredProducts;
  120. };
  121.  
  122. render() {
  123. const { isLoading } = this.props;
  124.  
  125. return (
  126. <View style={[commonStyle.content, { flex: 1 }]}>
  127. <SortFilter
  128. modalMarginTop={
  129. this.props.sortFilterModalMarginTop
  130. ? this.props.sortFilterModalMarginTop
  131. : 154
  132. }
  133. updateSortFilter={this.updateSortFilter}
  134. />
  135. <Products
  136. data={this.extractProduct()}
  137. numberColumn={3}
  138. navigation={this.props.navigation}
  139. dispatch={this.props.dispatch}
  140. loadMore={this.handleLoadMore}
  141. viewDetail={true}
  142. isLoading={isLoading}
  143. />
  144. </View>
  145. );
  146. }
  147. }
  148.  
  149. const mapStateToProps = state => {
  150. const products = state.product;
  151. const loading = state.loading.isFetching;
  152. return {
  153. products: products,
  154. cart: state.cart,
  155. isLoading: loading
  156. };
  157. };
  158.  
  159. const mapDispatchToProps = dispatch => {
  160. return {
  161. getInventory: param => {
  162. dispatch(getProducts(param));
  163. },
  164. getBrands: param => {
  165. dispatch(getData(param));
  166. },
  167. dispatch: param => {
  168. dispatch(dispatchOnly(param));
  169. }
  170. };
  171. };
  172. export default connect(mapStateToProps, mapDispatchToProps)(ProductListScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement