Guest User

Untitled

a guest
Feb 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. type product = {
  2. category: string,
  3. price: string,
  4. stocked: bool,
  5. name: string
  6. };
  7.  
  8. type products = list(product);
  9.  
  10. let products = [
  11. {category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
  12. {category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
  13. {category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
  14. {category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
  15. {category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
  16. {category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
  17. ];
  18.  
  19. module ProductCategoryRow = {
  20. let component = ReasonReact.statelessComponent("ProductCategoryRow");
  21. let make = (~category, _children) => {
  22. ...component,
  23. render: (_self) => <tr> <th colSpan=2> (ReasonReact.stringToElement(category)) </th> </tr>
  24. };
  25. };
  26.  
  27. module ProductRow = {
  28. let component = ReasonReact.statelessComponent("ProductRow");
  29. let make = (~product, _children) => {
  30. ...component,
  31. render: (_self) =>
  32. <tr>
  33. <td> (ReasonReact.stringToElement(product.name)) </td>
  34. <td> (ReasonReact.stringToElement(product.price)) </td>
  35. </tr>
  36. };
  37. };
  38.  
  39. module ProductTable = {
  40. let component = ReasonReact.statelessComponent("ProductTable");
  41. let make = (~showProductInStock, ~filteredText=?, ~products, _children) => {
  42. ...component,
  43. render: (_self) => {
  44. let textValue =
  45. switch filteredText {
  46. | Some(value) => value
  47. | None => ""
  48. };
  49. let productList =
  50. products
  51. |> List.filter(
  52. (product) =>
  53. Js.String.includes(textValue |> String.lowercase, product.name |> String.lowercase)
  54. && showProductInStock === product.stocked
  55. )
  56. |> List.mapi((id, product) => <ProductRow product key=(string_of_int(id)) />);
  57. <table>
  58. <thead>
  59. <tr>
  60. <th> (ReasonReact.stringToElement("Name")) </th>
  61. <th> (ReasonReact.stringToElement("Price")) </th>
  62. </tr>
  63. </thead>
  64. <tbody> (productList |> Array.of_list |> ReasonReact.arrayToElement) </tbody>
  65. </table>
  66. }
  67. };
  68. };
  69.  
  70. module Searchbar = {
  71. let component = ReasonReact.statelessComponent("Searchbar ");
  72. let make = (~onHandleInputChange, ~inStockOnly, ~onChange, _children) => {
  73. ...component,
  74. render: (_self) =>
  75. <form>
  76. <input onChange _type="text" placeholder="search" />
  77. <p>
  78. <input
  79. _type="checkbox"
  80. checked=(inStockOnly ? Js.true_ : Js.false_)
  81. onChange=onHandleInputChange
  82. />
  83. (ReasonReact.stringToElement(" only show product in stock"))
  84. </p>
  85. </form>
  86. };
  87. };
  88.  
  89. type state = {
  90. filteredText: option(string),
  91. showProductInStock: bool
  92. };
  93.  
  94. type action =
  95. | OnSearch(string)
  96. | OnShowProductInStock;
  97.  
  98. let component = ReasonReact.reducerComponent("FilterableProductTable");
  99.  
  100. let make = (_children) => {
  101. ...component,
  102. initialState: () => {filteredText: None, showProductInStock: false},
  103. reducer: (action, state) =>
  104. switch action {
  105. | OnSearch(text) => ReasonReact.Update({...state, filteredText: Some(text)})
  106. | OnShowProductInStock =>
  107. ReasonReact.Update({...state, showProductInStock: ! state.showProductInStock})
  108. },
  109. render: (self) => {
  110. let onSearchText = (e) =>
  111. ReactEventRe.Form.target(e)
  112. |> ReactDOMRe.domElementToObj
  113. |> ((result) => result##value |> ((value) => self.send(OnSearch(value))));
  114. let onHandleInputChange = (_e) => self.send(OnShowProductInStock);
  115. <div>
  116. <Searchbar
  117. onChange=onSearchText
  118. inStockOnly=self.state.showProductInStock
  119. onHandleInputChange
  120. />
  121. <ProductTable
  122. products
  123. filteredText=?self.state.filteredText
  124. showProductInStock=self.state.showProductInStock
  125. />
  126. </div>
  127. }
  128. };
Add Comment
Please, Sign In to add comment