aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Feeds.js
blob: 3cbc7d4bd090d25c27ef8e0706049325f0c6ad11 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react';
import PropTypes from 'prop-types';
import ReactRouterPropTypes from 'react-router-prop-types';
import { Link } from 'react-router-dom';
import * as qs from 'query-string';
import moment from 'moment';

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

import  { getMessages } from '../api';
import { UserType } from './Types';

export function Discover(props) {
  const query = {
    baseUrl: '/messages',
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={false} query={query} {...props} />);
}

export function Discussions(props) {
  const query = {
    baseUrl: '/messages/discussions',
    pageParam: 'to'
  };
  return (<Feed authRequired={true} query={query} {...props} />);
}

export function Blog(props) {
  const { user } = props.match.params;
  const query = {
    baseUrl: '/messages',
    search: {
      uname: user
    },
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={false} query={query} {...props} />);
}

export function Tag(props) {
  const { tag } = props.match.params;
  const query = {
    baseUrl: '/messages',
    search: {
      tag: tag
    },
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={false} query={query} {...props} />);
}

export function Home(props) {
  const query = {
    baseUrl: '/home',
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={true} query={query} {...props} />);
}

class Feed extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      msgs: [],
      loading: false
    };
  }
  componentDidMount() {
    this.loadMessages(this.props.visitor.hash, this.props.location.search);
  }
  getSnapshotBeforeUpdate(prevProps) {
    return (this.props.location.search != prevProps.location.search
      || this.props.visitor != prevProps.visitor);
  }
  componentDidUpdate(prevProps, prevState, shouldReload) {
    if (shouldReload) {
      this.loadMessages(this.props.visitor.hash, this.props.location.search);
    }
  }
  loadMessages = (hash = '', filter = '') => {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    this.setState({ msgs: [], loading: true });
    const filterParams = qs.parse(filter);
    let params = Object.assign({}, filterParams || {}, this.props.query.search || {});
    let url = this.props.query.baseUrl;
    if (hash) {
      params.hash = hash;
    }
    if (!params.hash && this.props.authRequired) {
      this.props.history.push('/');
    }
    getMessages(url, params)
      .then(response => {
        const { data } = response;
        const { pageParam } = this.props.query;
        const lastMessage = data.slice(-1)[0] || {};
        const pageValue = pageParam === 'before_mid' ? lastMessage.mid : moment.utc(lastMessage.updated).valueOf();
        let newFilter = Object.assign({}, filterParams);
        newFilter[pageParam] = pageValue;
        const nextpage = `?${qs.stringify(newFilter)}`;
        this.setState({ msgs: data, loading: false, nextpage: nextpage });
      }).catch(ex => {
        this.setState({ error: true });
      });
  }

  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} />)
        }
        {
          this.state.msgs.length >= 20 && (
            <p className="page">
              <Link to={{ pathname: this.props.location.pathname, search: this.state.nextpage }} rel="prev">Next </Link>
            </p>
          )
        }
      </React.Fragment>
    );
    return this.state.msgs.length > 0 ? (
      <div className="msgs">{nodes}</div>
    ) : this.state.error ? <div>error</div> : this.state.loading ? <div className="msgs"><Spinner /><Spinner /><Spinner /><Spinner /></div> : <div>No more messages</div>;
  }
}

Blog.propTypes = {
  match: ReactRouterPropTypes.match.isRequired
};

Tag.propTypes = {
  match: ReactRouterPropTypes.match.isRequired
};

Feed.propTypes = {
  authRequired: PropTypes.bool,
  visitor: UserType,
  history: ReactRouterPropTypes.history.isRequired,
  location: ReactRouterPropTypes.location.isRequired,
  msgs: PropTypes.array,
  query: PropTypes.shape({
    baseUrl: PropTypes.string.isRequired,
    search: PropTypes.object,
    pageParam: PropTypes.string.isRequired
  })
};