Skip to content

Commit

Permalink
Allow FullView to tweak the global config that will be used
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Feb 28, 2023
1 parent 906a619 commit 0c93c8a
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 170 deletions.
31 changes: 25 additions & 6 deletions searchlib/components/SearchView/SearchView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import React from 'react';

import { Link, useLocation } from 'react-router-dom';
import { withAppConfig } from '@eeacms/search/lib/hocs';
import { Icon } from 'semantic-ui-react';
import {
Expand Down Expand Up @@ -80,18 +81,36 @@ export const SearchView = (props) => {

// React.useEffect(() => () => console.log('unmount SearchView'), []);

const domain = typeof window !== 'undefined' ? window.location.host : null;
const { landingPageURL } = appConfig;
let backToHome = landingPageURL;

if (landingPageURL) {
const url = new URL(landingPageURL);
if (url.host === domain) {
backToHome = url.pathname;
}
}

return (
<div className={`searchapp searchapp-${appName} ${customClassName}`}>
<Layout
appConfig={appConfig}
header={
<>
{wasInteracted && (
<a href={appConfig.landingPageURL} className="back-link">
<Icon className="arrow left" />
Back to search home
</a>
)}
{wasInteracted &&
backToHome &&
(backToHome.startsWith('/') ? (
<Link to={backToHome} className="back-link">
<Icon className="arrow left" />
Back to search home
</Link>
) : (
<a href={backToHome} className="back-link">
<Icon className="arrow left" />
Back to search home
</a>
))}
<RenderSlot
{...props}
searchState={searchState}
Expand Down
14 changes: 11 additions & 3 deletions src/SearchBlock/templates/FullView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { BodyClass } from '@plone/volto/helpers';
import { SEARCH_STATES, SLOTS, SearchApp } from '@eeacms/search';
import { SlotEditor, BlockContainer } from './../BlockContainer';
import { searchResultsSchemaEnhancer } from './schema';

const slotCombinations = SLOTS.reduce(
(acc, slot) => [
Expand Down Expand Up @@ -92,9 +93,16 @@ function FullView(props) {
);
}

FullView.schemaEnhancer = ({ schema }) => {
// schema.fieldsets[0].fields.unshift('defaultResultView');

FullView.schemaEnhancer = (props) => {
// searchResultsSchemaEnhancer;
const schema = searchResultsSchemaEnhancer(props);
schema.fieldsets[1].fields.push('showLandingPage');
schema.properties.showLandingPage = {
title: 'Show landing page?',
type: 'boolean',
default: true,
configPath: 'showLandingPage',
};
return schema;
};

Expand Down
162 changes: 2 additions & 160 deletions src/SearchBlock/templates/SearchResultsView.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { BodyClass } from '@plone/volto/helpers';
import { SearchResultsApp } from '@eeacms/search';
import { searchResultsSchemaEnhancer } from './schema';

export default function SearchResultsView(props) {
const { appName, mode } = props;
Expand All @@ -24,163 +25,4 @@ export default function SearchResultsView(props) {
);
}

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, registry, appName) => {
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.activeFilterLabel || facet.label || facet.field,
]);

schema.properties.value.facetName = data.name;
schema.properties.value.appConfig = appConfig;
schema.properties.value.appName = appName;
schema.properties.value.registry = registry;

return schema;
};
};

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

schema.fieldsets.splice(1, 0, {
id: 'searchResultsSettings',
title: 'Search results settings',
fields: [
'defaultResultView',
'alwaysSearchOnInitialLoad',
'showFilters',
'showFacets',
'showSorting',
'showClusters',
'availableFacets',
'defaultFacets',
'defaultFilters',
'defaultSort',
],
});

schema.properties = {
...schema.properties,

alwaysSearchOnInitialLoad: {
title: 'Autoload results',
type: 'boolean',
default: true,
configPath: 'alwaysSearchOnInitialLoad',
},
showFilters: {
title: 'Show active filters?',
type: 'boolean',
default: true,
configPath: 'showFilters',
},
showFacets: {
title: 'Show facets?',
type: 'boolean',
default: true,
configPath: 'showFacets',
},
showClusters: {
title: 'Show tab clusters?',
type: 'boolean',
default: true,
configPath: 'showClusters',
},
showSorting: {
title: 'Show sorting?',
type: 'boolean',
default: true,
configPath: 'showSorting',
},
defaultFilters: {
title: 'Pre-applied filters',
widget: 'object_list',
schema: FilterSchema({ formData }),
schemaExtender: (schema) => schema,
},
availableFacets: {
title: 'Available Facets',
widget: 'array',
choices: [],
},
defaultFacets: {
title: 'Default Facets',
widget: 'array',
choices: [],
},
defaultSort: {
title: 'Default sort',
widget: 'sort_widget',
choices: [],
},
};

if (appConfig) {
const { resultViews } = appConfig;
// console.log(appConfig);

const availableFacets = appConfig.facets?.map(({ field, label }) => [
field,
label,
]);

schema.properties.availableFacets.choices = availableFacets;
schema.properties.defaultFacets.choices = availableFacets;

// 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, registry, appName);
extender.applied = true;
schema.properties.defaultFilters.schemaExtender = extender;
}

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

return schema;
};
SearchResultsView.schemaEnhancer = searchResultsSchemaEnhancer;
Loading

0 comments on commit 0c93c8a

Please sign in to comment.