aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Feeds.js
blob: 0216a1b4c198ddcadfeb5a88937be120e3843277 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import * as qs from 'query-string';

import Message from './Message';
import Spinner from './Spinner';

export function Discover(props) {
  return (<Feed query={{ baseUrl: "https://api.juick.com/messages" }} {...props} />)
}

export function Discussions(props) {
  return (<Feed authRequired="true" query={{ baseUrl: "https://api.juick.com/messages/discussions" }} {...props} />)
}

export function Blog(props) {
  const { user } = props.match.params;
  return (<Feed query={{ baseUrl: `https://api.juick.com/messages`, search: { uname: user } }} {...props} />)
}

export function Tag(props) {
  const { tag } = props.match.params;
  return (<Feed query={{ baseUrl: `https://api.juick.com/messages`, search: { tag: tag } }} {...props} />)
}

class Feed extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      msgs: []
    };
    this.loadMessages = this.loadMessages.bind(this);
  }
  componentDidMount() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    this.loadMessages(this.props.visitor.hash, this.props.location.search);
  }
  componentWillReceiveProps(nextProps) {
    if (this.props.location.search != nextProps.location.search
      || this.props.visitor != nextProps.visitor) {
      this.loadMessages(nextProps.visitor.hash, nextProps.location.search)
    }
  }
  loadMessages(hash = '', filter = '') {
    this.setState({ msgs: [] })
    let params = Object.assign({}, qs.parse(filter) || {}, this.props.query.search || {});
    let url = this.props.query.baseUrl;
    if (hash) {
      params.hash = hash;
    }
    if (Object.keys(params).length > 0) {
      url = `${url}?${qs.stringify(params)}`;
    }
    if (!params.hash && this.props.authRequired) {
      this.props.history.push('/')
    }
    fetch(url)
      .then(response => {
        return response.json()
      })
      .then(data =>
        this.setState({ msgs: data })
      ).catch(ex => {
        console.log(ex);
      });
  }

  render() {
    const { tag } = qs.parse(this.props.location.search || {});
    const nodes = (
      <React.Fragment>
        {
          tag && (
            <p className="page">
              <Link to={{ pathname: `/tag/${tag}` }}>
                <span> All posts with tag&nbsp;</span><b>{tag}</b>
              </Link>
            </p>
          )
        }
        {this.state.msgs.map(msg =>
          <Message key={msg.mid} data={msg} visitor={this.props.visitor} />)
        }
      </React.Fragment>
    );
    return this.state.msgs.length > 0 ? (
      <div className="msgs" id="content">{nodes}</div>
    ) : <div className="msgs" id="content"><Spinner /><Spinner /><Spinner /><Spinner /></div>;
  }
}

Feed.propTypes = {
  msgs: PropTypes.array,
  query: PropTypes.shape({
    baseUrl: PropTypes.string.isRequired,
    search: PropTypes.object
  })
};