-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentfulManager.js
156 lines (141 loc) · 3.78 KB
/
ContentfulManager.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { createClient } from 'contentful/dist/contentful.browser.min.js'
function transformMedia(image) {
try {
return {
src: `https:${image.fields.file.url}`,
width: image.fields.file.details?.image?.width,
height: image.fields.file.details?.image?.height,
}
} catch (error) {
console.warn('Unable to transform image in transformMedia')
return {
src: '',
width: null,
height: null,
}
}
}
function transformAuthor(author) {
return {
id: author.sys.id,
name: author.fields.name,
profilePhoto: transformMedia(author.fields.profilePhoto),
title: author.fields.title,
discordUsername: author.fields.discordUsername,
discordId: author.fields.discordId,
}
}
function transformArtists(artists) {
return artists.map(transformArtist)
}
export function transformArtist(artist) {
return {
id: artist.sys.id,
name: artist.fields.name,
coverPhoto: transformMedia(artist.fields.coverPhoto),
spotifyUrl: artist.fields.spotifyUrl,
soundCloudUrl: artist.fields.soundCloudUrl,
}
}
function transformBlogPost(post) {
return {
id: post.sys.id,
title: post.fields.title,
author: transformAuthor(post.fields.author),
coverPhoto: transformMedia(post.fields.coverPhoto),
content: post.fields.content,
createdAt: post.sys.createdAt,
}
}
function transformNewsItem(post) {
return {
id: post.sys.id,
title: post.fields.title,
link: post.fields.link,
}
}
function transformBlogPosts(posts) {
return posts.map(transformBlogPost)
}
function transformNews(posts) {
return posts.map(transformNewsItem)
}
function transformEvent(event) {
return {
id: event.sys.id,
name: event.fields.name,
shortDescription: event.fields.shortDescription,
coverCharge: event.fields.coverCharge,
coverPhoto: transformMedia(event.fields.coverPhoto),
date: event.fields.date,
endDateTime: event.fields.endDateTime,
startDateTime: event.fields.startDateTime,
location: event.fields.location,
locationAddress: event.fields.locationAddress,
locationName: event.fields.locationName,
artists: transformArtists(event.fields.artists),
about: event.fields.about,
}
}
function transformEvents(events) {
return events.map(transformEvent)
}
class ContentfulManager {
constructor() {
this.client = createClient({
// This is the space ID. A space is like a project folder in Contentful terms
space: 'z08x63qrkj2y',
// This is the access token for this space. Normally you get both ID and the token in the Contentful web app
accessToken: 'LZCL1WbvDarZQA9wRhPv0tkwc8vkuVVyRpbqpy-D8T0',
})
}
getEvents = () => {
return new Promise((resolve, reject) => {
this.client
.getEntries({
content_type: 'event',
include: 10,
})
.then((response) => {
resolve(transformEvents(response?.items || []))
})
.catch((error) => {
console.warn(error)
reject(error)
})
})
}
getBlogPosts = () => {
return new Promise((resolve, reject) => {
this.client
.getEntries({
content_type: 'blogPost',
include: 10,
})
.then((response) => {
resolve(transformBlogPosts(response?.items || []))
})
.catch((error) => {
console.warn(error)
reject(error)
})
})
}
getNews = () => {
return new Promise((resolve, reject) => {
this.client
.getEntries({
content_type: 'news',
include: 10,
})
.then((response) => {
resolve(transformNews(response?.items || []))
})
.catch((error) => {
console.warn(error)
reject(error)
})
})
}
}
export default new ContentfulManager()