Passed
Push — feature/typescript ( 497e7e )
by Kevin Van
06:13
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
12
// Generic helper function to group an array by a property.
13
const groupBy = (key) => (array) =>
14
  array.reduce((objectsByKeyValue, obj) => {
15
    const value = obj[key]
16
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj)
17
    return objectsByKeyValue
18
  }, {})
19
20
// Specific implementation of our groupBy function, to group by a property "field_position".
21
const groupByPosition = groupBy("field_position")
22
23
const TeamTemplate = ({ data }) => {
24
  const node = data.nodeTeam
25
26
  // If we have players, group them by their position.
27
  const playersByPosition =
28
    node.relationships.field_players.length > 0 &&
29
    groupByPosition(node.relationships.field_players)
30
31
  const picture = node.relationships.field_media_article_image
32
  // Create a fluid/responsive team image instance.
33
  const teamPicture = picture && (
34
    <Img
35
      fluid={
36
        picture.relationships.field_media_image.localFile.childImageSharp.fluid
37
      }
38
      alt={picture.field_media_image.alt}
39
      className={"team-detail__team-picture"}
40
    />
41
  )
42
43
  // Helper variable so we don't have to do the check over and over again.
44
  const hasDivision = node.field_fb_id || node.field_fb_id_2
45
46
  console.log(hasDivision);
47
48
  const pathUrl = node.path.alias
49
  const ogImage = picture && {
50
    src:
51
      node.relationships.field_media_article_image.relationships
52
        .field_media_image.localFile.childImageSharp.resize.src,
53
    width:
54
      node.relationships.field_media_article_image.relationships
55
        .field_media_image.localFile.childImageSharp.resize.width,
56
    height:
57
      node.relationships.field_media_article_image.relationships
58
        .field_media_image.localFile.childImageSharp.resize.height,
59
  }
60
61
  return (
62
    <Layout>
63
      <SEO
64
        lang="nl-BE"
65
        title={node.title + " / " + node.field_division_full}
66
        description={"KCVV Elewijt " + node.field_division_full}
67
        path={pathUrl}
68
        image={ogImage || null}
69
      />
70
71
      <article className={"team-detail"}>
72
        <header className={"team-detail__header"}>
73
          <h1 className={"team-detail__name"}>
74
            {/* > GEWESTELIJKE U13 K */}
75
            <span className={"team-detail__name-division"}>
76
              {node.field_division_full}
77
            </span>
78
            {/* > The A-team */}
79
            <span className={"team-detail__name-tagline"}>
80
              {node.field_tagline}
81
            </span>
82
          </h1>
83
84
          <div className={"bg-green-mask"}>
85
            <div className={"bg-white-end"} />
86
          </div>
87
88
          {/* > 2A, 2G9K... */}
89
          {/* 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) */}
90
          {hasDivision && (
91
            <div className={"team-detail__division-number"} aria-hidden="true">
92
              {node.field_fb_id_2 ? node.field_fb_id_2 : node.field_fb_id}
93
            </div>
94
          )}
95
        </header>
96
97
        <div className={"team-break"}></div>
98
99
        {/* 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 */}
100
        {(playersByPosition || hasDivision) && (
101
          <section className={"team-sub_navigation"}>
102
            {/* Foundation tabs structure */}
103
            <ul
104
              className={"tabs team-sub_navigation__tabs "}
105
              data-tabs
106
              data-deep-link="true"
107
              data-update-history="true"
108
              data-deep-link-smudge="true"
109
              data-deep-link-smudge-delay="500"
110
              id="team-subnavigation_tabs"
111
            >
112
              <li className="tabs-title is-active">
113
                <a href="#team-info">Info</a>
114
              </li>
115
              {/* Youth teams don't have lineups, so we don't show the tab link. */}
116
              {playersByPosition && (
117
                <li className="tabs-title">
118
                  <a href="#team-lineup">Lineup</a>
119
                </li>
120
              )}
121
              {hasDivision && (
122
                <>
123
                  <li className={"tabs-title"}>
124
                    <a data-tabs-target="team-matches" href="#team-matches">
125
                      Wedstrijden
126
                    </a>
127
                  </li>
128
                  <li className={"tabs-title"}>
129
                    <a data-tabs-target="team-ranking" href="#team-ranking">
130
                      Stand
131
                    </a>
132
                  </li>
133
                </>
134
              )}
135
            </ul>
136
          </section>
137
        )}
138
139
        {/* Foundation content of the tabs. */}
140
        <div
141
          className={"tabs-content"}
142
          data-tabs-content="team-subnavigation_tabs"
143
        >
144
          <div className={"tabs-panel is-active"} id="team-info">
145
            {teamPicture || ""}
146
            {node.field_contact_info && (
147
              <div
148
                className={"team-detail__team-info"}
149
                dangerouslySetInnerHTML={{
150
                  __html: node.field_contact_info.processed,
151
                }}
152
              />
153
            )}
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