2019-10-12 05:32:17 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { Helmet } from 'react-helmet';
|
2020-11-10 02:22:27 +00:00
|
|
|
import { sourceLink, infoLine, getLogoUrl } from './utils.js';
|
2019-11-07 22:08:28 +00:00
|
|
|
import AbortController from 'abort-controller';
|
2019-10-12 05:32:17 +00:00
|
|
|
|
|
|
|
class Results extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
stories: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2019-11-07 22:08:28 +00:00
|
|
|
this.controller = null;
|
2019-10-12 05:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
performSearch = () => {
|
2019-11-07 22:08:28 +00:00
|
|
|
if (this.controller) {
|
|
|
|
this.controller.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.controller = new AbortController();
|
|
|
|
const signal = this.controller.signal;
|
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
const search = this.props.location.search;
|
2019-11-07 22:08:28 +00:00
|
|
|
fetch('/api/search' + search, { method: 'get', signal: signal })
|
2019-10-12 05:32:17 +00:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(
|
|
|
|
(result) => {
|
|
|
|
this.setState({ stories: result.results });
|
|
|
|
},
|
|
|
|
(error) => {
|
2019-11-07 22:08:28 +00:00
|
|
|
if (error.message !== 'The operation was aborted. ') {
|
|
|
|
this.setState({ error: true });
|
|
|
|
}
|
2019-10-12 05:32:17 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.performSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.location.search !== prevProps.location.search) {
|
|
|
|
this.performSearch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const stories = this.state.stories;
|
|
|
|
const error = this.state.error;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='container'>
|
|
|
|
<Helmet>
|
|
|
|
<title>Feed - QotNews</title>
|
|
|
|
</Helmet>
|
|
|
|
{error && <p>Connection error?</p>}
|
|
|
|
{stories ?
|
2020-08-14 03:58:11 +00:00
|
|
|
<>
|
|
|
|
<p>Search results:</p>
|
|
|
|
<div className='comment lined'>
|
|
|
|
{stories.length ?
|
2020-11-03 03:45:56 +00:00
|
|
|
stories.map(x =>
|
|
|
|
<div className='item' key={x.id}>
|
2020-08-14 03:58:11 +00:00
|
|
|
<div className='title'>
|
|
|
|
<Link className='link' to={'/' + x.id}>
|
2020-11-10 02:22:27 +00:00
|
|
|
<img className='source-logo' src={getLogoUrl(x)} alt='source logo' /> {x.title}
|
2020-08-14 03:58:11 +00:00
|
|
|
</Link>
|
2019-10-12 05:32:17 +00:00
|
|
|
|
2020-08-14 03:58:11 +00:00
|
|
|
<span className='source'>
|
2020-11-03 03:45:56 +00:00
|
|
|
({sourceLink(x)})
|
2020-08-14 03:58:11 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
2019-10-12 05:32:17 +00:00
|
|
|
|
2020-08-14 03:58:11 +00:00
|
|
|
{infoLine(x)}
|
|
|
|
</div>
|
|
|
|
)
|
2020-11-10 02:22:27 +00:00
|
|
|
:
|
2020-08-14 03:58:11 +00:00
|
|
|
<p>none</p>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</>
|
2020-11-10 02:22:27 +00:00
|
|
|
:
|
2019-10-12 05:32:17 +00:00
|
|
|
<p>loading...</p>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Results;
|