Passed
Push — feature/node-16 ( 2d86d4...d617b7 )
by Kevin Van
09:18 queued 03:35
created

src/components/RelatedNews.tsx   A

Complexity

Total Complexity 8
Complexity/F 0

Size

Lines of Code 55
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 45
mnd 8
bc 8
fnc 0
dl 0
loc 55
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React, { FunctionComponent } from "react"
2
import { CardTeaser } from "./Card"
3
import RelatedNewsProps, { RelatedNewsItem } from "./RelatedNews.types"
4
import fallbackImg from "../images/kcvv-player-bg.png"
5
6
import "./RelatedNews.scss"
7
import { getImage, getImageData, getSrc } from "gatsby-plugin-image"
8
9
const shuffle = (array: RelatedNewsItem[]): RelatedNewsItem[] => {
10
  for (let i = array.length - 1; i > 0; i--) {
11
    const j = Math.floor(Math.random() * (i + 1))
12
    const temp = array[i]
13
    array[i] = array[j]
14
    array[j] = temp
15
  }
16
  return array
17
}
18
19
const RelatedNews: FunctionComponent<RelatedNewsProps> = ({ items, limit = -1 }) => {
20
  if (limit <= 0) {
21
    limit = items.length
22
  }
23
24
  return (
25
    <section className="related_news__wrapper">
26
      {items.length > 0 && <h2 className="featured-border">Gerelateerde inhoud</h2>}
27
28
      <main className="related_news__items">
29
        {items
30
          .sort((a, b) => {
31
            if (a.timestamp && b.timestamp) {
32
              return b.timestamp - a.timestamp
33
            } else {
34
              return 0
35
            }
36
          })
37
          .splice(0, limit)
38
          .map((item, i) => {
39
            const picture =
40
              item.relationships?.field_media_article_image?.relationships?.field_media_image?.localFile
41
                ?.childImageSharp.gatsbyImageData || null
42
43
            return <CardTeaser key={i} title={item.title} picture={picture} link={item.path.alias} />
44
          })}
45
      </main>
46
    </section>
47
  )
48
}
49
50
RelatedNews.defaultProps = {
51
  limit: -1,
52
}
53
54
export default RelatedNews
55