Passed
Push — feature/player-stats ( 32be2a...ea44e6 )
by Kevin Van
04:11
created

src/templates/team.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 338
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 232
mnd 1
bc 1
fnc 0
dl 0
loc 338
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { graphql } from "gatsby"
2
import React from "react"
3
import SEO from "../components/seo"
4
import Layout from "../layouts/index"
5
import Img from "gatsby-image"
6
import { TeamSection } from "../components/team--section"
7
import "./team.scss"
8
import Ranking from "../components/ranking"
9
import TeamCalendarMatches from "../components/team-calendar-matches"
10
import TeamCalendarMetaMatches from "../components/team-calendar-meta-matches"
11
import TeamStats from "../components/team--stats"
12
13
// Generic helper function to group an array by a property.
14
const groupBy = (key) => (array) =>
15
  array.reduce((objectsByKeyValue, obj) => {
16
    const value = obj[key]
17
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj)
18
    return objectsByKeyValue
19
  }, {})
20
21
// Specific implementation of our groupBy function, to group by a property "field_position".
22
const groupByPosition = groupBy("field_position")
23
24
const TeamTemplate = ({ data }) => {
25
  const node = data.nodeTeam
26
27
  // If we have players, group them by their position.
28
  const playersByPosition =
29
    node.relationships.field_players.length > 0 &&
30
    groupByPosition(node.relationships.field_players)
31
32
  const picture = node.relationships.field_media_article_image
33
  // Create a fluid/responsive team image instance.
34
  const teamPicture = picture && (
35
    <Img
36
      fluid={
37
        picture.relationships.field_media_image.localFile.childImageSharp.fluid
38
      }
39
      alt={picture.field_media_image.alt}
40
      className={"team-detail__team-picture"}
41
    />
42
  )
43
44
  // Helper variable so we don't have to do the check over and over again.
45
  const hasDivision = node.field_fb_id || node.field_fb_id_2
46
47
  const pathUrl = node.path.alias
48
  const ogImage = picture && {
49
    src:
50
      node.relationships.field_media_article_image.relationships
51
        .field_media_image.localFile.childImageSharp.resize.src,
52
    width:
53
      node.relationships.field_media_article_image.relationships
54
        .field_media_image.localFile.childImageSharp.resize.width,
55
    height:
56
      node.relationships.field_media_article_image.relationships
57
        .field_media_image.localFile.childImageSharp.resize.height,
58
  }
59
60
  return (
61
    <Layout>
62
      <SEO
63
        lang="nl-BE"
64
        title={node.title + " / " + node.field_division_full}
65
        description={"KCVV Elewijt " + node.field_division_full}
66
        path={pathUrl}
67
        image={ogImage || null}
68
      />
69
70
      <article className={"team-detail"}>
71
        <header className={"team-detail__header"}>
72
          <h1 className={"team-detail__name"}>
73
            {/* > GEWESTELIJKE U13 K */}
74
            <span className={"team-detail__name-division"}>
75
              {node.field_division_full}
76
            </span>
77
            {/* > The A-team */}
78
            <span className={"team-detail__name-tagline"}>
79
              {node.field_tagline}
80
            </span>
81
          </h1>
82
83
          <div className={"bg-green-mask"}>
84
            <div className={"bg-white-end"} />
85
          </div>
86
87
          {/* > 2A, 2G9K... */}
88
          {/* FB ID if only one of either is set, FB ID 2 if it has a value (will only be published around new year's, so it's always the most relevant as soon as it's known) */}
89
          {hasDivision && (
90
            <div className={"team-detail__division-number"} aria-hidden="true">
91
              {node.field_fb_id_2 ? node.field_fb_id_2 : node.field_fb_id}
92
            </div>
93
          )}
94
        </header>
95
96
        <div className={"team-break"}></div>
97
98
        {/* Only show tab links if there are either players assigned to the team, or the team has an (active) division, so we can show rankings/matches */}
99
        {(playersByPosition || hasDivision) && (
100
          <section className={"team-sub_navigation"}>
101
            {/* Foundation tabs structure */}
102
            <ul
103
              className={"tabs team-sub_navigation__tabs "}
104
              data-tabs
105
              data-deep-link="true"
106
              data-update-history="true"
107
              data-deep-link-smudge="true"
108
              data-deep-link-smudge-delay="500"
109
              id="team-subnavigation_tabs"
110
            >
111
              <li className="tabs-title is-active">
112
                <a href="#team-info">Info</a>
113
              </li>
114
              {/* Youth teams don't have lineups, so we don't show the tab link. */}
115
              {playersByPosition && (
116
                <li className="tabs-title">
117
                  <a href="#team-lineup">Lineup</a>
118
                </li>
119
              )}
120
              {hasDivision && (
121
                <>
122
                  <li className={"tabs-title"}>
123
                    <a data-tabs-target="team-matches" href="#team-matches">
124
                      Wedstrijden
125
                    </a>
126
                  </li>
127
                  <li className={"tabs-title"}>
128
                    <a data-tabs-target="team-ranking" href="#team-ranking">
129
                      Stand
130
                    </a>
131
                  </li>
132
                </>
133
              )}
134
            </ul>
135
          </section>
136
        )}
137
138
        {/* Foundation content of the tabs. */}
139
        <div
140
          className={"tabs-content"}
141
          data-tabs-content="team-subnavigation_tabs"
142
        >
143
          <div className={"tabs-panel is-active"} id="team-info">
144
            {teamPicture || ""}
145
            {node.field_contact_info && (
146
              <div
147
                className={"team-detail__team-info"}
148
                dangerouslySetInnerHTML={{
149
                  __html: node.field_contact_info.processed,
150
                }}
151
              />
152
            )}
153
            <TeamStats team={node} />
154
          </div>
155
          {/* If our page displays staff only (e.g. the "board" page), we change the title. */}
156
          {node.relationships.field_staff && !playersByPosition && (
157
            <main
158
              className={"team-detail__lineup team-detail__lineup--staff-only"}
159
            >
160
              <TeamSection
161
                title="Stafleden"
162
                lineup={node.relationships.field_staff}
163
              />
164
            </main>
165
          )}
166
          {playersByPosition && (
167
            <div className={"tabs-panel"} id="team-lineup">
168
              <main className={"team-detail__lineup"}>
169
                {node.relationships.field_staff && (
170
                  <TeamSection
171
                    title="Stafleden"
172
                    lineup={node.relationships.field_staff}
173
                  />
174
                )}
175
                {playersByPosition["k"] && (
176
                  <TeamSection
177
                    title="Doelmannen"
178
                    lineup={playersByPosition["k"]}
179
                  />
180
                )}
181
                {playersByPosition["d"] && (
182
                  <TeamSection
183
                    title="Verdedigers"
184
                    lineup={playersByPosition["d"]}
185
                  />
186
                )}
187
                {playersByPosition["m"] && (
188
                  <TeamSection
189
                    title="Middenvelder"
190
                    lineup={playersByPosition["m"]}
191
                  />
192
                )}
193
                {playersByPosition["a"] && (
194
                  <TeamSection
195
                    title="Aanvallers"
196
                    lineup={playersByPosition["a"]}
197
                  />
198
                )}
199
              </main>
200
            </div>
201
          )}
202
          {hasDivision && (
203
            <>
204
              <div className={"tabs-panel"} id="team-matches">
205
                {node.field_fb_id_2 && (
206
                  <div className={"team-ranking__wrapper"}>
207
                    {node.field_fb_id && <h2>Wedstrijden na nieuwjaar</h2>}
208
209
                    {/* Metamatches is the big banner on top with the previous and next match highlighted. */}
210
                    {!node.field_fb_id && (
211
                      <TeamCalendarMetaMatches
212
                        season="2021"
213
                        region="bra"
214
                        division={node.field_fb_id_2}
215
                        regnumber="00055"
216
                      />
217
                    )}
218
219
                    <TeamCalendarMatches
220
                      season="2021"
221
                      region="bra"
222
                      division={node.field_fb_id_2}
223
                    />
224
                  </div>
225
                )}
226
                {node.field_fb_id && (
227
                  <div className={"team-ranking__wrapper"}>
228
                    {node.field_fb_id_2 && <h2>Wedstrijden voor nieuwjaar</h2>}
229
230
                    {/* Metamatches is the big banner on top with the previous and next match highlighted. */}
231
                    {!node.field_fb_id_2 && (
232
                      <TeamCalendarMetaMatches
233
                        season="2021"
234
                        region="bra"
235
                        division={node.field_fb_id}
236
                        regnumber="00055"
237
                      />
238
                    )}
239
240
                    <TeamCalendarMatches
241
                      season="2021"
242
                      region="bra"
243
                      division={node.field_fb_id}
244
                    />
245
                  </div>
246
                )}
247
              </div>
248
              <div className={"tabs-panel"} id="team-ranking">
249
                {node.field_fb_id_2 && (
250
                  <div className={"team-ranking__wrapper"}>
251
                    {node.field_fb_id && <h2>Ranking na nieuwjaar</h2>}
252
                    <Ranking
253
                      season="2021"
254
                      region="bra"
255
                      division={node.field_fb_id_2}
256
                      highlight="KCVV.Elewijt A"
257
                    />
258
                  </div>
259
                )}
260
                {node.field_fb_id && (
261
                  <div className={"team-ranking__wrapper"}>
262
                    {node.field_fb_id_2 && <h2>Ranking voor nieuwjaar</h2>}
263
                    <Ranking
264
                      season="2021"
265
                      region="bra"
266
                      division={node.field_fb_id}
267
                      highlight="KCVV.Elewijt A"
268
                    />
269
                  </div>
270
                )}
271
              </div>
272
            </>
273
          )}
274
        </div>
275
      </article>
276
    </Layout>
277
  )
278
}
279
280
export const query = graphql`
281
  query($slug: String!) {
282
    nodeTeam(path: { alias: { eq: $slug } }) {
283
      path {
284
        alias
285
      }
286
      title
287
      field_contact_info {
288
        processed
289
      }
290
      field_fb_id
291
      field_fb_id_2
292
      field_division_full
293
      field_tagline
294
      relationships {
295
        field_staff {
296
          path {
297
            alias
298
          }
299
          field_position_short
300
          field_lastname
301
          field_firstname
302
          relationships {
303
            field_image {
304
              localFile {
305
                ...KCVVFluidPlayerTeaser
306
              }
307
            }
308
          }
309
        }
310
        field_players {
311
          path {
312
            alias
313
          }
314
          field_shirtnumber
315
          field_lastname
316
          field_firstname
317
          field_position
318
          relationships {
319
            field_image {
320
              localFile {
321
                ...KCVVFluidPlayerTeaser
322
              }
323
            }
324
          }
325
        }
326
        field_media_article_image {
327
          ...ArticleImage
328
          field_media_image {
329
            alt
330
          }
331
        }
332
      }
333
    }
334
  }
335
`
336
337
export default TeamTemplate;
338