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

srcBU/components/Calendar.tsx   A

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 98
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 76
mnd 0
bc 0
fnc 1
dl 0
loc 98
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Calendar.tsx ➔ getData 0 4 1
1
import axios from "axios"
2
import { graphql, useStaticQuery } from "gatsby"
3
import moment from "moment-timezone"
4
import "moment-timezone/node_modules/moment/locale/nl-be"
5
import React, { FunctionComponent, useEffect, useState } from "react"
6
7
import { capitalizeFirstLetter, groupByDate } from "../scripts/helper"
8
import "./Calendar.scss"
9
import Icon from "./Icon"
10
import Spinner from "./Spinner"
11
12
const CalendarEvent: FunctionComponent<CalendarEventProps> = ({ event }: CalendarEventProps) => {
13
  moment.tz.setDefault(`Europe/Brussels`)
14
  moment.locale(`nl-be`)
15
  moment.localeData(`nl-be`)
16
17
  const startDate = moment(event.start)
18
  const endDate = moment(event.end)
19
20
  return (
21
    <article className={`calendar__event calendar__event--${event.type}`}>
22
      <div className="calendar__event__date">
23
        <Icon icon="fa-clock-o" /> {startDate.format(`HH:mm`)} - {endDate.format(`HH:mm`)}
24
      </div>
25
      <div className={`calendar__event__type calendar__event__type--${event.type}`}>
26
        {capitalizeFirstLetter(event.type)}
27
      </div>
28
      <div>
29
        {event.cancelled && (
30
          <span className="calendar__event__status calendar__event__status--cancelled">
31
            <Icon icon={`fa-times`} alt={`Afgelast`} />
32
          </span>
33
        )}
34
        {` `}
35
        <span className="calendar__event__title">{event.title}</span>
36
      </div>
37
      {event.description && <p dangerouslySetInnerHTML={{ __html: event.description }} />}
38
    </article>
39
  )
40
}
41
42
const Calendar: FunctionComponent = () => {
43
  const [data, setData] = useState<CalendarEvent[]>([])
44
  const [loading, setLoading] = useState<boolean>(true)
45
46
  const {
47
    site: {
48
      siteMetadata: { kcvvPsdApi },
49
    },
50
  }: CalendarQueryData = useStaticQuery(graphql`
51
    {
52
      site {
53
        siteMetadata {
54
          kcvvPsdApi
55
        }
56
      }
57
    }
58
  `)
59
60
  moment.tz.setDefault(`Europe/Brussels`)
61
  moment.locale(`nl-be`)
62
  moment.localeData(`nl-be`)
63
64
  useEffect(() => {
65
    async function getData() {
66
      const response = await axios.get(`${kcvvPsdApi}/events/next`)
67
      setData(response.data)
68
      setLoading(false)
69
    }
70
    getData()
71
  }, [])
72
73
  const groupedEvents = groupByDate(data)
74
75
  return (
76
    <div className={`calendar`}>
77
      {data.length > 0 || loading === false || <Spinner />}
78
      {data.length <= 0 && loading === false && <div>Er zijn voorlopig geen evenementen gepland</div>}
79
80
      {groupedEvents.map((group, i) => {
81
        const date = moment(group.date)
82
        return (
83
          <div key={i} className="calendar__events__group">
84
            <h2>{capitalizeFirstLetter(date.format(`dddd D MMMM YYYY`))}</h2>
85
            <div className={`calendar__events`}>
86
              {group.objects.map((event: CalendarEvent, j: number) => (
87
                <CalendarEvent event={event} key={j} />
88
              ))}
89
            </div>
90
          </div>
91
        )
92
      })}
93
    </div>
94
  )
95
}
96
97
export default Calendar
98