2019-08-24 05:04:51 +00:00
|
|
|
|
import React from 'react';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
2019-10-10 21:52:28 +00:00
|
|
|
|
import { HashLink } from 'react-router-hash-link';
|
|
|
|
|
import { Helmet } from 'react-helmet';
|
2019-08-24 05:04:51 +00:00
|
|
|
|
import moment from 'moment';
|
2019-10-12 23:41:31 +00:00
|
|
|
|
import localForage from 'localforage';
|
2019-10-22 07:31:59 +00:00
|
|
|
|
import { infoLine, ToggleDot } from './utils.js';
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
|
|
|
|
class Article extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
2019-10-18 21:26:22 +00:00
|
|
|
|
const id = this.props.match.params.id;
|
|
|
|
|
const cache = this.props.cache;
|
|
|
|
|
|
|
|
|
|
if (id in cache) console.log('cache hit');
|
|
|
|
|
|
2019-08-24 05:04:51 +00:00
|
|
|
|
this.state = {
|
2019-10-18 21:26:22 +00:00
|
|
|
|
story: cache[id] || false,
|
2019-08-24 05:04:51 +00:00
|
|
|
|
error: false,
|
2020-10-26 21:57:10 +00:00
|
|
|
|
collapsed: [],
|
|
|
|
|
expanded: [],
|
2019-08-24 05:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-18 21:26:22 +00:00
|
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
|
componentDidMount() {
|
2019-08-24 05:04:51 +00:00
|
|
|
|
const id = this.props.match.params.id;
|
|
|
|
|
|
2019-10-12 23:41:31 +00:00
|
|
|
|
localForage.getItem(id)
|
|
|
|
|
.then(
|
|
|
|
|
(value) => {
|
|
|
|
|
this.setState({ story: value });
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
|
fetch('/api/' + id)
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
(result) => {
|
|
|
|
|
this.setState({ story: result.story }, () => {
|
2019-10-10 21:52:28 +00:00
|
|
|
|
const hash = window.location.hash.substring(1);
|
|
|
|
|
if (hash) {
|
|
|
|
|
document.getElementById(hash).scrollIntoView();
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-10-12 23:41:31 +00:00
|
|
|
|
localForage.setItem(id, result.story);
|
2019-10-12 05:32:17 +00:00
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
this.setState({ error: true });
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-08-24 05:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 21:57:10 +00:00
|
|
|
|
collapseComment(cid) {
|
|
|
|
|
this.setState(prevState => ({
|
|
|
|
|
...prevState,
|
|
|
|
|
collapsed: [...prevState.collapsed, cid],
|
|
|
|
|
expanded: prevState.expanded.filter(x => x !== cid),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expandComment(cid) {
|
|
|
|
|
this.setState(prevState => ({
|
|
|
|
|
...prevState,
|
|
|
|
|
collapsed: prevState.collapsed.filter(x => x !== cid),
|
|
|
|
|
expanded: [...prevState.expanded, cid],
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countComments(c) {
|
|
|
|
|
return c.comments.reduce((sum, x) => sum + this.countComments(x), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 01:37:50 +00:00
|
|
|
|
displayComment(story, c, level) {
|
2020-10-26 21:57:10 +00:00
|
|
|
|
const cid = c.author+c.date;
|
|
|
|
|
|
|
|
|
|
const collapsed = this.state.collapsed.includes(cid);
|
|
|
|
|
const expanded = this.state.expanded.includes(cid);
|
|
|
|
|
|
|
|
|
|
const hidden = collapsed || (level == 4 && !expanded);
|
|
|
|
|
const hasChildren = c.comments.length !== 0;
|
|
|
|
|
|
2019-08-24 05:04:51 +00:00
|
|
|
|
return (
|
2020-10-26 21:57:10 +00:00
|
|
|
|
<div className={level ? 'comment lined' : 'comment'} key={cid}>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
<div className='info'>
|
2019-10-10 21:52:28 +00:00
|
|
|
|
<p>
|
|
|
|
|
{c.author === story.author ? '[OP]' : ''} {c.author || '[Deleted]'}
|
2020-10-26 21:57:10 +00:00
|
|
|
|
{' '} | <HashLink to={'#'+cid} id={cid}>{moment.unix(c.date).fromNow()}</HashLink>
|
|
|
|
|
|
|
|
|
|
{hidden || hasChildren &&
|
|
|
|
|
<span className='collapser pointer' onClick={() => this.collapseComment(cid)}>–</span>
|
|
|
|
|
}
|
2019-10-10 21:52:28 +00:00
|
|
|
|
</p>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
2020-10-27 00:41:36 +00:00
|
|
|
|
<div className={collapsed ? 'text hidden' : 'text'} dangerouslySetInnerHTML={{ __html: c.text }} />
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
2020-10-26 21:57:10 +00:00
|
|
|
|
{hidden && hasChildren ?
|
|
|
|
|
<div className='comment lined info pointer' onClick={() => this.expandComment(cid)}>[show {this.countComments(c)-1} more]</div>
|
2019-08-26 01:37:50 +00:00
|
|
|
|
:
|
2020-10-26 21:57:10 +00:00
|
|
|
|
c.comments.map(i => this.displayComment(story, i, level + 1))
|
2019-08-26 01:37:50 +00:00
|
|
|
|
}
|
2019-08-24 05:04:51 +00:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-08-25 08:50:49 +00:00
|
|
|
|
const id = this.props.match.params.id;
|
2019-08-24 05:04:51 +00:00
|
|
|
|
const story = this.state.story;
|
|
|
|
|
const error = this.state.error;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className='container'>
|
2019-09-24 08:23:14 +00:00
|
|
|
|
{error && <p>Connection error?</p>}
|
|
|
|
|
{story ?
|
|
|
|
|
<div className='article'>
|
2019-10-10 21:52:28 +00:00
|
|
|
|
<Helmet>
|
|
|
|
|
<title>{story.title} - QotNews Comments</title>
|
|
|
|
|
</Helmet>
|
|
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
|
<h1>{story.title}</h1>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
|
<div className='info'>
|
2019-10-10 21:52:28 +00:00
|
|
|
|
<Link to={'/' + story.id}>View article</Link>
|
2019-09-24 08:23:14 +00:00
|
|
|
|
</div>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
|
{infoLine(story)}
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
|
<div className='comments'>
|
|
|
|
|
{story.comments.map(c => this.displayComment(story, c, 0))}
|
|
|
|
|
</div>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
</div>
|
2019-09-24 08:23:14 +00:00
|
|
|
|
:
|
|
|
|
|
<p>loading...</p>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
}
|
2019-09-24 08:23:14 +00:00
|
|
|
|
<ToggleDot id={id} article={true} />
|
2019-08-24 05:04:51 +00:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Article;
|