Passed
Pull Request — develop (#758)
by Kevin Van
08:39 queued 04:54
created

src/templates/Team.tsx   B

Complexity

Total Complexity 52
Complexity/F 0

Size

Lines of Code 245
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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