Skip to content

Commit

Permalink
Adds support for resolve: values Array-based serverless pagination …
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 17, 2023
1 parent 9ff1ca9 commit 9574ae8
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Plugins/Pagination.js
Expand Up @@ -328,10 +328,18 @@ class Pagination {

// Serverless pagination:
if (this._has(this.data, "pagination.serverless")) {
let serverlessPaginationKey = 0;
let serverlessPaginationKey;

if (this.paginationTargetType === "object" && this.shouldResolveDataToObjectValues()) {
serverlessPaginationKey = Object.keys(this.fullDataSet)[0];
} else {
serverlessPaginationKey = 0;
}

if (this._has(this.data, this.data.pagination.serverless)) {
serverlessPaginationKey = this._get(this.data, this.data.pagination.serverless);
}

if (this.paginationTargetType === "array") {
currentPageIndex = parseInt(serverlessPaginationKey, 10);

Expand All @@ -347,8 +355,13 @@ class Pagination {
}
indeces.add(items.length - 1); // last
} else if (this.paginationTargetType === "object") {
// TODO support pagination `resolve: values` here, right now it only works with keys (`this.shouldResolveDataToObjectValues()`)
currentPageIndex = items.findIndex((entry) => entry[0] === serverlessPaginationKey);
if (this.shouldResolveDataToObjectValues()) {
currentPageIndex = Object.keys(this.fullDataSet).findIndex(
(key) => key === serverlessPaginationKey
);
} else {
currentPageIndex = items.findIndex((entry) => entry[0] === serverlessPaginationKey);
}
indeces.add(currentPageIndex); // current
}
} else {
Expand Down Expand Up @@ -387,7 +400,11 @@ class Pagination {
key.map((key) => this._get(this.fullDataSet, key))
);
} else {
lodashSet(paginationData, this.alias, this._get(this.fullDataSet, keys));
if (this.shouldResolveDataToObjectValues()) {
lodashSet(paginationData, this.alias, keys);
} else {
lodashSet(paginationData, this.alias, this._get(this.fullDataSet, keys));
}
}
} else {
lodashSet(paginationData, this.alias, this.getNormalizedItems(items[pageNumber]));
Expand All @@ -414,7 +431,7 @@ class Pagination {

if (validUrls.length === 0) {
throw new Error(
`Serverless pagination template has no \`permalink.${key}\` with \`/:${key}/\``
`Serverless pagination template (${this.data.page.inputPath}) has no \`permalink.${key}\` with \`/:${key}/\``
);
}
href = serverlessUrlFilter(validUrls[0], { [key]: pageNumber });
Expand Down

0 comments on commit 9574ae8

Please sign in to comment.