src/templates/Team.tsx   B
last analyzed

Complexity

Total Complexity 50
Complexity/F 0

Size

Lines of Code 244
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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