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

srcBU/templates/team.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 253
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 161
mnd 1
bc 1
fnc 0
dl 0
loc 253
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { graphql } from "gatsby"
2
import { GatsbyImage, getSrc } from "gatsby-plugin-image"
3
import React, { Fragment } from "react"
4
5
import { MatchTeasers } from "../components/MatchTeaser"
6
import Matches from "../components/Matches"
7
import Ranking from "../components/Ranking"
8
import RelatedNews from "../components/RelatedNews"
9
import TeamStats from "../components/TeamStats"
10
import SEO from "../components/seo"
11
import { TeamSection } from "../components/team--section"
12
import Layout from "../layouts/index"
13
import "./team.scss"
14
15
// Generic helper function to group an array by a property.
16
const groupBy = (key) => (array) =>
17
  array.reduce((objectsByKeyValue, obj) => {
18
    const value = obj[key]
19
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj)
20
    return objectsByKeyValue
21
  }, {})
22
23
// Specific implementation of our groupBy function, to group by a property "field_position".
24
const groupByPosition = groupBy(`field_position`)
25
26
const TeamTemplate = ({ data }) => {
27
  const node = data.nodeTeam
28
29
  // If we have players, group them by their position.
30
  const playersByPosition =
31
    node.relationships.field_players.length > 0 && groupByPosition(node.relationships.field_players)
32
33
  const picture = node.relationships.field_media_article_image
34
35
  // Create a fluid/responsive team image instance.
36
  const teamPicture = picture && (
37
    <GatsbyImage
38
      image={picture.relationships.field_media_image.localFile.childImageSharp.gatsbyImageData}
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 || node.field_vv_id
46
47
  const pathUrl = node.path.alias
48
49
  const ogImage = picture && {
50
    src: getSrc(picture.relationships.field_media_image.localFile.childImageSharp.gatsbyImageData),
51
    width: picture.relationships.field_media_image.localFile.childImageSharp.gatsbyImageData.width,
52
    height: picture.relationships.field_media_image.localFile.childImageSharp.gatsbyImageData.height,
53
  }
54
55
  const articles = node.relationships.node__article || []
56
57
  return (
58
    <Layout>
59
      <SEO
60
        lang="nl-BE"
61
        title={node.title + ` / ` + node.field_division_full}
62
        description={`KCVV Elewijt ` + node.field_division_full}
63
        path={pathUrl}
64
        image={ogImage || null}
65
      />
66
67
      <article className={`team-detail`}>
68
        <header className={`team-detail__header`}>
69
          <h1 className={`team-detail__name`}>
70
            {/* > GEWESTELIJKE U13 K */}
71
            <span className={`team-detail__name-division`}>{node.field_division_full}</span>
72
            {/* > The A-team */}
73
            <span className={`team-detail__name-tagline`}>{node.field_tagline}</span>
74
          </h1>
75
76
          <div className={`bg-green-mask`}>
77
            <div className={`bg-white-end`} />
78
          </div>
79
80
          {/* > 2A, 2G9K... */}
81
          {/* 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) */}
82
          {hasDivision && (
83
            <div className={`team-detail__division-number`} aria-hidden="true">
84
              {node.field_fb_id_2 ? node.field_fb_id_2 : node.field_fb_id}
85
            </div>
86
          )}
87
        </header>
88
89
        <div className={`team-break`}></div>
90
91
        {/* 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 */}
92
        {(playersByPosition || hasDivision) && (
93
          <section className={`team-sub_navigation`}>
94
            {/* Foundation tabs structure */}
95
            <ul
96
              className={`tabs team-sub_navigation__tabs`}
97
              data-tabs
98
              data-deep-link="true"
99
              data-update-history="true"
100
              data-deep-link-smudge="true"
101
              data-deep-link-smudge-delay="500"
102
              id="team-subnavigation_tabs"
103
            >
104
              <li className="tabs-title is-active">
105
                <a href="#team-info">Info</a>
106
              </li>
107
              {/* Youth teams don't have lineups, so we don't show the tab link. */}
108
              {playersByPosition && (
109
                <li className="tabs-title">
110
                  <a href="#team-lineup">Lineup</a>
111
                </li>
112
              )}
113
              {hasDivision && (
114
                <Fragment>
115
                  <li className={`tabs-title`}>
116
                    <a data-tabs-target="team-matches" href="#team-matches">
117
                      Wedstrijden
118
                    </a>
119
                  </li>
120
                  <li className={`tabs-title`}>
121
                    <a data-tabs-target="team-ranking" href="#team-ranking">
122
                      Stand
123
                    </a>
124
                  </li>
125
                </Fragment>
126
              )}
127
            </ul>
128
          </section>
129
        )}
130
131
        <div className={`tabs-content`} data-tabs-content="team-subnavigation_tabs">
132
          <div className={`tabs-panel is-active`} id="team-info">
133
            {teamPicture || ``}
134
            {node.field_contact_info && (
135
              <div
136
                className={`team-detail__team-info`}
137
                dangerouslySetInnerHTML={{
138
                  __html: node.field_contact_info.processed,
139
                }}
140
              />
141
            )}
142
            {node.field_vv_id && <TeamStats teamId={node.field_vv_id} />}
143
            {/* If our page displays staff only (e.g. the "board" page), we change the title. */}
144
            {node.relationships.field_staff && !playersByPosition && (
145
              <main className={`team-detail__lineup team-detail__lineup--staff-only`}>
146
                <TeamSection title="Stafleden" lineup={node.relationships.field_staff} />
147
              </main>
148
            )}
149
            {articles && <RelatedNews items={articles} limit={6} />}
150
          </div>
151
152
          {playersByPosition && (
153
            <div className={`tabs-panel`} id="team-lineup">
154
              <main className={`team-detail__lineup`}>
155
                {node.relationships.field_staff && (
156
                  <TeamSection title="Stafleden" lineup={node.relationships.field_staff} />
157
                )}
158
                {playersByPosition[`k`] && <TeamSection title="Doelmannen" lineup={playersByPosition[`k`]} />}
159
                {playersByPosition[`d`] && <TeamSection title="Verdedigers" lineup={playersByPosition[`d`]} />}
160
                {playersByPosition[`m`] && <TeamSection title="Middenvelder" lineup={playersByPosition[`m`]} />}
161
                {playersByPosition[`a`] && <TeamSection title="Aanvallers" lineup={playersByPosition[`a`]} />}
162
              </main>
163
            </div>
164
          )}
165
          {hasDivision && (
166
            <Fragment>
167
              <div className={`tabs-panel`} id="team-matches">
168
                {node.field_vv_id && <MatchTeasers teamId={node.field_vv_id} />}
169
                {node.field_vv_id && <Matches teamId={node.field_vv_id} />}
170
              </div>
171
              <div className={`tabs-panel`} id="team-ranking">
172
                {node.field_vv_id && <Ranking teamId={node.field_vv_id} />}
173
              </div>
174
            </Fragment>
175
          )}
176
        </div>
177
      </article>
178
    </Layout>
179
  )
180
}
181
182
export const query = graphql`
183
  query ($slug: String!) {
184
    nodeTeam(path: { alias: { eq: $slug } }) {
185
      path {
186
        alias
187
      }
188
      title
189
      field_contact_info {
190
        processed
191
      }
192
      field_fb_id
193
      field_fb_id_2
194
      field_vv_id
195
      field_division_full
196
      field_tagline
197
      relationships {
198
        field_staff {
199
          path {
200
            alias
201
          }
202
          field_position_short
203
          field_lastname
204
          field_firstname
205
          relationships {
206
            field_image {
207
              localFile {
208
                ...KCVVFluidPlayerTeaser
209
              }
210
            }
211
          }
212
        }
213
        field_players {
214
          path {
215
            alias
216
          }
217
          field_shirtnumber
218
          field_lastname
219
          field_firstname
220
          field_position
221
          relationships {
222
            field_image {
223
              localFile {
224
                ...KCVVFluidPlayerTeaser
225
              }
226
            }
227
          }
228
        }
229
        field_media_article_image {
230
          ...FullImage
231
          field_media_image {
232
            alt
233
          }
234
        }
235
        node__article {
236
          title
237
          timestamp: created(formatString: "x")
238
          path {
239
            alias
240
          }
241
          relationships {
242
            field_media_article_image {
243
              ...ArticleImage
244
            }
245
          }
246
        }
247
      }
248
    }
249
  }
250
`
251
252
export default TeamTemplate
253