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): In search block on edit, the sort on and sort order are not working #205

Merged
merged 2 commits into from
Mar 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//this customization is used for fixing: in the search block, on edit sort on and reversed order doesn't work.
//See here volto pr: https://github.com/plone/volto/pull/5262
import React, { useEffect } from 'react';
import { defineMessages } from 'react-intl';
import { compose } from 'redux';

import { SidebarPortal, BlockDataForm } from '@plone/volto/components';
import { addExtensionFieldToSchema } from '@plone/volto/helpers/Extensions/withBlockSchemaEnhancer';
import { getBaseUrl } from '@plone/volto/helpers';
import config from '@plone/volto/registry';

import { SearchBlockViewComponent } from '@plone/volto/components/manage/Blocks/Search/SearchBlockView';
import Schema from '@plone/volto/components/manage/Blocks/Search/schema';
import {
withSearch,
withQueryString,
} from '@plone/volto/components/manage/Blocks/Search/hocs';
import { cloneDeep } from 'lodash';

const messages = defineMessages({
template: {
id: 'Results template',
defaultMessage: 'Results template',
},
});

const SearchBlockEdit = (props) => {
const {
block,
onChangeBlock,
data,
selected,
intl,
navRoot,
contentType,
onTriggerSearch,
querystring = {},
} = props;
const { sortable_indexes = {} } = querystring;

let schema = Schema({ data, intl });

schema = addExtensionFieldToSchema({
schema,
name: 'listingBodyTemplate',
items: config.blocks.blocksConfig.listing.variations,
intl,
title: { id: intl.formatMessage(messages.template) },
});
const listingVariations = config.blocks.blocksConfig?.listing?.variations;
let activeItem = listingVariations.find(
(item) => item.id === data.listingBodyTemplate,
);
const listingSchemaEnhancer = activeItem?.schemaEnhancer;
if (listingSchemaEnhancer)
schema = listingSchemaEnhancer({
schema: cloneDeep(schema),
data,
intl,
});
schema.properties.sortOnOptions.items = {
choices: Object.keys(sortable_indexes).map((k) => [
k,
sortable_indexes[k].title,
]),
};

const { query = {} } = data || {};
// We don't need deep compare here, as this is just json serializable data.
const deepQuery = JSON.stringify(query);
useEffect(() => {
onTriggerSearch(
'',
data?.facets,
data?.query?.sort_on,
data?.query?.sort_order,
);
}, [deepQuery, onTriggerSearch, data]);

return (
<>
<SearchBlockViewComponent
{...props}
path={getBaseUrl(props.pathname)}
mode="edit"
/>
<SidebarPortal selected={selected}>
<BlockDataForm
schema={schema}
onChangeField={(id, value) => {
onChangeBlock(block, {
...data,
[id]: value,
});
}}
onChangeBlock={onChangeBlock}
formData={data}
navRoot={navRoot}
contentType={contentType}
/>
</SidebarPortal>
</>
);
};

export default compose(withQueryString, withSearch())(SearchBlockEdit);
Loading