Skip to content

Commit

Permalink
Catch error on non existing subjects field name.
Browse files Browse the repository at this point in the history
Array.isArray(result[subjectsFieldname])
  • Loading branch information
ksuess committed Jan 4, 2023
1 parent ec1a3bb commit b02a79f
Showing 1 changed file with 95 additions and 99 deletions.
194 changes: 95 additions & 99 deletions src/components/Views/FacetedSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ import { CustomESRequestSerializer } from '../Searchkit/CustomESRequestSerialize
import { CustomESResponseSerializer } from '../Searchkit/CustomESResponseSerializer';
import { Results } from '../Searchkit/Results';

import { flattenESUrlToPath, getObjectFromObjectList, scrollToTarget } from '../helpers';
import {
flattenESUrlToPath,
getObjectFromObjectList,
scrollToTarget,
} from '../helpers';
import { ElasticSearchHighlights } from './ElasticSearchHighlights';
import StateLogger from '../StateLogger';

import './less/springisnow-volto-searchkit-block.less';


// TODO Make reviewstatemapping configurable
export const ploneSearchApi = (data) => {
return new PloneSearchApi({
Expand Down Expand Up @@ -89,25 +92,30 @@ const MyResults = (props) => {

const OnResults = withState(MyResults);


class _ExtraInfo extends React.Component {
render() {
const {result} = this.props;
const extrainfo_fields = getObjectFromObjectList(this.props.currentQueryState.data.extrainfo_fields);
const facet_fields = getObjectFromObjectList(this.props.currentQueryState.data.facet_fields);
let subjectsFieldname = this.props.currentQueryState.data.subjectsFieldname; // "subjects";
const { result } = this.props;
const extrainfo_fields = getObjectFromObjectList(
this.props.currentQueryState.data.extrainfo_fields,
);
const facet_fields = getObjectFromObjectList(
this.props.currentQueryState.data.facet_fields,
);
let subjectsFieldname = this.props.currentQueryState.data?.subjectsFieldname; // "subjects";
return (
<Item.Extra>
{Object.keys(extrainfo_fields).map((extrainfo_key, idx) => {
if (! result[extrainfo_key]) {
console.debug("not indexed:", extrainfo_key)
return
if (!result[extrainfo_key]) {
console.debug('not indexed:', extrainfo_key);
return;
}
const extrainfo_value = Array.isArray(result[extrainfo_key]) ? result[extrainfo_key] : [result[extrainfo_key]];
const extrainfo_value = Array.isArray(result[extrainfo_key])
? result[extrainfo_key]
: [result[extrainfo_key]];

return Object.keys(facet_fields).includes(extrainfo_key) ? (
<React.Fragment key={extrainfo_key}>
<span className='label'>{extrainfo_fields[extrainfo_key]}:</span>
<span className="label">{extrainfo_fields[extrainfo_key]}:</span>
{extrainfo_value?.map((item, index) => {
let tito = item.title || item.token;
let payloadOfFilter = {
Expand All @@ -117,7 +125,12 @@ class _ExtraInfo extends React.Component {
layout: 'list',
page: 1,
size: 10,
filters: [[`${extrainfo_key}_agg.inner.${extrainfo_key}_token`, item.token]],
filters: [
[
`${extrainfo_key}_agg.inner.${extrainfo_key}_token`,
item.token,
],
],
},
};
return (
Expand All @@ -137,9 +150,8 @@ class _ExtraInfo extends React.Component {
)}
</React.Fragment>
) : (

<React.Fragment key={extrainfo_key}>
<span className='label'>{extrainfo_fields[extrainfo_key]}:</span>
<span className="label">{extrainfo_fields[extrainfo_key]}:</span>
{extrainfo_value?.map((item, index) => {
let tito = item.title || item.token || item;
return (
Expand All @@ -156,13 +168,11 @@ class _ExtraInfo extends React.Component {
);
})}

{result[subjectsFieldname]?.length > 0 ? (
{Array.isArray(result[subjectsFieldname]) &&
result[subjectsFieldname]?.length > 0 ? (
<div className="metadata-tags">
<span className='label'>
<FormattedMessage
id="Tags"
defaultMessage="Tags"
/>:
<span className="label">
<FormattedMessage id="Tags" defaultMessage="Tags" />:
</span>
{result[subjectsFieldname]?.map((item, index) => {
let tito = item;
Expand Down Expand Up @@ -195,7 +205,7 @@ class _ExtraInfo extends React.Component {
</div>
) : null}
</Item.Extra>
)
);
}
}

Expand Down Expand Up @@ -256,9 +266,7 @@ const CustomResultsListItem = (props) => {
{truncate(result.description, { length: 200 })}
</Link>
</Item.Description>
<ExtraInfo
result={result}
/>
<ExtraInfo result={result} />
<ElasticSearchHighlights
highlight={result.highlight}
indexResult={index}
Expand Down Expand Up @@ -369,12 +377,7 @@ const customBucketAggregationContainerElement = ({ valuesCmp }) => {
};

const customBucketAggregationValuesElement = (props) => {
const {
bucket,
isSelected,
onFilterClicked,
keyField,
} = props;
const { bucket, keyField, isSelected, onFilterClicked, childAggCmps } = props;
const label = bucket.label
? `${bucket.label} (${bucket.doc_count})`
: `${keyField} (${bucket.doc_count})`;
Expand All @@ -386,7 +389,7 @@ const customBucketAggregationValuesElement = (props) => {
>
{label}
</Item>

{childAggCmps}
</Dropdown.Item>
);
};
Expand All @@ -396,12 +399,9 @@ const customEmpytResultsElement = (props) => {
return (
<Segment>
<Header icon>
<FormattedMessage
id="No results"
defaultMessage="No results"
/>
<FormattedMessage id="No results" defaultMessage="No results" />
</Header>
<Button
<Button
onClick={() => {
resetQuery();
}}
Expand Down Expand Up @@ -472,53 +472,53 @@ const customPaginationElement = (props) => {

return pages > 1 ? (
<Paginator
activePage={currentPage}
totalPages={pages}
onPageChange={_onPageChange}
boundaryRange={boundaryRangeCount}
siblingRange={siblingRangeCount}
ellipsisItem={
showEllipsis
? {
content: <IconSemantic name="ellipsis horizontal" />,
icon: true,
}
: null
}
firstItem={
showFirst
? {
content: <Icon name={firstAngle} size="20px" />,
icon: true,
}
: null
}
lastItem={
showLast
? {
content: <Icon name={lastAngle} size="20px" />,
icon: true,
}
: null
}
prevItem={
showPrev
? {
content: <Icon name={leftAngle} size="20px" />,
icon: true,
}
: null
}
nextItem={
showNext
? {
content: <Icon name={rightAngle} size="20px" />,
icon: true,
}
: null
}
size={size}
/>
activePage={currentPage}
totalPages={pages}
onPageChange={_onPageChange}
boundaryRange={boundaryRangeCount}
siblingRange={siblingRangeCount}
ellipsisItem={
showEllipsis
? {
content: <IconSemantic name="ellipsis horizontal" />,
icon: true,
}
: null
}
firstItem={
showFirst
? {
content: <Icon name={firstAngle} size="20px" />,
icon: true,
}
: null
}
lastItem={
showLast
? {
content: <Icon name={lastAngle} size="20px" />,
icon: true,
}
: null
}
prevItem={
showPrev
? {
content: <Icon name={leftAngle} size="20px" />,
icon: true,
}
: null
}
nextItem={
showNext
? {
content: <Icon name={rightAngle} size="20px" />,
icon: true,
}
: null
}
size={size}
/>
) : null;
};

Expand Down Expand Up @@ -572,10 +572,7 @@ const initialState = {
* @param {object} overriddenComponents Override with custom components, ignore to stay with default 'dropdown' or step back to react-searchkit default components with value {}
* @returns
*/
const FacetedSearch = ({
data,
overriddenComponents,
}) => {
const FacetedSearch = ({ data, overriddenComponents }) => {
const { facet_fields, relocation, filterLayout } = data;
const facet_fields_object = getObjectFromObjectList(facet_fields);
const token = useSelector((state) => state.userSession?.token);
Expand Down Expand Up @@ -612,7 +609,7 @@ const FacetedSearch = ({
<ReactSearchKit
searchApi={ploneSearchApi(data)}
eventListenerEnabled={true}
initialQueryState={{...initialState, data: data}}
initialQueryState={{ ...initialState, data: data }}
>
<Container>
{typeof document !== 'undefined' && relocation?.length > 0 ? (
Expand All @@ -633,7 +630,7 @@ const FacetedSearch = ({
className: 'searchbarinput',
}}
actionProps={{
content: intl.formatMessage(messages.search)
content: intl.formatMessage(messages.search),
}}
/>
<IconSemantic
Expand All @@ -655,7 +652,7 @@ const FacetedSearch = ({
iconPosition: 'left',
}}
actionProps={{
content: intl.formatMessage(messages.search)
content: intl.formatMessage(messages.search),
}}
/>
<IconSemantic
Expand All @@ -677,14 +674,13 @@ const FacetedSearch = ({
className={'facetedsearch_filter ' + filterLayout}
>
{Object.keys(facet_fields_object)?.map((facet) => (
<BucketAggregation
title={facet_fields_object[facet]}
agg={{
field: facet,
aggName:
`${facet}_agg.inner.${facet}_token`,
}}
/>
<BucketAggregation
title={facet_fields_object[facet]}
agg={{
field: facet,
aggName: `${facet}_agg.inner.${facet}_token`,
}}
/>
))}
</Grid.Column>
</Grid.Row>
Expand Down

0 comments on commit b02a79f

Please sign in to comment.