Passed
Push — feature/full-redesign ( 15ceb9...e2df01 )
by Kevin Van
04:43
created

src/templates/Team.tsx   B

Complexity

Total Complexity 43
Complexity/F 0

Size

Lines of Code 224
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 134
mnd 43
bc 43
fnc 0
dl 0
loc 224
rs 8.96
bpm 0
cpm 0
noi 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like src/templates/Team.tsx often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { graphql } from "gatsby"
2
import { GatsbyImage, getSrc, StaticImage } from "gatsby-plugin-image"
3
import React from "react"
4
import { Seo } from "../components/Seo"
5
import Layout from "../layouts"
6
import { TeamQuery } from "../Types/Team"
7
import ReactFitText from "@kennethormandy/react-fittext"
8
import "./Team.scss"
9
import TeamStats from "../components/TeamStats"
10
import Lineup from "../components/Lineup"
11
12
const Team = ({ data: { nodeTeam } }: TeamQuery) => {
13
  const heroImage = nodeTeam?.relationships?.field_media_article_image
14
  const teamPicture = heroImage?.relationships?.field_media_image?.localFile?.childImageSharp?.gatsbyImageData && (
15
    <GatsbyImage
16
      image={heroImage.relationships?.field_media_image?.localFile?.childImageSharp?.gatsbyImageData}
17
      alt={heroImage?.field_media_image?.alt || ``}
18
      className={`team-detail__team-picture`}
19
    />
20
  )
21
22
  // Helper variable so we don't have to do the check over and over again.
23
  const hasDivision = nodeTeam?.field_fb_id || nodeTeam?.field_fb_id_2 || nodeTeam?.field_vv_id
24
  const articles = nodeTeam?.relationships?.node__article || []
25
  const playersByPosition = false
26
27
  return (
28
    <Layout>
29
      <header className="page_header__wrapper page_header__wrapper--inset-large">
30
        <div className="page_header">
31
          <h1 className="team__header__name">
32
            {/* > GEWESTELIJKE U13 K */}
33
            <span className="team__header_name__division">
34
              <ReactFitText compressor={1.5}>{nodeTeam?.field_division_full}</ReactFitText>
35
            </span>
36
            {/* > The A-team */}
37
            <span className="team__header_name__division__tagline">{nodeTeam?.field_tagline}</span>
38
          </h1>
39
          {hasDivision && (
40
            <div className="team__header_name__division__number" aria-hidden="true">
41
              {nodeTeam?.field_fb_id_2 ? nodeTeam?.field_fb_id_2 : nodeTeam?.field_fb_id}
42
            </div>
43
          )}
44
        </div>
45
      </header>
46
      {(playersByPosition || hasDivision) && (
47
        <nav className="team__sub_navigation">
48
          {/* Foundation tabs structure */}
49
          <ul
50
            className="tabs team__sub_navigation__tabs"
51
            data-tabs
52
            data-deep-link="true"
53
            data-update-history="true"
54
            data-deep-link-smudge="true"
55
            data-deep-link-smudge-delay="500"
56
            id="team-subnavigation_tabs"
57
          >
58
            <li className="tabs-title is-active">
59
              <a href="#team-info" className="rich-link-center">
60
                Info
61
              </a>
62
            </li>
63
            {/* Youth teams don't have lineups, so we don't show the tab link. */}
64
            {playersByPosition && (
65
              <li className="tabs-title">
66
                <a href="#team-lineup" className="rich-link-center">
67
                  Lineup
68
                </a>
69
              </li>
70
            )}
71
            {hasDivision && (
72
              <>
73
                <li className={`tabs-title`}>
74
                  <a data-tabs-target="team-matches" href="#team-matches" className="rich-link-center">
75
                    Wedstrijden
76
                  </a>
77
                </li>
78
                <li className={`tabs-title`}>
79
                  <a data-tabs-target="team-ranking" href="#team-ranking" className="rich-link-center">
80
                    Stand
81
                  </a>
82
                </li>
83
              </>
84
            )}
85
          </ul>
86
        </nav>
87
      )}
88
      <article className="page__wrapper team__wrapper">
89
        <div className="tabs-content" data-tabs-content="team-subnavigation_tabs">
90
          <div className="tabs-panel is-active" id="team-info">
91
            <div className="team_info__hero_image">{teamPicture || ``}</div>
92
            {nodeTeam?.field_contact_info && (
93
              <div
94
                className={`team_info__contact page__wrapper page__wrapper--limited`}
95
                dangerouslySetInnerHTML={{
96
                  __html: nodeTeam?.field_contact_info.processed || ``,
97
                }}
98
              />
99
            )}
100
            {nodeTeam?.field_vv_id && <TeamStats teamId={+nodeTeam?.field_vv_id} />}
101
            {nodeTeam?.relationships?.field_staff && !playersByPosition && (
102
              <main className={`team-detail__lineup team-detail__lineup--staff-only`}>
103
                <Lineup title="Stafleden" lineup={nodeTeam?.relationships?.field_staff} />
104
              </main>
105
            )}
106
          </div>
107
          {playersByPosition && (
108
            <div className={`tabs-panel`} id="team-lineup">
109
              <main className={`team-detail__lineup`}>
110
                {/* {nodeTeam?.relationships.field_staff && (
111
                  <TeamSection title="Stafleden" lineup={node.relationships.field_staff} />
112
                )} */}
113
                {/* {playersByPosition[`k`] && <TeamSection title="Doelmannen" lineup={playersByPosition[`k`]} />} */}
114
                {/* {playersByPosition[`d`] && <TeamSection title="Verdedigers" lineup={playersByPosition[`d`]} />} */}
115
                {/* {playersByPosition[`m`] && <TeamSection title="Middenvelder" lineup={playersByPosition[`m`]} />} */}
116
                {/* {playersByPosition[`a`] && <TeamSection title="Aanvallers" lineup={playersByPosition[`a`]} />} */}
117
              </main>
118
            </div>
119
          )}
120
          {hasDivision && (
121
            <>
122
              <div className={`tabs-panel`} id="team-matches">
123
                {/* {nodeTeam?.field_vv_id && <MatchTeasers teamId={nodeTeam?.field_vv_id} />} */}
124
                {/* {nodeTeam?.field_vv_id && <Matches teamId={nodeTeam?.field_vv_id} />} */}
125
              </div>
126
              <div className={`tabs-panel`} id="team-ranking">
127
                {/* {nodeTeam?.field_vv_id && <Ranking teamId={nodeTeam?.field_vv_id} />} */}
128
              </div>
129
            </>
130
          )}
131
        </div>
132
      </article>
133
    </Layout>
134
  )
135
}
136
137
export default Team
138
139
export const Head = ({ data: { nodeTeam } }: TeamQuery) => {
140
  const pathUrl = nodeTeam?.path?.alias || ``
141
  const heroImage =
142
    nodeTeam?.relationships?.image_og?.relationships?.field_media_image?.localFile?.childImageSharp?.gatsbyImageData
143
144
  const ogImage = heroImage && {
145
    src: getSrc(heroImage) || ``,
146
    width: heroImage.width,
147
    height: heroImage.height,
148
  }
149
  return <Seo title={`${nodeTeam?.title} / ${nodeTeam?.field_division_full}`} path={pathUrl} image={ogImage} />
150
}
151
152
export const query = graphql`
153
  query TeamQuery($slug: String!) {
154
    nodeTeam(path: { alias: { eq: $slug } }) {
155
      path {
156
        alias
157
      }
158
      title
159
      field_contact_info {
160
        processed
161
      }
162
      field_fb_id
163
      field_fb_id_2
164
      field_vv_id
165
      field_division_full
166
      field_tagline
167
      relationships {
168
        field_staff {
169
          path {
170
            alias
171
          }
172
          field_position_short
173
          field_lastname
174
          field_firstname
175
          relationships {
176
            field_image {
177
              localFile {
178
                ...KCVVFluidPlayerTeaser
179
              }
180
            }
181
          }
182
        }
183
        field_players {
184
          path {
185
            alias
186
          }
187
          field_shirtnumber
188
          field_lastname
189
          field_firstname
190
          field_position
191
          relationships {
192
            field_image {
193
              localFile {
194
                ...KCVVFluidPlayerTeaser
195
              }
196
            }
197
          }
198
        }
199
        field_media_article_image {
200
          ...FullImage
201
          field_media_image {
202
            alt
203
          }
204
        }
205
        image_og: field_media_article_image {
206
          ...ArticleImage
207
        }
208
        node__article {
209
          title
210
          timestamp: created(formatString: "x")
211
          path {
212
            alias
213
          }
214
          relationships {
215
            field_media_article_image {
216
              ...ArticleImage
217
            }
218
          }
219
        }
220
      }
221
    }
222
  }
223
`
224