From c5360db65f78c2d9dbc8c9db0ad3e283a3d62bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Sat, 26 Dec 2020 12:38:06 +0100 Subject: [PATCH] Update FacetedSearch.jsx --- src/components/Views/FacetedSearch.jsx | 369 ++++++++++++++++++++++++- 1 file changed, 365 insertions(+), 4 deletions(-) diff --git a/src/components/Views/FacetedSearch.jsx b/src/components/Views/FacetedSearch.jsx index d3b3e186..b3c7cb79 100644 --- a/src/components/Views/FacetedSearch.jsx +++ b/src/components/Views/FacetedSearch.jsx @@ -1,9 +1,370 @@ -import React, { useState } from 'react'; +import React, { Component, useEffect } from 'react'; -const FacetedSearch = ({ data }) => { - // const [activeIndex, setActiveIndex] = useState(new Set()); +import { OverridableContext } from 'react-overridable'; +import { Link } from 'react-router-dom'; +import _truncate from 'lodash/truncate'; +import { + Button, + Card, + Container, + Dropdown, + Grid, + Item, + List, + Label, + Menu, + Segment, +} from 'semantic-ui-react'; +import { + BucketAggregation, + EmptyResults, + Error, + ReactSearchKit, + ResultsLoader, + withState, +} from 'react-searchkit'; - return
I am a Faceted Search component
; +import { ESSearchApi } from './Searchkit/ESSearchApi'; +import { IGIBESRequestSerializer } from './Searchkit/IGIBESRequestSerializer'; +import { Results } from './Searchkit/Results'; + +import { settings } from '~/config'; +import { NoSSR } from './helpers'; + +import { useDispatch, useSelector } from 'react-redux'; + +const OnResults = withState(Results); + +const customAggComp = (title, containerCmp) => { + return containerCmp ? ( + + + {title} + {containerCmp} + + + ) : null; +}; + +const customAggValuesContainerCmp = (valuesCmp) => ( + {valuesCmp} +); + +const customAggValueCmp = ( + bucket, + isSelected, + onFilterClicked, + getChildAggCmps, +) => { + const childAggCmps = getChildAggCmps(bucket); + return ( + onFilterClicked(bucket.key)} + > + + {bucket.key} + {childAggCmps} + + ); +}; + +// class Tags extends Component { +// onClick = (event, value) => { +// window.history.push({ +// search: `${window.location.search}&f=tags_agg:${value}`, +// }); +// event.preventDefault(); +// }; + +// render() { +// if (!this.props.tags) return null; +// return this.props.tags?.map((tag, index) => ( +// +// )); +// } +// } + +const IGIBResultsListItem = ({ result, index }) => { + return ( + + + + {result.title} + + + {_truncate(result.description, { length: 200 })} + + + {result.kompasscomponent?.map((item) => { + let tito = item.title || item.token; + return ( + + ); + })} + {result.targetaudience?.map((item) => { + let tito_foo = item.title || item.token; + return ( + + ); + })} + {result.organisationunit?.map((item) => { + let tito = item.title || item.token; + return ( + + ); + })} + {result.informationtype?.map((item) => { + let tito = item.title || item.token; + return ( + + ); + })} + +
+ {result.freemanualtags?.map((item) => { + let tito = item; + return ( + + ); + })} +
+
+
+
+ ); +}; + +const myCountElement = ({ totalResults }) =>
{totalResults} Treffer
; + +// One Filter of Faceted Navigation +const customBucketAggregationElement = (props) => { + // console.debug('IGIBBucketAggregationElement', props); + const { title, containerCmp } = props; + const selectedFilters = containerCmp.props.selectedFilters.map((el) => el[1]); + // console.debug(selectedFilters); + return ( + containerCmp && ( + // + // + // + // {selectedFilters.length ? selectedFilters.join(' ') : title} + // + // + // {containerCmp} + // + + + + {containerCmp} + {/* + + + + + + */} + + + ) + ); +}; + +const customBucketAggregationContainerElement = ({ valuesCmp }) => ( + <>{valuesCmp} +); + +const customBucketAggregationValuesElement = (props) => { + const { + bucket, + isSelected, + onFilterClicked, + getChildAggCmps, + keyField, + } = props; + const label = bucket.label + ? bucket.label + : `${keyField} (${bucket.doc_count})`; + const childAggCmps = getChildAggCmps(bucket); + return ( + + {/* onFilterClicked(bucket.key)} + checked={isSelected} + /> */} + onFilterClicked(bucket.key)}>{label} + {childAggCmps} + + ); +}; + +const overriddenComponents = { + 'BucketAggregation.element': customBucketAggregationElement, + 'BucketAggregationContainer.element': customBucketAggregationContainerElement, + 'BucketAggregationValues.element': customBucketAggregationValuesElement, + 'ResultsList.item.elasticsearch': IGIBResultsListItem, + 'Count.element': myCountElement, +}; + +const initialState = { + queryString: '', + layout: 'list', + page: 1, + size: 10, +}; + +const FacetedSearch = ({ data, location }) => { + const { + search_url = data.elastic_search_api_url || 'http://localhost:9200', + search_index = data.elastic_search_api_index || 'plone2020', + } = data; + + const dispatch = useDispatch(); + + const searchApi = new ESSearchApi({ + axios: { + // url: 'http://localhost:9200/plone2020/_search', + url: search_url + '/' + search_index + '/_search', + timeout: 5000, + headers: { Accept: 'application/json' }, + }, + es: { + requestSerializer: IGIBESRequestSerializer, + }, + }); + + useEffect(() => { + // TODO set value of input field + let searchParams = new URLSearchParams(location?.search); + let q = searchParams.get('q'); + console.debug('FNView useEffect: querystring of location', q, location); + }, [location, dispatch]); + + return ( + + +

+ DEBUG: url {search_url} and index {search_index} +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ ); }; export default FacetedSearch;