Passed
Push — feature/typescript ( 497e7e )
by Kevin Van
06:13
created

Clublogo.tsx ➔ imageErrorHandler   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import React, { FunctionComponent } from "react"
2
import { graphql, useStaticQuery } from "gatsby"
3
import LazyLoad from "react-lazyload"
4
5
import defaultLogo from "../images/default.png"
6
import flatLogoElewijt from "../images/logo-flat.png"
7
8
interface ClublogoProps {
9
  lazyload: boolean
10
  regNumber: string
11
  title: string
12
  className: string
13
}
14
15
interface ClublogoData {
16
  site: {
17
    siteMetadata: {
18
      logoUrl: string
19
    }
20
  }
21
}
22
23
const getLogoImageSrcUrl = (apiUrl: string, regNumber: string) => {
24
  if (regNumber === "00055") {
25
    return flatLogoElewijt
26
  }
27
  return `${apiUrl}/${regNumber}`
28
}
29
30
const ClubLogo: FunctionComponent<ClublogoProps> = ({
31
  lazyload,
32
  regNumber,
33
  title,
34
  className,
35
}) => {
36
  function imageErrorHandler(e: React.SyntheticEvent<HTMLImageElement, Event>) {
37
    const el = e.target as HTMLImageElement
38
    el.onerror = null
39
    el.src = defaultLogo
40
  }
41
42
  const data: ClublogoData = useStaticQuery(graphql`
43
    query {
44
      site {
45
        siteMetadata {
46
          logoUrl
47
        }
48
      }
49
    }
50
  `)
51
52
  const logoSourceUrl = getLogoImageSrcUrl(
53
    data.site.siteMetadata.logoUrl,
54
    regNumber
55
  )
56
57
  const image = (
58
    <img
59
      src={logoSourceUrl}
60
      role="presentation"
61
      onError={imageErrorHandler}
62
      alt={title}
63
      className={className}
64
    />
65
  )
66
67
  if (lazyload === true) {
68
    return <LazyLoad debounce={false}>{image}</LazyLoad>
69
  } else {
70
    return image
71
  }
72
}
73
74
ClubLogo.defaultProps = {
75
  lazyload: false,
76
  regNumber: "00055",
77
  title: "KCVV Elewijt",
78
  className: "",
79
}
80
81
export default ClubLogo
82