Passed
Pull Request — develop (#758)
by Kevin Van
04:17
created

src/templates/Team.tsx   B

Complexity

Total Complexity 52
Complexity/F 0

Size

Lines of Code 249
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 52
eloc 160
mnd 52
bc 52
fnc 0
dl 0
loc 249
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 7.44

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
  console.log(nodeTeam?.relationships?.node__article)
32
33
  const allPlayers = [...(nodeTeam?.relationships?.field_players || [])]
34
35
  // Specific implementation of our groupBy function, to group by a property "field_position".
36
  const groupByPosition = groupBy(allPlayers, (v) => v?.field_position || ``)
37
38
  return (
39
    <Layout>
40
      <header className="page_header__wrapper page_header__wrapper--inset-large">
41
        <div className="page_header">
42
          <h1 className="team__header__name">
43
            {/* > GEWESTELIJKE U13 K */}
44
            <span className="team__header_name__division">
45
              <ReactFitText compressor={1.5}>{nodeTeam?.field_division_full}</ReactFitText>
46
            </span>
47
            {/* > The A-team */}
48
            <span className="team__header_name__division__tagline">{nodeTeam?.field_tagline}</span>
49
          </h1>
50
          {hasDivision && (
51
            <div className="team__header_name__division__number" aria-hidden="true">
52
              {nodeTeam?.field_fb_id}
53
            </div>
54
          )}
55
        </div>
56
      </header>
57
      {(allPlayers.length > 0 || hasDivision) && (
58
        <nav className="team__sub_navigation">
59
          {/* Foundation tabs structure */}
60
          <ul
61
            className="tabs team__sub_navigation__tabs"
62
            data-tabs
63
            data-deep-link="true"
64
            data-update-history="true"
65
            data-deep-link-smudge="true"
66
            data-deep-link-smudge-delay="500"
67
            id="team-subnavigation_tabs"
68
          >
69
            <li className="tabs-title is-active">
70
              <a href="#team-info" className="rich-link-center">
71
                Info
72
              </a>
73
            </li>
74
            {/* Youth teams don't have lineups, so we don't show the tab link. */}
75
            {allPlayers.length > 0 && (
76
              <li className="tabs-title">
77
                <a href="#team-lineup" className="rich-link-center">
78
                  Lineup
79
                </a>
80
              </li>
81
            )}
82
            {hasDivision && (
83
              <>
84
                <li className={`tabs-title`}>
85
                  <a data-tabs-target="team-matches" href="#team-matches" className="rich-link-center">
86
                    Wedstrijden
87
                  </a>
88
                </li>
89
                <li className={`tabs-title`}>
90
                  <a data-tabs-target="team-ranking" href="#team-ranking" className="rich-link-center">
91
                    Stand
92
                  </a>
93
                </li>
94
              </>
95
            )}
96
          </ul>
97
        </nav>
98
      )}
99
      <article className="page__wrapper team__wrapper">
100
        <div className="tabs-content" data-tabs-content="team-subnavigation_tabs">
101
          <div className="tabs-panel is-active" id="team-info">
102
            <div className="team_info__hero_image">{teamPicture || ``}</div>
103
            {nodeTeam?.field_contact_info && (
104
              <div
105
                className={`team_info__contact page__wrapper page__wrapper--limited`}
106
                dangerouslySetInnerHTML={{
107
                  __html: nodeTeam?.field_contact_info.processed || ``,
108
                }}
109
              />
110
            )}
111
            {nodeTeam?.field_vv_id && <TeamStats teamId={+nodeTeam?.field_vv_id} />}
112
            {nodeTeam?.relationships?.field_staff && allPlayers.length <= 0 && (
113
              <main className={`team-detail__lineup team-detail__lineup--staff-only`}>
114
                <Lineup lineup={nodeTeam?.relationships?.field_staff} />
115
              </main>
116
            )}
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
          {articles && <RelatedNews items={[...articles]} limit={6} />}
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 && (
135
                  <>
136
                    <AltTitle title="KALENDER" variant="white" />
137
                    <Matches teamId={+nodeTeam?.field_vv_id} />
138
                  </>
139
                )}
140
              </div>
141
              <div className={`tabs-panel`} id="team-ranking">
142
                {nodeTeam?.field_vv_id && <Ranking teamId={+nodeTeam?.field_vv_id} />}
143
              </div>
144
            </>
145
          )}
146
        </div>
147
      </article>
148
    </Layout>
149
  )
150
}
151
152
export default Team
153
154
export const Head = ({ data: { nodeTeam } }: TeamQuery) => {
155
  const pathUrl = nodeTeam?.path?.alias || ``
156
  const heroImage =
157
    nodeTeam?.relationships?.image_og?.relationships?.field_media_image?.localFile?.childImageSharp?.gatsbyImageData
158
159
  const ogImage = heroImage && {
160
    src: getSrc(heroImage) || ``,
161
    width: heroImage.width,
162
    height: heroImage.height,
163
  }
164
  return <Seo title={`${nodeTeam?.title} / ${nodeTeam?.field_division_full}`} path={pathUrl} image={ogImage} />
165
}
166
167
const groupBy = <T, K extends keyof T>(array: T[], groupOn: K | ((i: T) => string)): Record<string, T[]> => {
168
  const groupFn = typeof groupOn === `function` ? groupOn : (o: T) => o[groupOn]
169
170
  return Object.fromEntries(
171
    array.reduce((acc, obj) => {
172
      const groupKey = groupFn(obj)
173
      return acc.set(groupKey, [...(acc.get(groupKey) || []), obj])
174
    }, new Map())
175
  ) as Record<string, T[]>
176
}
177
178
export const query = graphql`
179
  query TeamQuery($slug: String!) {
180
    nodeTeam(path: { alias: { eq: $slug } }) {
181
      path {
182
        alias
183
      }
184
      title
185
      field_contact_info {
186
        processed
187
      }
188
      field_fb_id
189
      field_vv_id
190
      field_division_full
191
      field_tagline
192
      relationships {
193
        field_staff {
194
          path {
195
            alias
196
          }
197
          field_position_short
198
          field_lastname
199
          field_firstname
200
          relationships {
201
            field_image {
202
              localFile {
203
                ...KCVVFluidPlayerTeaser
204
              }
205
            }
206
          }
207
        }
208
        field_players {
209
          path {
210
            alias
211
          }
212
          field_shirtnumber
213
          field_lastname
214
          field_firstname
215
          field_position
216
          relationships {
217
            field_image {
218
              localFile {
219
                ...KCVVFluidPlayerTeaser
220
              }
221
            }
222
          }
223
        }
224
        field_media_article_image {
225
          ...FullImage
226
          field_media_image {
227
            alt
228
          }
229
        }
230
        image_og: field_media_article_image {
231
          ...ArticleImage
232
        }
233
        node__article {
234
          title
235
          timestamp: created(formatString: "x")
236
          path {
237
            alias
238
          }
239
          relationships {
240
            field_media_article_image {
241
              ...ArticleImage
242
            }
243
          }
244
        }
245
      }
246
    }
247
  }
248
`
249