Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 82 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 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 |