Skip to content

Commit

Permalink
WIP on implementing facet filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Dec 20, 2022
1 parent 3407055 commit d01e215
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/SearchBlock/SearchBlockEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ const SearchBlockEdit = (props) => {

const schema = React.useMemo(() => {
const schema = SearchBlockSchema({ formData: stableData || {} });
const conf = config.settings.searchlib.searchui;
schema.properties.appName.choices = Object.keys(conf).map((k) => [

const { searchui } = config.settings.searchlib;

schema.properties.appName.choices = Object.keys(searchui).map((k) => [
k,
k,
// conf[k].title || k,
]);

const resultViews = conf[stableData.appName || 'default'].resultViews;

schema.properties.defaultResultView = {
...schema.properties.defaultResultView,
choices: resultViews.map(({ id, title }) => [id, title]),
default: resultViews.find(({ isDefault }) => isDefault).id,
};
schema.appName = stableData.appName || 'default';
schema.appConfig = searchui[schema.appName];

return schema;
}, [stableData]);
Expand Down
70 changes: 69 additions & 1 deletion src/SearchBlock/templates/SearchResultsView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,56 @@ export default function SearchResultsView(props) {
);
}

SearchResultsView.schemaEnhancer = ({ schema }) => {
function FilterSchema({ formData }) {
return {
title: 'Filter',
fieldsets: [
{
id: 'default',
title: 'Default',
fields: ['name', 'value'],
},
],
properties: {
name: {
title: 'Filter',
choices: [],
},
value: {
title: 'Value',
widget: 'facet_value',
},
},
required: [],
};
}

const setFacetWidgetProps = (appConfig, data) => {
return (schema, data, intl) => {
// Note: this is a hack, it's needed to be able to pass the computed
// appConfig from the edit block. Hackish because the block schemaEnhancers
// (and the ObjectWidget) aren't passed the whole available props

schema.properties.name.choices = appConfig.facets.map((facet) => [
facet.id || facet.field,
facet.label || facet.field,
]);

console.log('setfacet', { schema, appConfig, data });
return schema;
};
};

SearchResultsView.schemaEnhancer = ({ schema, formData }) => {
const { appConfig } = schema;

schema.fieldsets[0].fields.unshift(
'defaultResultView',
'alwaysSearchOnInitialLoad',
'showFilters',
'defaultFilters',
);

schema.properties.alwaysSearchOnInitialLoad = {
title: 'Autoload results',
type: 'boolean',
Expand All @@ -41,6 +85,30 @@ SearchResultsView.schemaEnhancer = ({ schema }) => {
default: true,
configPath: 'showFilters',
};
schema.properties.defaultFilters = {
title: 'Default filters',
widget: 'object_list',
schema: FilterSchema({ formData }),
schemaExtender: (schema) => schema,
};

if (appConfig) {
const { resultViews } = appConfig;

// fill in defaultResultView choices
schema.properties.defaultResultView = {
...schema.properties.defaultResultView,
choices: resultViews.map(({ id, title }) => [id, title]),
default: resultViews.find(({ isDefault }) => isDefault).id,
};
const { schemaExtender } = schema.properties.defaultFilters;

if (!schemaExtender.applied) {
const extender = setFacetWidgetProps(appConfig);
extender.applied = true;
schema.properties.defaultFilters.schemaExtender = extender;
}
}

return schema;
};

0 comments on commit d01e215

Please sign in to comment.