2019-08-24 05:04:51 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import moment from 'moment';
|
2019-08-25 08:50:49 +00:00
|
|
|
import { sourceLink, infoLine, ToggleDot } from './utils.js';
|
2019-08-24 05:04:51 +00:00
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
const apiUrl = 'https://news-api.t0.vc/';
|
2019-08-24 05:04:51 +00:00
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-26 01:37:50 +00:00
|
|
|
displayComment(story, c, level) {
|
2019-08-24 05:04:51 +00:00
|
|
|
return (
|
|
|
|
<div className={level ? 'comment lined' : 'comment'}>
|
|
|
|
<div className='info'>
|
2019-08-26 01:37:50 +00:00
|
|
|
<p>{c.author === story.author ? '[OP]' : ''} {c.author || '[Deleted]'} | {moment.unix(c.date).fromNow()}</p>
|
2019-08-24 05:04:51 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='text' dangerouslySetInnerHTML={{ __html: c.text }} />
|
|
|
|
|
2019-08-30 06:22:26 +00:00
|
|
|
{level < 5 ?
|
2019-08-26 01:37:50 +00:00
|
|
|
c.comments.map(i => this.displayComment(story, i, level + 1))
|
|
|
|
:
|
|
|
|
<div className='info'><p>[replies snipped]</p></div>
|
|
|
|
}
|
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'>
|
|
|
|
<h1>{story.title}</h1>
|
2019-08-24 05:04:51 +00:00
|
|
|
|
2019-09-24 08:23:14 +00:00
|
|
|
<div className='info'>
|
|
|
|
<Link to={'/' + story.id + '/a'}>View article</Link>
|
|
|
|
</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;
|