Skip to content

Commit ca106e2

Browse files
committed
Fixes #53: Fixes 'Comments' view in Offline Mode
While working on the REST fallback for offline mode, I neglected to refactor item fetching for the newer promises model. This mean’t we weren’t correctly populating stores that would be used in this mode. This has been confirmed as now working with this fix. Deploy with fix running at https://react-hn-comments.appspot.com/
1 parent b159831 commit ca106e2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/services/HNServiceRest.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function itemRef(id) {
2525
return fetch(endPoint + '/item/' + id + '.json', options)
2626
}
2727

28+
function itemRefJSON(id) {
29+
return itemRef(id).then(function(response) {
30+
return response.json()
31+
})
32+
}
33+
2834
function userRef(id) {
2935
return fetch(endPoint + '/user/' + id + '.json', options)
3036
}
@@ -34,22 +40,23 @@ function updatesRef() {
3440
}
3541

3642
function fetchItem(id, cb) {
37-
itemRef(id).once('value', function(snapshot) {
38-
cb(snapshot.val())
43+
itemRef(id).then(function(snapshot) {
44+
cb(snapshot)
3945
})
4046
}
4147

4248
function fetchItems(ids, cb) {
4349
var items = []
50+
var promises = []
4451
ids.forEach(function(id) {
45-
fetchItem(id, addItem)
52+
promises.push(itemRefJSON(id))
4653
})
47-
function addItem(item) {
48-
items.push(item)
54+
Promise.all(promises).then(function(values) {
55+
items = values
4956
if (items.length >= ids.length) {
5057
cb(items)
5158
}
52-
}
59+
})
5360
}
5461

5562
module.exports = {

0 commit comments

Comments
 (0)