import React from 'react'; import { Helmet } from 'react-helmet'; import AbortController from 'abort-controller'; import { StoryItem } from '../components/StoryItem.js'; class Results extends React.Component { constructor(props) { super(props); this.state = { stories: false, error: false, }; this.controller = null; } performSearch = () => { if (this.controller) { this.controller.abort(); } this.controller = new AbortController(); const signal = this.controller.signal; const search = this.props.location.search; const params = new URLSearchParams(search); params.set('skip', params.get('skip') || 0); params.set('limit', params.get('limit') || 20); fetch('/api/search?' + params.toString(), { method: 'get', signal: signal }) .then(res => res.json()) .then( (result) => { this.setState({ stories: result.results }); }, (error) => { if (error.message !== 'The operation was aborted. ') { this.setState({ error: true }); } } ); } 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; const search = this.props.location.search; const params = new URLSearchParams(search); const q = params.get('q') || ''; const skip = params.get('skip') || 0; const limit = params.get('limit') || 20; return (
Feed - QotNews {error &&

Connection error?

} {stories ? <>

Search results:

{stories ? stories.map(story => ) :

loading...

}
:

loading...

}
{Number(skip) > 0 && Previous} {stories.length == Number(limit) && Next}
); } } export default Results;