diff --git a/webclient/src/Article.js b/webclient/src/Article.js index d4def54..007acd6 100644 --- a/webclient/src/Article.js +++ b/webclient/src/Article.js @@ -1,32 +1,24 @@ -import React from 'react'; +import React, { useState, useEffect, useMemo } from 'react'; +import { useParams } 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); +function Article({ cache }) { + const { id } = useParams(); - const id = this.props.match ? this.props.match.params.id : 'CLOL'; - const cache = this.props.cache; + if (id in cache) console.log('cache hit'); - if (id in cache) console.log('cache hit'); - - this.state = { - story: cache[id] || false, - error: false, - pConv: [], - }; - } - - componentDidMount() { - const id = this.props.match ? this.props.match.params.id : 'CLOL'; + const [story, setStory] = useState(cache[id] || false); + const [error, setError] = useState(false); + const [pConv, setPConv] = useState([]); + useEffect(() => { localForage.getItem(id) .then( (value) => { if (value) { - this.setState({ story: value }); + setStory(value); } } ); @@ -35,79 +27,76 @@ class Article extends React.Component { .then(res => res.json()) .then( (result) => { - this.setState({ story: result.story }); + setStory(result.story); localForage.setItem(id, result.story); }, (error) => { - this.setState({ error: true }); + setError(true); } ); - } + }, [id]); - pConvert = (n) => { - this.setState({ pConv: [...this.state.pConv, n]}); - } + const pConvert = (n) => { + setPConv(prevPConv => [...prevPConv, n]); + }; - render() { - const id = this.props.match ? this.props.match.params.id : 'CLOL'; - const story = this.state.story; - const error = this.state.error; - const pConv = this.state.pConv; - let nodes = null; - - if (story.text) { + const nodes = useMemo(() => { + if (story && story.text) { let div = document.createElement('div'); div.innerHTML = story.text; - nodes = div.childNodes; + return div.childNodes; } + return null; + }, [story]); - return ( -
Connection error?
} - {story ? -Connection error?
} + {story ? +{v.data}
- : - <> -Problem getting article :(
- } +loading...
- } -{v.data}
+ : +Problem getting article :(
+ } +loading...
+ } +