2019-08-24 05:04:51 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom';
|
2019-10-10 21:52:28 +00:00
|
|
|
import { Helmet } from 'react-helmet';
|
2019-10-12 23:41:31 +00:00
|
|
|
import localForage from 'localforage';
|
2019-12-02 23:54:02 +00:00
|
|
|
import { sourceLink, infoLine, logos } from './utils.js';
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
|
|
class Feed extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
stories: JSON.parse(localStorage.getItem('stories')) || false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
}
|
2019-09-24 08:23:14 +00:00
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
componentDidMount() {
|
|
|
|
fetch('/api')
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(
|
|
|
|
(result) => {
|
2019-10-12 23:41:31 +00:00
|
|
|
const updated = !this.state.stories || this.state.stories[0].id !== result.stories[0].id;
|
|
|
|
console.log('updated:', updated);
|
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
this.setState({ stories: result.stories });
|
|
|
|
localStorage.setItem('stories', JSON.stringify(result.stories));
|
2019-10-12 23:41:31 +00:00
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
localForage.clear();
|
2019-10-19 07:33:06 +00:00
|
|
|
result.stories.forEach((x, i) => {
|
2019-10-12 23:41:31 +00:00
|
|
|
fetch('/api/' + x.id)
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(result => {
|
|
|
|
localForage.setItem(x.id, result.story)
|
|
|
|
.then(console.log('preloaded', x.id, x.title));
|
2019-10-18 21:26:22 +00:00
|
|
|
this.props.updateCache(x.id, result.story);
|
2019-10-12 23:41:31 +00:00
|
|
|
}, error => {}
|
|
|
|
);
|
2019-10-19 07:33:06 +00:00
|
|
|
|
|
|
|
if (i < 20 && x.img) {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = x.img;
|
|
|
|
console.log('prefetched image', x.img);
|
|
|
|
}
|
2019-10-12 23:41:31 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-12 05:32:17 +00:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.setState({ error: true });
|
|
|
|
}
|
|
|
|
);
|
2019-08-24 05:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const stories = this.state.stories;
|
|
|
|
const error = this.state.error;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='container'>
|
2019-10-10 21:52:28 +00:00
|
|
|
<Helmet>
|
|
|
|
<title>Feed - QotNews</title>
|
|
|
|
</Helmet>
|
2019-09-24 08:23:14 +00:00
|
|
|
{error && <p>Connection error?</p>}
|
|
|
|
{stories ?
|
2019-08-24 05:04:51 +00:00
|
|
|
<div>
|
2019-09-24 08:23:14 +00:00
|
|
|
{stories.map((x, i) =>
|
2019-10-12 05:32:17 +00:00
|
|
|
<div className='item' key={i}>
|
2019-09-24 08:23:14 +00:00
|
|
|
<div className='num'>
|
|
|
|
{i+1}.
|
|
|
|
</div>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
<div className='title'>
|
2019-10-18 05:09:49 +00:00
|
|
|
<Link className='link' to={'/' + x.id}>
|
2019-12-02 23:54:02 +00:00
|
|
|
<img className='source-logo' src={logos[x.source]} alt='source logo' /> {x.title}
|
2019-10-18 05:09:49 +00:00
|
|
|
</Link>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
<span className='source'>
|
|
|
|
​({sourceLink(x)})
|
|
|
|
</span>
|
|
|
|
</div>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
{infoLine(x)}
|
2019-08-24 05:04:51 +00:00
|
|
|
</div>
|
2019-09-24 08:23:14 +00:00
|
|
|
)}
|
2019-08-24 05:04:51 +00:00
|
|
|
</div>
|
2019-09-24 08:23:14 +00:00
|
|
|
:
|
|
|
|
<p>loading...</p>
|
2019-08-24 05:04:51 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Feed;
|