qotnews/webclient/src/App.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-08-24 05:04:51 +00:00
import React from 'react';
2019-10-12 05:32:17 +00:00
import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';
2019-10-18 21:26:22 +00:00
import localForage from 'localforage';
2019-08-24 05:04:51 +00:00
import './Style-light.css';
import './Style-dark.css';
2019-08-25 07:46:58 +00:00
import './fonts/Fonts.css';
2019-08-24 05:04:51 +00:00
import Feed from './Feed.js';
import Article from './Article.js';
import Comments from './Comments.js';
2019-10-12 05:32:17 +00:00
import Search from './Search.js';
import Results from './Results.js';
import ScrollToTop from './ScrollToTop.js';
2019-08-24 05:04:51 +00:00
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: localStorage.getItem('theme') || '',
};
2019-10-18 21:26:22 +00:00
this.cache = {};
}
updateCache = (key, value) => {
this.cache[key] = value;
2019-08-24 05:04:51 +00:00
}
light() {
this.setState({ theme: '' });
localStorage.setItem('theme', '');
}
dark() {
this.setState({ theme: 'dark' });
localStorage.setItem('theme', 'dark');
}
2019-10-18 21:26:22 +00:00
componentDidMount() {
if (!this.cache.length) {
localForage.iterate((value, key) => {
this.updateCache(key, value);
});
console.log('loaded cache from localforage');
}
}
2019-08-24 05:04:51 +00:00
render() {
const theme = this.state.theme;
2019-08-25 07:46:58 +00:00
document.body.style.backgroundColor = theme === 'dark' ? '#000' : '#eeeeee';
2019-08-24 05:04:51 +00:00
return (
<div className={theme}>
<Router>
<div className='container menu'>
<p>
<Link to='/'>QotNews - Feed</Link>
2019-08-24 05:04:51 +00:00
<span className='theme'>Theme: <a href='#' onClick={() => this.light()}>Light</a> - <a href='#' onClick={() => this.dark()}>Dark</a></span>
<br />
<span className='slogan'>Reddit, Hacker News, and Tildes combined, then pre-rendered in reader mode.</span>
2019-08-24 05:04:51 +00:00
</p>
2019-10-12 05:32:17 +00:00
<Route path='/(|search)' component={Search} />
2019-08-24 05:04:51 +00:00
</div>
2019-10-12 05:32:17 +00:00
2019-10-18 21:26:22 +00:00
<Route path='/' exact render={(props) => <Feed {...props} updateCache={this.updateCache} />} />
2019-10-12 05:32:17 +00:00
<Switch>
<Route path='/search' component={Results} />
2019-10-18 21:26:22 +00:00
<Route path='/:id' exact render={(props) => <Article {...props} cache={this.cache} />} />
2019-10-12 05:32:17 +00:00
</Switch>
2019-10-18 21:26:22 +00:00
<Route path='/:id/c' exact render={(props) => <Comments {...props} cache={this.cache} />} />
<ScrollToTop />
2019-08-24 05:04:51 +00:00
</Router>
</div>
);
}
}
export default App;