aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Thread.js
blob: 1a3ac862d4da9f7828725b8c27be72a087a648a7 (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]}/>
    ) : (
      <Spinner />
    );
  }
}