qotnews/webclient/src/App.js

92 lines
2.5 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';
import { ForwardDot } from './utils.js';
2019-10-12 05:32:17 +00:00
import Search from './Search.js';
2019-11-08 05:55:30 +00:00
import Submit from './Submit.js';
import ScrollToTop from './ScrollToTop.js';
import Feed from './pages/Feed.js';
import Article from './pages/Article.js';
import Comments from './pages/Comments.js';
import Results from './pages/Results.js';
2020-12-01 03:47:04 +00:00
const pagingKey = (props) => {
const query = new URLSearchParams(props.location.search);
const skip = query.get('skip') || 0;
const limit = query.get('limit') || 20;
return `skip=${skip}&limit=${limit}`;
}
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-11-08 05:55:30 +00:00
<Route path='/(|search)' component={Submit} />
2019-08-24 05:04:51 +00:00
</div>
2019-10-12 05:32:17 +00:00
2020-12-01 03:47:04 +00:00
<Route path='/' exact render={(props) => <Feed {...props} updateCache={this.updateCache} key={pagingKey(props)} />} />
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>
2020-11-30 05:11:45 +00:00
<Route path='/:id/c' exact render={(props) => <Comments {...props} cache={this.cache} key={`${props.match.params.id}`} />} />
<ForwardDot />
<ScrollToTop />
2019-08-24 05:04:51 +00:00
</Router>
</div>
);
}
}
export default App;