Total Complexity | 4 |
Complexity/F | 2 |
Lines of Code | 84 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import React, { Component } from "react" |
||
2 | import { graphql, StaticQuery } from "gatsby" |
||
3 | import LazyLoad from "react-lazy-load" |
||
4 | |||
5 | import defaultLogo from "../images/default.png" |
||
6 | import flatLogoElewijt from "../images/logo-flat.png" |
||
7 | |||
8 | /** |
||
9 | * Render club logo based on the registration number of a club. |
||
10 | * |
||
11 | * If Logo was not found (HTTP error) a default shield is shown as fallback. |
||
12 | */ |
||
13 | class ClubLogo extends Component { |
||
14 | constructor(props) { |
||
15 | super(props) |
||
16 | |||
17 | this.apiLogoUrl = props.config.site.siteMetadata.logoUrl |
||
18 | } |
||
19 | |||
20 | // Official logo @ KBVB is still old / wannabe 3D-ish. |
||
21 | getLogoImageSrcUrl(regNumber) { |
||
22 | if (regNumber === "00055") { |
||
23 | return flatLogoElewijt |
||
24 | } |
||
25 | return `${this.apiLogoUrl}/${regNumber}` |
||
26 | } |
||
27 | |||
28 | render() { |
||
29 | const { lazyload, regNumber, title, className } = this.props |
||
30 | const logoSourceUrl = this.getLogoImageSrcUrl(regNumber) |
||
31 | const image = ( |
||
32 | <img |
||
33 | src={logoSourceUrl} |
||
34 | role="presentation" |
||
35 | onError={({ target }) => { |
||
36 | target.onerror = null |
||
37 | target.src = defaultLogo |
||
38 | }} |
||
39 | alt={title} |
||
40 | className={className} |
||
41 | /> |
||
42 | ) |
||
43 | |||
44 | if (lazyload === true) { |
||
45 | return <LazyLoad debounce={false}>{image}</LazyLoad> |
||
46 | } else { |
||
47 | return image |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | // Retrieve endpoint of the logo's api from the site metadata (gatsby-config.js). |
||
53 | const query = graphql` |
||
54 | query { |
||
55 | site { |
||
56 | siteMetadata { |
||
57 | logoUrl |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | ` |
||
62 | |||
63 | export default ({ |
||
64 | // If no regnumber was given, we return the KCVV Elewijt logo by default. |
||
65 | regNumber = "00055", |
||
66 | title = "KCVV Elewijt", |
||
67 | className = "", |
||
68 | lazyload = false, |
||
69 | }) => ( |
||
70 | <StaticQuery |
||
71 | query={query} |
||
72 | render={(data) => ( |
||
73 | <ClubLogo |
||
74 | // Data is the result of our query. |
||
75 | config={data} |
||
76 | regNumber={regNumber} |
||
77 | title={title} |
||
78 | className={className} |
||
79 | lazyload={lazyload} |
||
80 | /> |
||
81 | )} |
||
82 | /> |
||
83 | ) |
||
84 |