Finish prototype web client

This commit is contained in:
2019-08-24 05:04:51 +00:00
parent 62d68da415
commit dde6ac4566
18 changed files with 10773 additions and 0 deletions

49
webclient/src/App.js Normal file
View File

@@ -0,0 +1,49 @@
import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import './Style-light.css';
import './Style-dark.css';
import Feed from './Feed.js';
import Article from './Article.js';
import Comments from './Comments.js';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: localStorage.getItem('theme') || '',
};
}
light() {
this.setState({ theme: '' });
localStorage.setItem('theme', '');
}
dark() {
this.setState({ theme: 'dark' });
localStorage.setItem('theme', 'dark');
}
render() {
const theme = this.state.theme;
return (
<div className={theme}>
<Router>
<div className='container menu'>
<p>
<Link to='/'>QNN - Home</Link>
<span className='theme'>Theme: <a href='#' onClick={() => this.light()}>Light</a> - <a href='#' onClick={() => this.dark()}>Dark</a></span>
</p>
</div>
<Route path='/' exact component={Feed} />
<Route path='/:id' exact component={Comments} />
<Route path='/:id/a' exact component={Article} />
</Router>
</div>
);
}
}
export default App;

71
webclient/src/Article.js Normal file
View File

@@ -0,0 +1,71 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { sourceLink, infoLine } from './utils.js';
const apiUrl = 'http://news-api.dns.t0.vc/';
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,
error: false,
};
}
componentDidMount() {
const id = this.props.match.params.id;
fetch(apiUrl + id)
.then(res => res.json())
.then(
(result) => {
this.setState({ story: result.story });
localStorage.setItem(id, JSON.stringify(result.story));
},
(error) => {
this.setState({ error: true });
}
);
}
render() {
const story = this.state.story;
const error = this.state.error;
return (
<div className='article-container'>
{error ?
<p>Something went wrong.</p>
:
<div>
{story ?
<div className='article'>
<h1>{story.title}</h1>
<div className='info'>
Source: {sourceLink(story)}
</div>
{infoLine(story)}
{story.text ?
<div dangerouslySetInnerHTML={{ __html: story.text }} />
:
<p>Problem getting article :(</p>
}
</div>
:
<p>loading...</p>
}
</div>
}
</div>
);
}
}
export default Article;

84
webclient/src/Comments.js Normal file
View File

@@ -0,0 +1,84 @@
import React from 'react';
import { Link } from 'react-router-dom';
import moment from 'moment';
import { sourceLink, infoLine } from './utils.js';
const apiUrl = 'http://news-api.dns.t0.vc/';
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,
error: false,
};
}
componentDidMount() {
const id = this.props.match.params.id;
fetch(apiUrl + id)
.then(res => res.json())
.then(
(result) => {
this.setState({ story: result.story });
localStorage.setItem(id, JSON.stringify(result.story));
},
(error) => {
this.setState({ error: true });
}
);
}
displayComment(c, level) {
return (
<div className={level ? 'comment lined' : 'comment'}>
<div className='info'>
<p>{c.author || '[Deleted]'} | {moment.unix(c.date).fromNow()}</p>
</div>
<div className='text' dangerouslySetInnerHTML={{ __html: c.text }} />
{c.comments.map(i => this.displayComment(i, level + 1))}
</div>
);
}
render() {
const story = this.state.story;
const error = this.state.error;
return (
<div className='container'>
{error ?
<p>Something went wrong.</p>
:
<div>
{story ?
<div className='article'>
<h1>{story.title}</h1>
<div className='info'>
<Link to={'/' + story.id + '/a'}>View article</Link>
</div>
{infoLine(story)}
<div className='comments'>
{story.comments.map(c => this.displayComment(c, 0))}
</div>
</div>
:
<p>loading...</p>
}
</div>
}
</div>
);
}
}
export default Article;

81
webclient/src/Feed.js Normal file
View File

@@ -0,0 +1,81 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { sourceLink, infoLine } from './utils.js';
const apiUrl = 'http://news-api.dns.t0.vc/';
class Feed extends React.Component {
constructor(props) {
super(props);
this.state = {
stories: JSON.parse(localStorage.getItem('stories')) || false,
error: false,
};
}
componentDidMount() {
fetch(apiUrl)
.then(res => res.json())
.then(
(result) => {
this.setState({ stories: result.stories });
localStorage.setItem('stories', JSON.stringify(result.stories));
result.stories.slice(0, 25).forEach(x => {
fetch(apiUrl + 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 => {}
);
});
},
(error) => {
this.setState({ error: true });
}
);
}
render() {
const stories = this.state.stories;
const error = this.state.error;
return (
<div className='container'>
{error ?
<p>Something went wrong.</p>
:
<div>
{stories ?
<div>
{stories.map((x, i) =>
<div className='item'>
<div className='num'>
{i+1}.
</div>
<div className='title'>
<Link className='link' to={'/' + x.id + '/a'}>{x.title}</Link>
<span className='source'>
&#8203;({sourceLink(x)})
</span>
</div>
{infoLine(x)}
</div>
)}
</div>
:
<p>loading...</p>
}
</div>
}
</div>
);
}
}
export default Feed;

View File

@@ -0,0 +1,48 @@
.dark {
background: #202020;
color: #eeeeee;
position: absolute; top: 0; left: 0; right: 0;
min-height: 100%;
padding: 0 8px;
}
.dark a {
color: #eeeeee;
}
.dark .item {
color: #828282;
}
.dark .item a {
color: #828282;
}
.dark .item a.link {
color: #eeeeee;
}
.dark .item a.link:visited {
color: #828282;
}
.dark .item .info a.hot {
color: #CCCCCC;
}
.dark .article a {
color: #eeeeee;
border-bottom: 1px solid #aaaaaa;
}
.dark .article .info {
color: #828282;
}
.dark .article .info a {
color: #828282;
border-bottom: none;
}
.dark .comment.lined {
border-left: 1px solid #444444;
}

View File

@@ -0,0 +1,127 @@
body {
font: 1rem/1.3 sans-serif;
background: #eeeeee;
color: #000000;
}
a {
color: #000000;
text-decoration: none;
outline: none;
}
.container {
margin: 1rem auto;
max-width: 64rem;
}
.menu {
font-size: 1.1rem;
padding: 0 1rem;
}
.theme {
float: right;
}
.item {
display: table;
color: #828282;
margin-bottom: 0.6rem;
}
.item a {
color: #828282;
}
.item a:hover {
text-decoration: underline;
}
.item .num {
display: table-cell;
width: 2em;
}
.item a.link {
font-size: 1.1rem;
color: #000000;
}
.item a.link:visited {
color: #828282;
}
span.source {
margin-left: 0.4rem;
}
.item .info a.hot {
color: #444444;
}
.article-container {
font: 1.2rem/1.5 sans-serif;
margin: 1rem auto;
max-width: 44rem;
}
.article h1 {
font-size: 1.6rem;
}
.article h2 {
font-size: 1.4rem;
}
.article h3, .article h4 {
font-size: 1.3rem;
}
.article img {
width: 100%;
height: auto;
}
.article figure {
width: 100%;
height: auto;
margin: 0;
}
.article iframe {
width: 100%;
margin: 0;
}
.article a {
color: #000000;
text-decoration: none;
border-bottom: 1px solid #222222;
}
.article .info {
color: #828282;
}
.article .info a {
color: #828282;
border-bottom: none;
}
.article .info a:hover {
text-decoration: underline;
}
.comments {
margin-left: -1rem;
}
.comment {
padding-left: 1rem;
}
.comment.lined {
border-left: 1px solid #cccccc;
}
.comment .text {
margin-top: -0.5rem;
}

5
webclient/src/index.js Normal file
View File

@@ -0,0 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));

22
webclient/src/utils.js Normal file
View File

@@ -0,0 +1,22 @@
import React from 'react';
import moment from 'moment';
import { Link } from 'react-router-dom';
export const sourceLink = (story) => {
const url = story.url || story.link;
const urlObj = new URL(url);
const host = urlObj.hostname.replace(/^www\./, '');
return (<a className='source' href={url}>{host}</a>);
};
export const infoLine = (story) =>
<div className='info'>
{story.score} points
by <a href={story.author_link}>{story.author}</a>
&#8203; {moment.unix(story.date).fromNow()}
&#8203; on <a href={story.link}>{story.source}</a> | &#8203;
<Link className={story.num_comments > 99 ? 'hot' : ''} to={'/' + story.id}>
{story.num_comments} comment{story.num_comments !== 1 && 's'}
</Link>
</div>
;