import 'whatwg-fetch'; import React from 'react'; import PropTypes from 'prop-types'; import * as qs from 'query-string'; import Message from './Message'; import Spinner from './Spinner'; export default class Discover extends React.Component { constructor(props) { super(props); this.state = { msgs: [] }; this.loadMessages = this.loadMessages.bind(this); } componentDidMount() { this.loadMessages(); } componentWillReceiveProps(nextProps) { if (this.props.location.search != nextProps.location.search || this.props.visitor != nextProps.visitor) { this.loadMessages(nextProps.location.search) } } loadMessages(filter = '') { this.setState({ msgs: [] }) let params = qs.parse(filter) || {} let url = 'https://api.juick.com/messages'; if (this.props.visitor && this.props.visitor.hash) { params.hash = this.props.visitor.hash; } if (Object.keys(params).length > 0) { url = `${url}?${qs.stringify(params)}`; } fetch(url) .then(response => { return response.json() }) .then(data => this.setState({ msgs: data }) ).catch(ex => { console.log(ex); }); } render() { var nodes = this.state.msgs.map(msg => { return (); }); return this.state.msgs.length > 0 ? (
{nodes}
) : ; } } Discover.propTypes = { msgs: PropTypes.array };