Cache all articles in IndexedDB

This commit is contained in:
2019-10-12 23:41:31 +00:00
parent 7cb87b59fe
commit 0f5b2a5ff9
10 changed files with 88 additions and 30 deletions

View File

@@ -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 });