aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Thread.js
blob: a1abdd6cdc17c50f24371f068848dd7c79607b47 (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
import 'whatwg-fetch';
import React from 'react';
import * as qs from 'query-string';

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

export default class Thread extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      replies: []
    };
  }
  componentDidMount() {
    this.loadReplies();
  }
  loadReplies() {
    this.setState({ replies: []})
    const { mid } = this.props.match.params;
    let params = { 
      mid: mid
    }
    if (this.props.visitor && this.props.visitor.hash) {
      params.hash = this.props.visitor.hash
    };
    const url = `https://api.juick.com/thread?${qs.stringify(params)}`;
    fetch(url)
        .then(response => {
            return response.json()
        })
        .then(data =>
            this.setState({ replies: data })
        ).catch(ex => {
      console.log(ex);
    });
  }
  render() {
    return this.state.replies && this.state.replies.length > 0 ? (
      <Message data={this.state.replies[0]} visitor={this.props.visitor}/>
    ) : (
      <Spinner />
    );
  }
}