Skip to content

Commit

Permalink
Add sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Dec 26, 2022
1 parent 208c490 commit dd1f8a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
4 changes: 3 additions & 1 deletion searchlib/components/SearchApp/SearchResultsApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ function BootstrapSearchResultsView(props) {
}

export default function SearchResultsApp(props) {
const { defaultFilters } = props;
const { defaultFilters, defaultSort = '' } = props;
const [sortField, sortDirection] = defaultSort.split('|');
const [initialState] = React.useState({
...(defaultFilters?.length ? { filters: defaultFilters } : {}),
...(defaultSort ? { sortField, sortDirection } : {}),
}); // this makes the prop stable

return (
Expand Down
3 changes: 2 additions & 1 deletion src/SearchBlock/SearchBlockView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ function SearchBlockView(props) {
return (
<div>
{mode !== 'view' && 'EEA Semantic Search block'}
{JSON.stringify(data.defaultFilters)}
{JSON.stringify(data)}
<Variation
registry={registry}
appName={appName}
mode={mode}
defaultSort={data.defaultSort}
defaultFilters={data.defaultFilters
?.map((f) => (f.value ? f.value : undefined))
.filter((f) => !!f)}
Expand Down
48 changes: 31 additions & 17 deletions src/SearchBlock/templates/SearchResultsView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,34 @@ SearchResultsView.schemaEnhancer = ({ schema, formData }) => {
'alwaysSearchOnInitialLoad',
'showFilters',
'defaultFilters',
'defaultSort',
);

schema.properties.alwaysSearchOnInitialLoad = {
title: 'Autoload results',
type: 'boolean',
default: true,
configPath: 'alwaysSearchOnInitialLoad',
};
schema.properties.showFilters = {
title: 'Show filters?',
type: 'boolean',
default: true,
configPath: 'showFilters',
};
schema.properties.defaultFilters = {
title: 'Default filters',
widget: 'object_list',
schema: FilterSchema({ formData }),
schemaExtender: (schema) => schema,
schema.properties = {
...schema.properties,

alwaysSearchOnInitialLoad: {
title: 'Autoload results',
type: 'boolean',
default: true,
configPath: 'alwaysSearchOnInitialLoad',
},
showFilters: {
title: 'Show filters?',
type: 'boolean',
default: true,
configPath: 'showFilters',
},
defaultFilters: {
title: 'Default filters',
widget: 'object_list',
schema: FilterSchema({ formData }),
schemaExtender: (schema) => schema,
},
defaultSort: {
title: 'Default sort',
choices: [],
},
};

if (appConfig) {
Expand All @@ -122,6 +131,11 @@ SearchResultsView.schemaEnhancer = ({ schema, formData }) => {
extender.applied = true;
schema.properties.defaultFilters.schemaExtender = extender;
}

schema.properties.defaultSort.choices = appConfig.sortOptions.map((opt) => [
`${opt.value}|${opt.direction}`,
opt.name,
]);
}

return schema;
Expand Down

0 comments on commit dd1f8a4

Please sign in to comment.