Passed
Push — feature/psd-api ( 1320f3...32e3af )
by Kevin Van
12:42 queued 07:19
created

PsdTraining.updateData   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
1
import React, { Component } from "react"
2
import { graphql, StaticQuery } from "gatsby"
3
4
class PsdTraining extends Component {
5
  constructor(props) {
6
    super(props)
7
    this.state = {
8
      data: [],
9
      loading: true,
10
    }
11
    this.psdApiUrl = props.config.site.siteMetadata.psdApiUrl
12
    this.psdApiKey = props.config.site.siteMetadata.psdApiKey
13
    this.psdApiSecret = props.config.site.siteMetadata.psdApiSecret
14
    this.psdClubKey = props.config.site.siteMetadata.psdClubKey
15
    this.apiRefreshRate = props.config.site.siteMetadata.refreshRate
16
    this.timeout = {}
17
    this.headers = {
18
      "Content-Type": "application/json",
19
      "Accept-Language": "nl-BE",
20
      "x-api-club": this.psdClubKey,
21
      "x-api-key": this.psdApiKey,
22
      Authorization: this.psdApiSecret,
23
    }
24
  }
25
26
  render() {
27
    if (this.state.loading === false && this.state.data) {
28
      return <div>Hier ist ze</div>
29
    } else {
30
      return <div>Loading...</div>
31
    }
32
  }
33
34
  updateData() {
35
    fetch(
36
      `${this.psdApiUrl}`, this.headers
37
    )
38
      .then((response) => response.json())
39
      .then((json) => this.setState({ data: json, loading: false }))
40
41
    this.timeout = setTimeout(() => {
42
      this.updateData(() => {})
43
    }, this.apiRefreshRate)
44
  }
45
46
  componentDidMount() {
47
    this.updateData()
48
  }
49
50
  componentWillUnmount() {
51
    clearInterval(this.timeout)
52
  }
53
}
54
55
const query = graphql`
56
  query {
57
    site {
58
      siteMetadata {
59
        psdApiUrl
60
        psdApiKey
61
        psdApiSecret
62
        psdClubKey
63
        refreshRate
64
      }
65
    }
66
  }
67
`
68
69
export default () => (
70
  <StaticQuery query={query} render={(data) => <PsdTraining config={data} />} />
71
)
72