Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Created May 31, 2018 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomvis/44aa824c48818f442e6bd4749cf996f8 to your computer and use it in GitHub Desktop.
Save Thomvis/44aa824c48818f442e6bd4749cf996f8 to your computer and use it in GitHub Desktop.
type loadableData('e) =
| Loading
| Loaded(list('e));
type loadableDataAction('e) =
| DidLoad(list('e));
type state = {data: loadableData(Api.product)};
/* Action declaration */
type action =
| DataAction(loadableDataAction(Api.product));
let component = ReasonReact.reducerComponent("ProductList");
let make = _children => {
...component,
initialState: () => {data: Loading},
didMount: self =>
Api.fetchProducts()
|> Js.Promise.then_(products => {
self.send(DataAction(DidLoad(products)));
Js.Promise.resolve(products);
})
|> ignore,
reducer: (action, _) =>
switch (action) {
| DataAction(DidLoad(products)) =>
ReasonReact.Update({data: Loaded(products)})
},
render: self =>
switch (self.state.data) {
| Loading => <label> (ReasonReact.string("Loading...")) </label>
| Loaded(products) =>
let items =
products
|> List.map((p: Api.product) =>
<li key=p.id> (ReasonReact.string(p.name)) </li>
);
<ul> (ReasonReact.array(Array.of_list(items))) </ul>;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment