Passed
Push — master ( 7aa1e4...a37cc0 )
by Kevin Van
05:30
created

src/templates/Team.tsx   B

Complexity

Total Complexity 50
Complexity/F 0

Size

Lines of Code 243
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 50
eloc 154
mnd 50
bc 50
fnc 0
dl 0
loc 243
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
              </main>
127
            </div>
128
          )}
129
          {hasDivision && (
130
            <>
131
              <div className={`tabs-panel`} id="team-matches">
132
                {nodeTeam?.field_vv_id && <MatchTeasers teamId={+nodeTeam?.field_vv_id} />}
133
                {nodeTeam?.field_vv_id && <Matches teamId={+nodeTeam?.field_vv_id} />}
134
              </div>
135
              <div className={`tabs-panel`} id="team-ranking">
136
                {nodeTeam?.field_vv_id && <Ranking teamId={+nodeTeam?.field_vv_id} />}
137
              </div>
138
            </>
139
          )}
140
        </div>
141
      </article>
142
    </Layout>
143
  )
144
}
145
146
export default Team
147
148
export const Head = ({ data: { nodeTeam } }: TeamQuery) => {
149
  const pathUrl = nodeTeam?.path?.alias || ``
150
  const heroImage =
151
    nodeTeam?.relationships?.image_og?.relationships?.field_media_image?.localFile?.childImageSharp?.gatsbyImageData
152
153
  const ogImage = heroImage && {
154
    src: getSrc(heroImage) || ``,
155
    width: heroImage.width,
156
    height: heroImage.height,
157
  }
158
  return <Seo title={`${nodeTeam?.title} / ${nodeTeam?.field_division_full}`} path={pathUrl} image={ogImage} />
159
}
160
161
const groupBy = <T, K extends keyof T>(array: T[], groupOn: K | ((i: T) => string)): Record<string, T[]> => {
162
  const groupFn = typeof groupOn === `function` ? groupOn : (o: T) => o[groupOn]
163
164
  return Object.fromEntries(
165
    array.reduce((acc, obj) => {
166
      const groupKey = groupFn(obj)
167
      return acc.set(groupKey, [...(acc.get(groupKey) || []), obj])
168
    }, new Map())
169
  ) as Record<string, T[]>
170
}
171
172
export const query = graphql`
173
  query TeamQuery($slug: String!) {
174
    nodeTeam(path: { alias: { eq: $slug } }) {
175
      path {
176
        alias
177
      }
178
      title
179
      field_contact_info {
180
        processed
181
      }
182
      field_fb_id
183
      field_vv_id
184
      field_division_full
185
      field_tagline
186
      relationships {
187
        field_staff {
188
          path {
189
            alias
190
          }
191
          field_position_short
192
          field_lastname
193
          field_firstname
194
          relationships {
195
            field_image {
196
              localFile {
197
                ...KCVVFluidPlayerTeaser
198
              }
199
            }
200
          }
201
        }
202
        field_players {
203
          path {
204
            alias
205
          }
206
          field_shirtnumber
207
          field_lastname
208
          field_firstname
209
          field_position
210
          relationships {
211
            field_image {
212
              localFile {
213
                ...KCVVFluidPlayerTeaser
214
              }
215
            }
216
          }
217
        }
218
        field_media_article_image {
219
          ...MaxWidth
220
          field_media_image {
221
            alt
222
          }
223
        }
224
        image_og: field_media_article_image {
225
          ...ArticleImage
226
        }
227
        node__article {
228
          title
229
          timestamp: created(formatString: "x")
230
          path {
231
            alias
232
          }
233
          relationships {
234
            field_media_article_image {
235
              ...ArticleImage
236
            }
237
          }
238
        }
239
      }
240
    }
241
  }
242
`
243