-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection-loader.js
148 lines (138 loc) · 4.34 KB
/
collection-loader.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
const get = require('lodash/get')
const flatMap = require('lodash/flatMap')
function loadCollectionItems (
client,
collections,
{ storyFields, storyLimits = {}, defaultNestedLimit, customLayouts = [], previewId = '', qtInternalAppsKey = '' }
) {
const bulkRequestBody = collections.reduce((acc, collection, index) => {
let limit = storyLimits[get(collection, ['associated-metadata', 'layout'], '')]
const customLayout = get(customLayouts, [index])
if (customLayout) {
limit = get(customLayout, ['storyLimit'])
}
if (!limit && get(collection, ['childCollectionLimit'])) {
limit = get(collection, ['childCollectionLimit'])
}
if (!limit && defaultNestedLimit) {
limit = defaultNestedLimit
}
return Object.assign(acc, {
[`${collection.slug}-${index}`]: {
_type: 'collection',
slug: collection.slug,
'story-fields': storyFields,
limit: limit
}
})
}, {})
const opts = { previewId, qtInternalAppsKey }
return client.getInBulk({ requests: bulkRequestBody }, opts).then(response => response.results)
}
// Ugly. This function updates all the items in place.
// However, this is way more readable than a pure version
function updateItemsInPlace (
client,
depth,
items,
{
storyFields,
storyLimits,
defaultNestedLimit,
nestedCollectionLimit,
collectionOfCollectionsIndexes = [],
customLayouts = [],
previewId = "",
qtInternalAppsKey = ""
}
) {
const collections = items.filter(item => item && item.type == 'collection')
if (depth == 0 || collections.length == 0) return Promise.resolve()
return loadCollectionItems(client, collections, {
storyFields,
storyLimits,
defaultNestedLimit,
customLayouts,
previewId,
qtInternalAppsKey
}).then(collectionSlugToCollection => {
collections.forEach((collection, index) => {
const slugWithIndex = `${collection.slug}-${index}`
collection.summary = get(collectionSlugToCollection, [slugWithIndex, 'summary'], '')
collection.automated = get(collectionSlugToCollection, [slugWithIndex, 'automated'])
collection['collection-cache-keys'] = get(
collectionSlugToCollection,
[slugWithIndex, 'collection-cache-keys'],
[]
)
collection.items = get(collectionSlugToCollection, [slugWithIndex, 'items'], [])
if (nestedCollectionLimit && collection.items) {
const customLayout = get(customLayouts, [index])
collection.items.forEach((item, index) => {
if (item.type === 'collection') {
if (customLayout && customLayout.nestedCollectionLimit) {
item.childCollectionLimit = get(customLayout, ['nestedCollectionLimit', index])
} else if (nestedCollectionLimit[get(collection, ['associated-metadata', 'layout'])]) {
item.childCollectionLimit =
nestedCollectionLimit[get(collection, ['associated-metadata', 'layout'])][index]
}
}
})
}
})
if (collectionOfCollectionsIndexes.length) {
const updatedCollections = collections
.filter((_, index) => collectionOfCollectionsIndexes.includes(index))
.map(childCollection => {
return updateItemsInPlace(client, 1, childCollection.items, {
storyFields,
storyLimits,
defaultNestedLimit,
previewId,
qtInternalAppsKey
})
})
return Promise.all(updatedCollections).then(collections => collections)
} else {
return updateItemsInPlace(
client,
depth - 1,
flatMap(collections, collection => collection.items),
{
storyFields,
storyLimits,
defaultNestedLimit,
previewId,
qtInternalAppsKey
}
)
}
})
}
function loadNestedCollectionData (
client,
collection,
{
depth,
storyFields,
storyLimits,
defaultNestedLimit,
nestedCollectionLimit,
collectionOfCollectionsIndexes,
customLayouts,
previewId,
qtInternalAppsKey
}
) {
return updateItemsInPlace(client, depth, collection.items, {
storyFields,
storyLimits,
defaultNestedLimit,
nestedCollectionLimit,
collectionOfCollectionsIndexes,
customLayouts,
previewId,
qtInternalAppsKey
}).then(() => collection)
}
module.exports = { loadNestedCollectionData }