import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { Helmet } from 'react-helmet'; import localForage from 'localforage'; import { sourceLink, infoLine, logos } from './utils.js'; function Feed({ updateCache }) { const [stories, setStories] = useState(() => JSON.parse(localStorage.getItem('stories')) || false); const [error, setError] = useState(false); useEffect(() => { fetch('/api') .then(res => res.json()) .then( (result) => { const updated = !stories || stories[0].id !== result.stories[0].id; console.log('updated:', updated); setStories(result.stories); localStorage.setItem('stories', JSON.stringify(result.stories)); if (updated) { localForage.clear(); result.stories.forEach((x, i) => { fetch('/api/' + x.id) .then(res => res.json()) .then(result => { localForage.setItem(x.id, result.story) .then(console.log('preloaded', x.id, x.title)); updateCache(x.id, result.story); }, error => {} ); }); } }, (error) => { setError(true); } ); }, [updateCache]); return (
QotNews {error &&

Connection error?

} {stories ?
{stories.map(x =>
source logo {x.title} ({sourceLink(x)})
{infoLine(x)}
)}
:

loading...

}
); } export default Feed;