From 4f90671cec5a2614c8d545289268ff11925795d4 Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Tue, 3 Nov 2020 16:21:23 +1300 Subject: [PATCH] order feed by reverse chronological --- webclient/src/Feed.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/webclient/src/Feed.js b/webclient/src/Feed.js index 1ae9e73..40b10ec 100644 --- a/webclient/src/Feed.js +++ b/webclient/src/Feed.js @@ -22,20 +22,25 @@ class Feed extends React.Component { const updated = !this.state.stories || this.state.stories[0].id !== result.stories[0].id; console.log('updated:', updated); - this.setState({ stories: result.stories }); - localStorage.setItem('stories', JSON.stringify(result.stories)); + const { stories } = result; + if (stories) { + stories.sort((a, b) => b.date - a.date); + } + + this.setState({ stories }); + localStorage.setItem('stories', JSON.stringify(stories)); if (updated) { localForage.clear(); - result.stories.forEach((x, i) => { + stories.forEach((x, i) => { fetch('/api/' + x.id) .then(res => res.json()) - .then(result => { - localForage.setItem(x.id, result.story) + .then(({ story }) => { + localForage.setItem(x.id, story) .then(console.log('preloaded', x.id, x.title)); - this.props.updateCache(x.id, result.story); - }, error => {} - ); + this.props.updateCache(x.id, story); + }, error => { } + ); }); } }, @@ -73,7 +78,7 @@ class Feed extends React.Component { )} - : + :

loading...

}