-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentful.js
391 lines (357 loc) · 13.9 KB
/
contentful.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const space = process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID
const accessToken = process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN
const client = require('contentful').createClient({
space: space,
accessToken: accessToken,
})
const aftcoClient = require('contentful').createClient({
space: 'vypp6kxkjn7l',
accessToken: 'CZRaIX910RmS-OEFcaonRPXkDi8Uajc6g07OXHl3pOY',
})
export async function menuEntries() {
const entries = await aftcoClient.getEntries({'content_type': 'menu'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function menuItem() {
const entries = await aftcoClient.getEntries({'content_type': 'menuItem','limit':500})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function variantColor() {
const entries = await aftcoClient.getEntries({'content_type': 'variantColor','limit':500})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getBadge() {
const entries = await aftcoClient.getEntries({'content_type': 'badge','limit':500})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getTechIco() {
const entries = await aftcoClient.getEntries({'content_type': 'techIcons'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getPdpBadge() {
const entries = await aftcoClient.getEntries({'content_type': 'pdpBadge'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getSizeChart() {
const entries = await aftcoClient.getEntries({'content_type': 'sizeCharts'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getcollectionSlugs() {
const entries = await aftcoClient.getEntries({'content_type': 'collections','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getblogCategoriesSlugs() {
const entries = await aftcoClient.getEntries({'content_type': 'blogCategories','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getSingleblogCategories(slug) {
const entries = await aftcoClient.getEntries({'content_type': 'blogCategories', 'fields.slug': slug,'limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getPagesSlugs() {
const entries = await aftcoClient.getEntries({'content_type': 'page','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getPagesSlugsTest() {
const entries = await aftcoClient.getEntries({'content_type': 'page','limit':1000, 'links_to_entry':'mPOiy74AiRhD8ZffJ7CQl'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getAllBlogPost() {
const entries = await aftcoClient.getEntries({'content_type': 'blog','limit':1000, 'order': '-fields.publishDate'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getSingleBlogPost(slug) {
const entries = await aftcoClient.getEntries({'content_type': 'blog','fields.slug': slug,'limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getGroupOfBlogCat() {
const entries = await aftcoClient.getEntries({'content_type': 'blogGroupOfCategories','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getFilter() {
const entries = await aftcoClient.getEntries({'content_type': 'filter','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getGwp() {
const entries = await aftcoClient.getEntries({'content_type': 'relatedAddToCart','limit':100})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getBannerList() {
const entries = await aftcoClient.getEntries({'content_type': 'productsBannerList','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getChainList() {
const entries = await aftcoClient.getEntries({'content_type': 'chainProduct'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getPdpRichText() {
const entries = await aftcoClient.getEntries({'content_type': 'richText','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getGroupOfCollection() {
const entries = await aftcoClient.getEntries({'content_type': 'groupOfCOllection','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getAds() {
const entries = await aftcoClient.getEntries({'content_type': 'ads'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getdiscountList() {
const entries = await aftcoClient.getEntries({'content_type': 'discount','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getVipdiscountList() {
const entries = await aftcoClient.getEntries({'content_type': 'vipDiscount'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getTopBar() {
const entries = await aftcoClient.getEntries({'content_type': 'topBar'})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getRedirectList() {
const entries = await aftcoClient.getEntries({'content_type': 'redirectList','limit':1000})
if (entries.items) return entries.items
console.log(`Error getting Entries for ${contentType.name}.`)
}
export async function getRelatedBlogPost(product, collections, productType ) {
let collection
collections ?
collection = collections.map((collection) => (
collection.node.handle
)) : "";
let blog
collection ? blog = collection.join(",") :""
const blogByHandle = await aftcoClient.getEntries({'content_type': 'blog', 'fields.productHandle[in]':product})
const blogByCollections = await aftcoClient.getEntries({'content_type': 'blog', 'fields.productCategory[in]':blog})
const blogByType = await aftcoClient.getEntries({'content_type': 'blog', 'fields.productCategory[in]':productType})
const blogByHandleTag = await aftcoClient.getEntries({'content_type': 'blog', 'fields.tags[in]':product})
const blogByCollectionsTag = await aftcoClient.getEntries({'content_type': 'blog', 'fields.tags[in]':blog})
const blogByTypeTag = await aftcoClient.getEntries({'content_type': 'blog', 'fields.tags[in]':productType})
const latestPost = await aftcoClient.getEntries({'content_type': 'blog', 'limit':5})
const allPost = blogByHandle.items.concat(blogByCollections.items, blogByType.items, latestPost.items, blogByHandleTag.items, blogByCollectionsTag.items, blogByTypeTag.items ).slice(0, 5);
const jsonObject = allPost.map(JSON.stringify);
const uniqueSet = new Set(jsonObject);
const uniqueArray = Array.from(uniqueSet).map(JSON.parse);
if (uniqueArray)
return uniqueArray
console.log(`Error getting Entries for ${contentType.name}.`)
}
async function getNavData(navs, contenfulData) {
const menuItem = contenfulData.menuSlug
const menuData = menuItem.concat(contenfulData.page, contenfulData.post, contenfulData.collections)
let entryNeeded = []
navs.map((menu) => {
menu.menuItem.map((item) => {
const mainMenuData = menuData.find((idxs) => {
return idxs.sys.id === item.sys.id;
})
if (mainMenuData != undefined) {
entryNeeded.push(mainMenuData)
}
<>
{item.fields.featured?
item.fields.featured.map((featured) =>
{
const featuredData = menuData.find((idxs) => {
return idxs.sys.id === featured.sys.id;
})
if (featuredData != undefined) {
entryNeeded.push(featuredData)
}
}
)
:""}
{item.fields.subMenu?
item.fields.subMenu.map((subMenu) =>
{
const subMenuData = menuData.find((idxs) => {
return idxs.sys.id === subMenu.sys.id;
})
if (subMenuData != undefined) {
entryNeeded.push(subMenuData)
}
}
)
:""
}
</>
})
})
entryNeeded.map((data) =>
data.fields.subMenu?
data.fields.subMenu.map((subMenu) =>
{
const subMenuData = menuData.find((idxs) => {
return idxs.sys.id === subMenu.sys.id;
})
if (subMenuData != undefined) {
entryNeeded.push(subMenuData)
}
}
)
:""
)
return entryNeeded
}
export async function getNavDataNeeded(navs, contenfulData) {
const navsData = await getNavData(navs, contenfulData)
const dataNeeded = navsData.reduce(function(a,b){
if (a.findIndex(img => img.sys.id === b.sys.id) < 0 ) a.push(b);
return a;
},[]);
const myJSON = JSON.stringify(dataNeeded)
const obj = JSON.parse(myJSON)
obj.map(data => {
if(data.hasOwnProperty('metadata')) {
delete data.metadata
}
if(data.fields.hasOwnProperty('content')) {
delete data.fields.content
}
if(data.fields.hasOwnProperty('meta_Description')) {
delete data.fields.meta_Description
}
if(data.fields.hasOwnProperty('metaTitleTag')) {
delete data.fields.metaTitleTag
}
if(data.fields.hasOwnProperty('shopifySource')) {
delete data.fields.shopifySource
}
if(data.fields.hasOwnProperty('heroImage')) {
delete data.fields.heroImage
}
if(data.fields.hasOwnProperty('subTag')) {
delete data.fields.subTag
}
})
return obj
}
export async function getRichTextDataNeeded(contents, contenfulData) {
let bannerList = []
let blogCat = []
let ColforPage = []
let featuredPost = []
let latestPost = null
contents.map(content =>{
const nodeType = content.nodeType
if(nodeType == "embedded-entry-block") {
const type = content.data.target.sys.contentType.sys.id
if (type == "productsBanner") {
content.data.target.fields.productList.map(prod => {
const prodData = contenfulData.bannerList.find((idxs) => {
return idxs.sys.id === prod.sys.id;
})
if (prodData != undefined) {
bannerList.push(prodData)
}
if(prodData.fields.contentSource && prodData.fields.contentSource.fields.blogCategory) {
const blogCatData = contenfulData.collections.find((idxs) => {
return idxs.sys.id === prodData.fields.contentSource.fields.blogCategory.sys.id;
})
if (blogCatData != undefined) {
blogCat.push(blogCatData)
}
}
})
}
if (type == "productsList" && content.data.target.fields.collection) {
const prodData = contenfulData.collections.find((idxs) => {
return idxs.sys.id === content.data.target.fields.collection.sys.id;
})
if (prodData != undefined) {
ColforPage.push(prodData)
}
}
if (type == "featuredCateggories") {
content.data.target.fields.categoriesList.map(col => {
const colData = contenfulData.collections.find((idxs) => {
return idxs.sys.id === col.sys.id;
})
if (colData != undefined) {
blogCat.push(colData)
}
})
}
if (type == "featuredPost") {
const type = content.data.target.fields.type
if( type == "Based on post entries" ) {
const postList = content.data.target.fields.postList
postList.map(list => {
featuredPost.push(contenfulData.post.find((idxs) => {
return idxs.sys.id === list.sys.id;
}))
})
}
else if( type == "Latest Post" ) {
const numberOfPost = content.data.target.fields.maxPost
latestPost = contenfulData.post.slice(0, numberOfPost)
}
else {
const catList = content.data.target.fields.categoriesList
catList.map(list => {
const catDetail = contenfulData.collections.find((idxs) => {
return idxs.sys.id === list.sys.id;
})
const post = contenfulData.post.filter((idxs) => {
let id
if (idxs.fields.blogCategory){
id = idxs.fields.blogCategory.sys.id
}
else {
id = null
}
return id === list.sys.id;
})
post.map((data,i) =>{i<5?featuredPost.push(data):""})
blogCat.push(catDetail)
})
}
}
}
})
const richtext = {"bannerList":bannerList,"post":featuredPost,"latestPost":latestPost,"blogCat":blogCat,"ColforPage":ColforPage}
return richtext
}
export default {
menuEntries,
menuItem,
variantColor,
getblogCategoriesSlugs,
getcollectionSlugs,
getPagesSlugs,
getAllBlogPost,
getSingleBlogPost,
getRelatedBlogPost,
getFilter,
getBannerList,
getChainList,
getPdpRichText,
getNavDataNeeded,
getRichTextDataNeeded,
getTopBar
}