Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #721 from Shinsina/sort-configuration
Browse files Browse the repository at this point in the history
Enable configuration of `defaultSortField` within `MarkoWebSearchConfig` and further `MarkoWebSearchQueryParamConfig`
  • Loading branch information
brandonbk authored Jun 12, 2023
2 parents aa7964b + 7b99da6 commit 44fd13f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions packages/marko-web-search/components/sort-by/index.marko
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { get, getAsArray, getAsObject } from "@parameter1/base-cms-object-path";
import { dasherize } from "@parameter1/base-cms-inflector";
import defaultValue from "@parameter1/base-cms-marko-core/utils/default-value";
import { isFunction } from '@parameter1/base-cms-utils';

$ const { $markoWebSearch: search, i18n } = out.global;
Expand All @@ -16,7 +15,7 @@ $ const options = isFunction(i18n) ? [

<marko-web-block name=blockName modifiers=input.modifiers>
$ const { searchQuery, sortField } = search.input;
$ const selectedId = input.sortField || sortField;
$ const selectedId = sortField;
<if(searchQuery)>
<marko-web-browser-component
name="MarkoWebSearchSortSelect"
Expand Down
4 changes: 4 additions & 0 deletions packages/marko-web-search/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MarkoWebSearchConfig {
resultsPerPage,
contentTypes,
assignedToWebsiteSectionIds,
defaultSortField,
} = validate(Joi.object({
resultsPerPage: Joi.object({
min: Joi.number().integer().default(1),
Expand All @@ -48,6 +49,8 @@ class MarkoWebSearchConfig {
assignedToWebsiteSectionIds: Joi.array().items(
Joi.number().integer().min(1).required(),
).default([]),

defaultSortField: Joi.string().allow('PUBLISHED', 'SCORE').default('PUBLISHED'),
}).default(), params);

this.contentTypeObjects = contentTypes.sort().map((type) => (type.label ? ({
Expand All @@ -68,6 +71,7 @@ class MarkoWebSearchConfig {
this.queryParams = new MarkoWebSearchQueryParamConfig({
resultsPerPage,
contentTypeIds: this.contentTypeObjects.map(({ id }) => id),
defaultSortField,
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/marko-web-search/config/query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MarkoWebSearchQueryParamConfig {
constructor({
resultsPerPage = {},
contentTypeIds = [],
defaultSortField = 'PUBLISHED',
} = {}) {
this.params = new Map();

Expand Down Expand Up @@ -71,7 +72,7 @@ class MarkoWebSearchQueryParamConfig {
})
.add('sortField', {
type: String,
defaultValue: 'PUBLISHED',
defaultValue: defaultSortField,
validator: (v) => sortFieldSet.has(v),
})
.add('sortOrder', {
Expand Down
2 changes: 2 additions & 0 deletions packages/marko-web-theme-monorail/routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ module.exports = (app, siteConfig) => {
const {
contentTypes = ['Article', 'Blog', 'Company', 'Podcast', 'Product', 'Video', 'Whitepaper'],
assignedToWebsiteSectionIds,
defaultSortField,
} = getAsObject(siteConfig, 'search');
const config = new MarkoWebSearchConfig({
resultsPerPage: { default: 18 },
contentTypes,
assignedToWebsiteSectionIds,
defaultSortField,
});
app.get('/search', (req, res, next) => {
if (!get(req, 'query.searchQuery') && get(req, 'query.sortField')) {
Expand Down
8 changes: 2 additions & 6 deletions packages/marko-web-theme-monorail/templates/search.marko
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ $ const description = i18nIsFunction ? `${i18n("Search")} ${config.siteName()}`

$ const currentPage = search.getCurrentPage();

$ const overrideSortField = get(site, 'config.setSearchSortFieldToScore');
$ const searchQueryPresent = (search.query.searchQuery && search.query.searchQuery !== '');
$ const setDefaultSortField = (searchQueryPresent && !search.query.sortField && overrideSortField);
$ const sortField = setDefaultSortField ? 'SCORE' : search.input.sortField;
$ const sponsorLogo = site.get('search.sponsorLogos.page');

<marko-web-default-page-layout type=type title=title description=description>
Expand Down Expand Up @@ -73,7 +69,7 @@ $ const sponsorLogo = site.get('search.sponsorLogos.page');
</marko-web-search-form>
</div>
<div class="col-xl-3">
<marko-web-search-sort-by sort-field=sortField />
<marko-web-search-sort-by />
<if(sponsorLogo)>
<div class="search-sponsor">
<span class="search-sponsor__label">Sponsored by</span>
Expand All @@ -91,7 +87,7 @@ $ const sponsorLogo = site.get('search.sponsorLogos.page');
<marko-web-search-query|{ nodes, totalCount }|
limit=search.getLimit()
skip=search.getSkip()
sort-field=sortField
sort-field=search.input.sortField
sort-order=search.input.sortOrder
content-types=search.input.contentTypes
search-query=search.input.searchQuery
Expand Down

0 comments on commit 44fd13f

Please sign in to comment.