Cache all articles in IndexedDB
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
import React from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { Helmet } from 'react-helmet';
|
||||
import localForage from 'localforage';
|
||||
import { sourceLink, infoLine, ToggleDot } from './utils.js';
|
||||
|
||||
class Article extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
const id = this.props.match.params.id;
|
||||
|
||||
this.state = {
|
||||
story: JSON.parse(localStorage.getItem(id)) || false,
|
||||
story: false,
|
||||
error: false,
|
||||
};
|
||||
}
|
||||
@@ -18,12 +17,19 @@ class Article extends React.Component {
|
||||
componentDidMount() {
|
||||
const id = this.props.match.params.id;
|
||||
|
||||
localForage.getItem(id)
|
||||
.then(
|
||||
(value) => {
|
||||
this.setState({ story: value });
|
||||
}
|
||||
);
|
||||
|
||||
fetch('/api/' + id)
|
||||
.then(res => res.json())
|
||||
.then(
|
||||
(result) => {
|
||||
this.setState({ story: result.story });
|
||||
localStorage.setItem(id, JSON.stringify(result.story));
|
||||
localForage.setItem(id, result.story);
|
||||
},
|
||||
(error) => {
|
||||
this.setState({ error: true });
|
||||
|
@@ -3,16 +3,15 @@ import { Link } from 'react-router-dom';
|
||||
import { HashLink } from 'react-router-hash-link';
|
||||
import { Helmet } from 'react-helmet';
|
||||
import moment from 'moment';
|
||||
import localForage from 'localforage';
|
||||
import { sourceLink, infoLine, ToggleDot } from './utils.js';
|
||||
|
||||
class Article extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
const id = this.props.match.params.id;
|
||||
|
||||
this.state = {
|
||||
story: JSON.parse(localStorage.getItem(id)) || false,
|
||||
story: false,
|
||||
error: false,
|
||||
};
|
||||
}
|
||||
@@ -20,17 +19,24 @@ class Article extends React.Component {
|
||||
componentDidMount() {
|
||||
const id = this.props.match.params.id;
|
||||
|
||||
localForage.getItem(id)
|
||||
.then(
|
||||
(value) => {
|
||||
this.setState({ story: value });
|
||||
}
|
||||
);
|
||||
|
||||
fetch('/api/' + id)
|
||||
.then(res => res.json())
|
||||
.then(
|
||||
(result) => {
|
||||
localStorage.setItem(id, JSON.stringify(result.story));
|
||||
this.setState({ story: result.story }, () => {
|
||||
const hash = window.location.hash.substring(1);
|
||||
if (hash) {
|
||||
document.getElementById(hash).scrollIntoView();
|
||||
}
|
||||
});
|
||||
localForage.setItem(id, result.story);
|
||||
},
|
||||
(error) => {
|
||||
this.setState({ error: true });
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import React from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { Helmet } from 'react-helmet';
|
||||
import localForage from 'localforage';
|
||||
import { siteLogo, sourceLink, infoLine } from './utils.js';
|
||||
import { clearStorage } from './utils.js';
|
||||
|
||||
class Feed extends React.Component {
|
||||
constructor(props) {
|
||||
@@ -19,18 +19,24 @@ class Feed extends React.Component {
|
||||
.then(res => res.json())
|
||||
.then(
|
||||
(result) => {
|
||||
const updated = !this.state.stories || this.state.stories[0].id !== result.stories[0].id;
|
||||
console.log('updated:', updated);
|
||||
|
||||
this.setState({ stories: result.stories });
|
||||
clearStorage();
|
||||
localStorage.setItem('stories', JSON.stringify(result.stories));
|
||||
result.stories.filter(x => x.score >= 20).slice(0, 25).forEach(x => {
|
||||
fetch('/api/' + x.id)
|
||||
.then(res => res.json())
|
||||
.then(result => {
|
||||
localStorage.setItem(x.id, JSON.stringify(result.story));
|
||||
console.log('Preloaded story', x.id, x.title);
|
||||
}, error => {}
|
||||
);
|
||||
});
|
||||
|
||||
if (updated) {
|
||||
localForage.clear();
|
||||
result.stories.forEach(x => {
|
||||
fetch('/api/' + x.id)
|
||||
.then(res => res.json())
|
||||
.then(result => {
|
||||
localForage.setItem(x.id, result.story)
|
||||
.then(console.log('preloaded', x.id, x.title));
|
||||
}, error => {}
|
||||
);
|
||||
});
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
this.setState({ error: true });
|
||||
|
@@ -109,7 +109,7 @@ span.source {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.article figure {
|
||||
.article figure, .article video {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
margin: 0;
|
||||
|
@@ -29,12 +29,6 @@ export const infoLine = (story) =>
|
||||
</div>
|
||||
;
|
||||
|
||||
export const clearStorage = () => {
|
||||
const themeSetting = localStorage.getItem('theme');
|
||||
localStorage.clear();
|
||||
localStorage.setItem('theme', themeSetting);
|
||||
};
|
||||
|
||||
export class ToggleDot extends React.Component {
|
||||
render() {
|
||||
const id = this.props.id;
|
||||
|
Reference in New Issue
Block a user