-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcontentfulClient.js
More file actions
66 lines (52 loc) · 1.59 KB
/
Copy pathcontentfulClient.js
File metadata and controls
66 lines (52 loc) · 1.59 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
import * as contentful from 'contentful'
const client = contentful.createClient({
space: 'bpm5i5mj2o02',
accessToken: 'kDjw4Ergl45z3NFMv6Bhd5Z5w_HTIdcioiY4Hq9mC0I' // read-only for free plan, dont worry :)
})
export const monthNames = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]
// --------- one cached fetch for *all* entries ---------
let allEntriesPromise = null
export const getEntries = async() => {
if (allEntriesPromise) return allEntriesPromise
allEntriesPromise = (async() => {
const pageSize = 1000
let skip = 0
const all = []
while (true) {
const res = await client.getEntries({ limit: pageSize, skip })
all.push(...res.items)
skip += res.items.length
if (all.length >= res.total || res.items.length === 0) break
}
return all
})().catch((err) => {
allEntriesPromise = null
throw err
})
return allEntriesPromise
}
const byType = (items, contentTypeId) =>
items.filter((e) => e.sys?.contentType?.sys?.id === contentTypeId)
export const getNews = async() => {
const items = byType(await getEntries(), 'latestNews')
return items.map(({ fields }) => ({
...fields,
image: fields.image?.fields,
imageDark: fields.imageDark?.fields
}))
}
export const getEvents = async() => {
const items = byType(await getEntries(), 'event')
return items.map(({ fields }) => ({
...fields,
image: fields.image?.fields
}))
}
export const getSponsors = async() => {
return byType(await getEntries(), 'sponsor')
}
export const clearEntriesCache = () => {
allEntriesPromise = null
}