Skip to content

Commit

Permalink
added possibility to configure facets to only be visible for authenti…
Browse files Browse the repository at this point in the history
…cated users
  • Loading branch information
zotya committed Nov 15, 2023
1 parent 0a1740a commit cbbe2ec
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { connect } from 'react-redux';
import { Accordion, Icon } from 'semantic-ui-react';
import { useAtom } from 'jotai';
import { openFacetsAtom } from '../state';
Expand All @@ -7,7 +8,7 @@ import { useAppConfig, useSearchContext } from '@eeacms/search/lib/hocs';
import Facet from '../Facet';

const AccordionFacetWrapper = (props) => {
const { collapsable = true, field, label } = props;
const { collapsable = true, field, label, token } = props;
const searchContext = useSearchContext();
const { facets, filters } = searchContext;

Expand Down Expand Up @@ -43,6 +44,7 @@ const AccordionFacetWrapper = (props) => {
let isOpened = openFacets[field]?.opened || false;
const [counter, setCounter] = React.useState(0);
if (facets[field] === undefined) return null;
if (facet?.authOnly && token === undefined) return null;
return collapsable ? (
<Accordion>
<Accordion.Title
Expand Down Expand Up @@ -98,4 +100,6 @@ const AccordionFacetWrapper = (props) => {
);
};

export default AccordionFacetWrapper;
export default connect((state) => ({
token: state.userSession?.token,
}))(AccordionFacetWrapper);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { connect } from 'react-redux';
import {
useAppConfig,
useProxiedSearchContext,
Expand Down Expand Up @@ -30,6 +31,7 @@ const DropdownFacetWrapper = (props) => {
removeFilter,
sortedOptions,
filterType,
token,
} = props;
const rawSearchContext = useSearchContext();
const {
Expand Down Expand Up @@ -65,6 +67,7 @@ const DropdownFacetWrapper = (props) => {
const { width } = useWindowDimensions();
const isSmallScreen = width < SMALL_SCREEN_SIZE;
if (facets[field] === undefined) return null;
if (facet?.authOnly && token === undefined) return null;

return (
<>
Expand Down Expand Up @@ -197,4 +200,6 @@ const DropdownFacetWrapper = (props) => {
);
};

export default DropdownFacetWrapper;
export default connect((state) => ({
token: state.userSession?.token,
}))(DropdownFacetWrapper);
1 change: 1 addition & 0 deletions src/SearchBlock/SearchBlockView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function SearchBlockView(props) {
const schemaFields = [
'availableFacets',
'defaultFacets',
'authOnlyFacets',
'defaultFilters',
'showClusters',
'defaultSort',
Expand Down
9 changes: 9 additions & 0 deletions src/SearchBlock/templates/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const searchResultsSchemaEnhancer = ({ schema, formData }) => {
'landingPageURL',
'availableFacets',
'defaultFacets',
'authOnlyFacets',
'defaultFilters',
'defaultSort',
],
Expand Down Expand Up @@ -112,6 +113,11 @@ export const searchResultsSchemaEnhancer = ({ schema, formData }) => {
widget: 'array',
choices: [],
},
authOnlyFacets: {
title: 'Facets for authenticated users',
widget: 'array',
choices: [],
},
defaultSort: {
title: 'Default sort',
// widget: 'sort_widget',
Expand All @@ -138,6 +144,9 @@ export const searchResultsSchemaEnhancer = ({ schema, formData }) => {
schema.properties.defaultFacets.choices = availableFacets;
schema.properties.defaultFacets.items = { choices: availableFacets };

schema.properties.authOnlyFacets.choices = availableFacets;
schema.properties.authOnlyFacets.items = { choices: availableFacets };

// fill in defaultResultView choices
schema.properties.defaultResultView = {
...schema.properties.defaultResultView,
Expand Down
6 changes: 6 additions & 0 deletions src/SearchBlock/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const _applyBlockSettings = (config, appName, data, schema) => {
});
}

if (data.authOnlyFacets){
settings.facets.forEach((f) => {
f.authOnly = data.authOnlyFacets.indexOf(f.field) > -1 ? true : false;
});
}

if (data.defaultFilters) {
const filters = data.defaultFilters
.map((f) => ({
Expand Down

0 comments on commit cbbe2ec

Please sign in to comment.