You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Despite using Firebase, here we don't really want to do real-time updates, just for the sake of simplicity, so we don't subscribe or do something like that. We get 500 items for given type – so we can download `topstories`, `new` and other categories using just this tile. We also cache results, so we can declaratively invoke it each time we need just a page, and be sure that it won't be downloaded again.
21
+
22
+
Now let's create tile for a single item. It will just download a single item, placing it under id namespace:
As you can see, the implementation is extremely similar to the previous item, `storiesTile`. The thing is that they are performing almost the same operation – request some API call, parse results (if needed – here we don't have to), set up nesting and caching – and, in fact, this is how it is supposed to be. The idea behind `redux-tiles` is that because of boilerplate-free code it is very cheap to create small "tiles", which represent atomic piece of functionality, and then combine them later.
34
+
Let's create tile for downloading list of items – there is no such endpoint, so we will have to compose existing tile:
35
+
36
+
```javascript
37
+
exportconstitemsTile=createTile({
38
+
type: ['hn_api', 'items'],
39
+
fn: ({ dispatch, actions, params }) =>
40
+
Promise.all(params.ids.map(id=>
41
+
dispatch(actions.hn_api.item({ id }))
42
+
))
43
+
});
44
+
```
45
+
46
+
We just iterate over ids and request all ids inside. If we want to perform only certain amount of simulatenous requests, we can chunkify these requests here (but for other tiles it will be completely abstracted).
47
+
48
+
And finally, now we can create functionality for returning stories by type with pagination. The logic is the following:
The last tile contains main business logic for our application, but it does not contain any direct api request, so if in the future response for some endpoint will change, or we will have to do different requests to get the same data, we can change it only inside these small tiles (parsing data or dispatching other small tiles).
0 commit comments