Passed
Push — master ( 7aa1e4...a37cc0 )
by Kevin Van
05:30
created

srcBU/pages/share.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 140
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 110
mnd 3
bc 3
fnc 0
dl 0
loc 140
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { graphql, useStaticQuery } from "gatsby"
2
import React, { FunctionComponent, useState } from "react"
3
import Select from "react-select"
4
5
import Spinner from "../components/Spinner"
6
import SEO from "../components/seo"
7
import Layout from "../layouts/index"
8
import { PlayerListProps } from "../types/pages.types"
9
10
const ShareForm = () => {
11
  const templateUrl = `https://footbalisto.be/share/instagram/`
12
13
  const [playerSelected, setPlayerSelected] = useState<string>(``)
14
  const [score, setScore] = useState<string>(``)
15
  const [match, setMatch] = useState<string>(``)
16
  const [imageUrl, setImageUrl] = useState<string>(``)
17
  const [imageLoading, setImageLoading] = useState<boolean>(false)
18
19
  const { playerEdges }: PlayerListProps = useStaticQuery(graphql`
20
    query {
21
      playerEdges: allNodePlayer(filter: { field_vv_id: { ne: null } }) {
22
        edges {
23
          node {
24
            path {
25
              alias
26
            }
27
            field_firstname
28
            field_lastname
29
          }
30
        }
31
      }
32
    }
33
  `)
34
35
  const players = playerEdges.edges.map(({ node }) => ({
36
    value: node.path.alias,
37
    label: `${node.field_firstname} ${node.field_lastname}`,
38
  }))
39
40
  const handlePlayerChange = (inputValue: { value: string; label: string } | null) => {
41
    if (inputValue?.value) {
42
      setPlayerSelected(inputValue.value.replace(`/player/`, ``))
43
    } else {
44
      setPlayerSelected(``)
45
    }
46
  }
47
48
  const handleMatchChange = (event: { target: { value: React.SetStateAction<string> } }) => {
49
    setMatch(event.target.value)
50
  }
51
52
  const handleScoreChange = (event: { target: { value: React.SetStateAction<string> } }) => {
53
    setScore(event.target.value)
54
  }
55
56
  const handleGenerateImage = (event: React.MouseEvent<HTMLButtonElement>) => {
57
    event.preventDefault()
58
    if (match !== `` && score !== `` && playerSelected !== ``) {
59
      setImageUrl(``)
60
      setImageUrl(`${templateUrl}${playerSelected}?score=${score}&match=${match}`)
61
      setImageLoading(true)
62
    }
63
  }
64
65
  const handleImageLoaded = () => {
66
    setImageLoading(false)
67
  }
68
69
  return (
70
    <div>
71
      <form>
72
        <p>
73
          <label htmlFor="calendar__player">Speler:</label>
74
          <Select
75
            options={players}
76
            name="player"
77
            id="calendar__player"
78
            className="select__input"
79
            onChange={handlePlayerChange}
80
          />
81
        </p>
82
        <p>
83
          <label htmlFor="calendar__game">Wedstrijd:</label>
84
          <input
85
            id="calendar__game"
86
            onChange={handleMatchChange}
87
            className={`input__input`}
88
            placeholder="KCVV Elewijt - KFC Eppegem"
89
            value={match}
90
            required={true}
91
          />
92
        </p>
93
        <p>
94
          <label htmlFor="calendar__score">Score:</label>
95
          <input
96
            id="calendar__score"
97
            onChange={handleScoreChange}
98
            className={`input__input`}
99
            placeholder="2 — 0"
100
            value={score}
101
            required={true}
102
          />
103
        </p>
104
        <p>
105
          <button onClick={handleGenerateImage} className="btn">
106
            Creëer afbeelding.
107
          </button>
108
        </p>
109
      </form>
110
111
      <p>
112
        {imageLoading && <Spinner />}
113
        {imageUrl && <img src={imageUrl} onLoad={handleImageLoaded} />}
114
      </p>
115
    </div>
116
  )
117
}
118
119
const CreateSharePage = () => (
120
  <Layout>
121
    <SEO
122
      lang="nl-BE"
123
      title="Creëer instagram goal story"
124
      description="Creëer een nieuwe instagram story voor een doelpunt."
125
      path="calendar"
126
    />
127
128
    <div className={`limited-width_wrapper`}>
129
      <header>
130
        <h1>Creëer afbeelding</h1>
131
      </header>
132
      <main>
133
        <ShareForm />
134
      </main>
135
    </div>
136
  </Layout>
137
)
138
139
export default CreateSharePage
140