Passed
Push — feature/player-stats ( 32be2a )
by Kevin Van
05:26
created

TeamCalendarMetaMatches.componentWillUnmount   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import React, { Component, Fragment } from "react"
2
import { graphql, StaticQuery } from "gatsby"
3
import MatchWithLogo from "./match-with-logo"
4
5
import "./team-calendar-meta-matches.scss"
6
7
class TeamCalendarMetaMatches extends Component {
8
  constructor(props) {
9
    super(props)
10
11
    this.state = {
12
      data: [],
13
      loading: true,
14
    }
15
16
    this.uuid = props.division.toLowerCase()
17
18
    this.apiServerUrl = props.config.site.siteMetadata.serverUrl
19
    this.apiRefreshRate = props.config.site.siteMetadata.refreshRate
20
    this.timeout = {}
21
  }
22
23
  updateData() {
24
    const { season, region, division, regnumber } = this.props
25
26
    fetch(
27
      `${this.apiServerUrl}/meta/${season}/${region}/${division}/${regnumber}`
28
    )
29
      .then((response) => response.json())
30
      .then((json) => this.setState({ data: json, loading: false }))
31
32
    this.timeout = setTimeout(() => {
33
      this.updateData(() => {})
34
    }, this.apiRefreshRate)
35
  }
36
37
  componentDidMount() {
38
    this.updateData()
39
  }
40
41
  componentWillUnmount() {
42
    clearInterval(this.timeout)
43
  }
44
45
  render() {
46
    if (this.state.loading === false && this.state.data) {
47
      const { next, previous } = this.state.data
48
49
      if (previous || next) {
50
        return (
51
          <Fragment>
52
            <div className={"team-calendar-meta-matches"}>
53
              {previous && (
54
                <MatchWithLogo match={previous.match} lazyload={true} />
55
              )}
56
              {next && <MatchWithLogo match={next.match} lazyload={true} />}
57
            </div>
58
          </Fragment>
59
        )
60
      } else {
61
        return null
62
      }
63
    } else {
64
      return null
65
    }
66
  }
67
}
68
69
const query = graphql`
70
  query {
71
    site {
72
      siteMetadata {
73
        serverUrl
74
        refreshRate
75
      }
76
    }
77
  }
78
`
79
80
export default ({ season, region, division, regnumber }) => (
81
  <StaticQuery
82
    query={query}
83
    render={(data) => (
84
      <TeamCalendarMetaMatches
85
        config={data}
86
        season={season}
87
        region={region}
88
        division={division}
89
        regnumber={regnumber}
90
      />
91
    )}
92
  />
93
)
94