forked from pedrocostadev/react-carousel-query
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (48 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react'
import { createRoot } from 'react-dom/client'
import ReactCarouselQuery from '../src'
const getUrl = (offset, limit) => {
const BASE_URL = 'https://gateway.marvel.com/v1/public/characters?'
const apikey = `apikey=${process.env.MARVEL_API_KEY}`
const params = `&offset=${offset}&limit=${limit}`
return `${BASE_URL}${apikey}${params}`
}
const formatData = response => {
const { offset, total, results } = response
return {
offset,
total,
items: results,
}
}
const getData = async ({ offset, limit }) => {
const url = getUrl(offset, limit)
const { data } = await (await fetch(url)).json()
return formatData(data)
}
const renderItem = ({ item }) => {
const imgSrc = `${item.thumbnail.path}.${item.thumbnail.extension}`
return (
<div style={{ height: '100%' }}>
<img
draggable={false}
alt={item.name}
style={{
display: 'block',
objectFit: 'cover',
height: '80%',
width: '100%',
background: '#ffffff url("/assets/iconLoading.svg") no-repeat center',
}}
src={imgSrc}
/>
<div style={{ height: '20%', backgroundColor: 'white', color: '#151515' }}>
<h3>{item.name}</h3>
<p className="line-clamp">{item.description || 'Description not available'}</p>
</div>
</div>
)
}
const container = document.getElementById('root')
const root = createRoot(container)
root.render(<ReactCarouselQuery fetchStep={3} renderItem={renderItem} getData={getData} />)