Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of the universalLimit in verticalsToConfig. #1021

Merged
merged 5 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions hbshelpers/getDefaultUniversalLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Generates a default result limit for each vertical on the universal page. The
* default is taken from the vertical's `universalLimit`, specified in the related
* page's VTC. If there is no such `universalLimit`, the back-end default of 10 will
* be used.
*
* @param {Object} pageConfigs - The configurations for each page.
* @returns {Object} The partial of the search configuration related to universal limits.
*/
module.exports = function getDefaultUniversalLimit(pageConfigs) {
const universalLimit = Object.entries(pageConfigs)
.filter(([pageName, _]) => pageName != 'index')
.reduce((limit, [_, pageConfig]) => {
const verticalKey = pageConfig.verticalKey;
const hasUniversalLimit =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the following code might be slightly simpler if this is just const universalLimit = ... but also feels like a personal preference thing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I had something like that initially.

const universalLimit = blah?.blah?.universalLimit

But somehow that syntax routinely caused our Percy snapshot generator to crash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I don't think optional chaining is supported for all the node versions we support. I was just thinking we coudl rename hasUniversalLimit to just universalLimit and then line 20 doesn't need to repeat pageConfig.verticalsToConfig[verticalKey].universalLimit

pageConfig.verticalsToConfig &&
pageConfig.verticalsToConfig[verticalKey] &&
pageConfig.verticalsToConfig[verticalKey].universalLimit;
if (hasUniversalLimit) {
limit[verticalKey] = pageConfig.verticalsToConfig[verticalKey].universalLimit;
}

return limit;
}, {});

return { universalLimit };
tmeyer2115 marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 6 additions & 0 deletions script/core.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
...{{{ json search }}}
},
{{/if}}
{{#unless ../verticalKey}}
search: {
...{{{ json (getDefaultUniversalLimit ../verticalConfigs) }}},
...{{{ json search }}}
},
{{/unless}}
{{/with}}
};
const token = window.AnswersExperience.runtimeConfig?.get('token');
Expand Down
13 changes: 0 additions & 13 deletions templates/universal-standard/script/universalresults.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ ANSWERS.addComponent("UniversalResults", Object.assign({}, {
pin: {{> templates/universal-standard/script/map-pin mapConfig }},
}),
{{/if}}
{{#if universalLimit}}
transformData: (data) => {
let results = data.results;
if (results) {
results = results.filter((rex, idx) => {
return idx < {{{universalLimit}}};
});
}
return Object.assign(data, {
results: results
});
},
{{/if}}
{{/inline}}

{{!--
Expand Down