Skip to content
Snippets Groups Projects
Commit 50a6695e authored by esikkala's avatar esikkala
Browse files

Simplify facet updates

parent c23d1465
No related branches found
No related tags found
No related merge requests found
...@@ -8,15 +8,12 @@ const ActiveFilters = props => { ...@@ -8,15 +8,12 @@ const ActiveFilters = props => {
<React.Fragment> <React.Fragment>
{Object.keys(uriFilters).map(facetID => { {Object.keys(uriFilters).map(facetID => {
const facetValues = []; const facetValues = [];
Object.entries(uriFilters[facetID]).forEach(([ key, value]) => { Object.values(uriFilters[facetID]).forEach(value => {
facetValues.push({ facetValues.push({
facetID: facetID, facetID: facetID,
facetLabel: facets[facetID].label, facetLabel: facets[facetID].label,
filterType: 'uriFilter', filterType: 'uriFilter',
value: { value: value // a react sortable tree object
id: key,
label: value.length > 18 ? `${value.substring(0, 18)}...` : value,
}
}); });
}); });
return ( return (
......
...@@ -21,10 +21,16 @@ class ChipsArray extends React.Component { ...@@ -21,10 +21,16 @@ class ChipsArray extends React.Component {
facetClass: this.props.facetClass, facetClass: this.props.facetClass,
facetID: data.facetID, facetID: data.facetID,
option: data.filterType, option: data.filterType,
value: data.value value: data.value // a react sortable tree object
}); });
}; };
generateLabel = (facetLabel, valueLabel) => {
return valueLabel.length > 18
? `${facetLabel}: ${valueLabel.substring(0, 18)}...`
: `${facetLabel}: ${valueLabel}`;
}
render() { render() {
const { classes, data } = this.props; const { classes, data } = this.props;
return ( return (
...@@ -33,9 +39,9 @@ class ChipsArray extends React.Component { ...@@ -33,9 +39,9 @@ class ChipsArray extends React.Component {
let icon = null; let icon = null;
return ( return (
<Chip <Chip
key={item.value.id} key={item.value.node.id}
icon={icon} icon={icon}
label={`${item.facetLabel.toLowerCase()}: ${item.value.label}`} label={this.generateLabel(item.facetLabel, item.value.node.prefLabel)}
onDelete={this.handleDelete(item)} onDelete={this.handleDelete(item)}
className={classes.chip} className={classes.chip}
/> />
......
...@@ -2,7 +2,7 @@ import React from 'react'; ...@@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { has } from 'lodash'; import { has } from 'lodash';
import { withStyles } from '@material-ui/core/styles'; import { withStyles } from '@material-ui/core/styles';
import Tree from './Tree'; import HierarchicalFacet from './HierarchicalFacet';
import DateSlider from './slider/DateSlider'; import DateSlider from './slider/DateSlider';
import Paper from '@material-ui/core/Paper'; import Paper from '@material-ui/core/Paper';
import FacetHeader from './FacetHeader'; import FacetHeader from './FacetHeader';
...@@ -60,12 +60,13 @@ class FacetBar extends React.Component { ...@@ -60,12 +60,13 @@ class FacetBar extends React.Component {
render() { render() {
const { classes, facetClass } = this.props; const { classes, facetClass } = this.props;
const { facetUpdateID, updatedFacet, facets } = this.props.facetData; const { facetUpdateID, updatedFacet, updatedFilter, facets } = this.props.facetData;
let uriFilters = {}; let uriFilters = {};
let spatialFilters = {}; let spatialFilters = {};
let activeUriFilters = false; let activeUriFilters = false;
let activeSpatialFilters = false; let activeSpatialFilters = false;
for (const [key, value] of Object.entries(facets)) { for (const [key, value] of Object.entries(facets)) {
//
if (value.uriFilter !== null) { if (value.uriFilter !== null) {
activeUriFilters = true; activeUriFilters = true;
uriFilters[key] = value.uriFilter; uriFilters[key] = value.uriFilter;
...@@ -113,14 +114,16 @@ class FacetBar extends React.Component { ...@@ -113,14 +114,16 @@ class FacetBar extends React.Component {
updateFacetOption={this.props.updateFacetOption} updateFacetOption={this.props.updateFacetOption}
/> />
<div className={classes[facets[id].containerClass]}> <div className={classes[facets[id].containerClass]}>
{facets[id].filterType === 'uriFilter' || facets[id].filterType === 'spatialFilter' ? {facets[id].filterType === 'uriFilter'
<Tree || facets[id].filterType === 'spatialFilter' ?
<HierarchicalFacet
facetID={id} facetID={id}
facet={facets[id]} facet={facets[id]}
facetClass={this.props.facetClass} facetClass={this.props.facetClass}
resultClass={this.props.resultClass} resultClass={this.props.resultClass}
facetUpdateID={facetUpdateID} facetUpdateID={facetUpdateID}
updatedFacet={updatedFacet} updatedFacet={updatedFacet}
updatedFilter={updatedFilter}
fetchFacet={this.props.fetchFacet} fetchFacet={this.props.fetchFacet}
updateFacetOption={this.props.updateFacetOption} updateFacetOption={this.props.updateFacetOption}
/> : /> :
......
...@@ -12,7 +12,6 @@ import IconButton from '@material-ui/core/IconButton'; ...@@ -12,7 +12,6 @@ import IconButton from '@material-ui/core/IconButton';
import NavigateNextIcon from '@material-ui/icons/NavigateNext'; import NavigateNextIcon from '@material-ui/icons/NavigateNext';
import NavigateBeforeIcon from '@material-ui/icons/NavigateBefore'; import NavigateBeforeIcon from '@material-ui/icons/NavigateBefore';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import ChipsArray from './ChipsArray';
const styles = () => ({ const styles = () => ({
facetSearchContainer: { facetSearchContainer: {
...@@ -64,7 +63,7 @@ const styles = () => ({ ...@@ -64,7 +63,7 @@ const styles = () => ({
This component is based on the React Sortable Tree example at: This component is based on the React Sortable Tree example at:
https://frontend-collective.github.io/react-sortable-tree/storybook/?selectedKind=Basics&selectedStory=Search&full=0&addons=0&stories=1&panelRight=0 https://frontend-collective.github.io/react-sortable-tree/storybook/?selectedKind=Basics&selectedStory=Search&full=0&addons=0&stories=1&panelRight=0
*/ */
class Tree extends Component { class HierarchicalFacet extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
...@@ -85,23 +84,32 @@ class Tree extends Component { ...@@ -85,23 +84,32 @@ class Tree extends Component {
} }
componentDidUpdate = prevProps => { componentDidUpdate = prevProps => {
// if (this.props.facetID === 'productionPlace') {
// console.log(this.props.facet.values)
// }
if (prevProps.facet.values != this.props.facet.values) { if (prevProps.facetUpdateID !== this.props.facetUpdateID) {
this.setState({ // update component state if the user modified this facet
treeData: this.props.facet.values if (this.props.updatedFacet === this.props.facetID ) {
}); const treeObj = this.props.updatedFilter;
} const newTreeData = changeNodeAtPath({
if (this.props.updatedFacet !== null treeData: this.state.treeData,
&& this.props.updatedFacet !== this.props.facetID getNodeKey: ({ treeIndex }) => treeIndex,
&& prevProps.facetUpdateID !== this.props.facetUpdateID) { path: treeObj.path,
this.props.fetchFacet({ newNode: {
facetClass: this.props.facetClass, ...treeObj.node,
facetID: this.props.facetID, selected: treeObj.added ? 'true' : 'false'
}); },
});
this.setState({ treeData: newTreeData });
}
// else fetch new values, because some other facet was updated
else {
this.props.fetchFacet({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
});
}
} }
// fetch new values if the user changes the filter type or sort order
if (prevProps.facet.filterType !== this.props.facet.filterType if (prevProps.facet.filterType !== this.props.facet.filterType
&& this.props.facet.filterType === 'uriFilter') { && this.props.facet.filterType === 'uriFilter') {
this.props.fetchFacet({ this.props.fetchFacet({
...@@ -115,27 +123,21 @@ class Tree extends Component { ...@@ -115,27 +123,21 @@ class Tree extends Component {
facetID: this.props.facetID, facetID: this.props.facetID,
}); });
} }
// when values have been fetched, update component's state
if (prevProps.facet.values != this.props.facet.values) {
this.setState({
treeData: this.props.facet.values
});
}
} }
handleCheckboxChange = treeObj => event => { handleCheckboxChange = treeObj => () => {
const newTreeData = changeNodeAtPath({
treeData: this.state.treeData,
getNodeKey: ({ treeIndex }) => treeIndex,
path: treeObj.path,
newNode: {
...treeObj.node,
selected: event.target.checked ? 'true' : 'false'
},
});
this.setState({ treeData: newTreeData });
this.props.updateFacetOption({ this.props.updateFacetOption({
facetClass: this.props.facetClass, facetClass: this.props.facetClass,
facetID: this.props.facetID, facetID: this.props.facetID,
option: this.props.facet.filterType, option: this.props.facet.filterType,
value: { value: treeObj
id: treeObj.node.id,
label: treeObj.node.prefLabel
}
}); });
}; };
...@@ -168,6 +170,15 @@ class Tree extends Component { ...@@ -168,6 +170,15 @@ class Tree extends Component {
generateLabel = node => { generateLabel = node => {
let count = node.totalInstanceCount == null || node.totalInstanceCount == 0 ? node.instanceCount : node.totalInstanceCount; let count = node.totalInstanceCount == null || node.totalInstanceCount == 0 ? node.instanceCount : node.totalInstanceCount;
if (node.noHits === 'true' || Array.isArray(node.noHits)) {
if (Array.isArray(node.instanceCount) ) {
count = Math.min(...node.instanceCount);
} else {
count = 0;
}
}
return ( return (
<React.Fragment> <React.Fragment>
<a <a
...@@ -203,6 +214,10 @@ class Tree extends Component { ...@@ -203,6 +214,10 @@ class Tree extends Component {
const { classes, facet } = this.props; const { classes, facet } = this.props;
const { isFetching, searchField } = facet; const { isFetching, searchField } = facet;
// if (this.props.facetID == 'owner') {
// console.log(this.state.treeData)
// }
// Case insensitive search of `node.title` // Case insensitive search of `node.title`
const customSearchMethod = ({ node, searchQuery }) => { const customSearchMethod = ({ node, searchQuery }) => {
let prefLabel = Array.isArray(node.prefLabel) ? node.prefLabel[0] : node.prefLabel; let prefLabel = Array.isArray(node.prefLabel) ? node.prefLabel[0] : node.prefLabel;
...@@ -226,7 +241,6 @@ class Tree extends Component { ...@@ -226,7 +241,6 @@ class Tree extends Component {
: 0, : 0,
}); });
//<ChipsArray data={this.props.facet.uriFilter} />}
return ( return (
<React.Fragment> <React.Fragment>
{isFetching ? {isFetching ?
...@@ -302,7 +316,7 @@ class Tree extends Component { ...@@ -302,7 +316,7 @@ class Tree extends Component {
} }
} }
Tree.propTypes = { HierarchicalFacet.propTypes = {
classes: PropTypes.object.isRequired, classes: PropTypes.object.isRequired,
facetID: PropTypes.string.isRequired, facetID: PropTypes.string.isRequired,
facet: PropTypes.object.isRequired, facet: PropTypes.object.isRequired,
...@@ -311,7 +325,8 @@ Tree.propTypes = { ...@@ -311,7 +325,8 @@ Tree.propTypes = {
fetchFacet: PropTypes.func, fetchFacet: PropTypes.func,
updateFacetOption: PropTypes.func, updateFacetOption: PropTypes.func,
facetUpdateID: PropTypes.number, facetUpdateID: PropTypes.number,
updatedFilter: PropTypes.object,
updatedFacet: PropTypes.string, updatedFacet: PropTypes.string,
}; };
export default withStyles(styles)(Tree); export default withStyles(styles)(HierarchicalFacet);
...@@ -70,13 +70,16 @@ const updateFacetFilter = (state, action) => { ...@@ -70,13 +70,16 @@ const updateFacetFilter = (state, action) => {
let newFacet = {}; let newFacet = {};
if (oldFacet.filterType === 'uriFilter') { if (oldFacet.filterType === 'uriFilter') {
let newUriFilter = oldFacet.uriFilter == null ? {} : oldFacet.uriFilter; let newUriFilter = oldFacet.uriFilter == null ? {} : oldFacet.uriFilter;
if (has(newUriFilter, value.id)) { // 'value' is a react sortable tree object
delete newUriFilter[value.id]; if (has(newUriFilter, value.node.id)) {
value.added = false;
delete newUriFilter[value.node.id];
if (isEmpty(newUriFilter)) { if (isEmpty(newUriFilter)) {
newUriFilter = null; newUriFilter = null;
} }
} else { } else {
newUriFilter[value.id] = value.label; value.added = true;
newUriFilter[value.node.id] = value;
} }
newFacet = { newFacet = {
...state.facets[facetID], ...state.facets[facetID],
...@@ -92,6 +95,7 @@ const updateFacetFilter = (state, action) => { ...@@ -92,6 +95,7 @@ const updateFacetFilter = (state, action) => {
...state, ...state,
updatedFacet: facetID, updatedFacet: facetID,
facetUpdateID: ++state.facetUpdateID, facetUpdateID: ++state.facetUpdateID,
updatedFilter: value, // a react sortable tree object
facets: { facets: {
...state.facets, ...state.facets,
[ facetID ]: newFacet [ facetID ]: newFacet
......
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
export const INITIAL_STATE = { export const INITIAL_STATE = {
updatedFacet: null, updatedFacet: null,
facetUpdateID: 0, facetUpdateID: 0,
updatedFilter: null,
facets: { facets: {
source: { source: {
id: 'source', id: 'source',
...@@ -68,23 +69,23 @@ export const INITIAL_STATE = { ...@@ -68,23 +69,23 @@ export const INITIAL_STATE = {
// startValue: null, // startValue: null,
// endValue: null // endValue: null
// }, // },
author: { // author: {
id: 'author', // id: 'author',
label: 'Author', // label: 'Author',
// predicate: defined in backend // // predicate: defined in backend
distinctValueCount: 0, // distinctValueCount: 0,
values: [], // values: [],
flatValues: [], // flatValues: [],
sortBy: 'prefLabel', // sortBy: 'prefLabel',
sortDirection: 'asc', // sortDirection: 'asc',
sortButton: true, // sortButton: true,
spatialFilterButton: false, // spatialFilterButton: false,
isFetching: false, // isFetching: false,
searchField: true, // searchField: true,
containerClass: 'ten', // containerClass: 'ten',
filterType: 'uriFilter', // filterType: 'uriFilter',
uriFilter: null // uriFilter: null
}, // },
owner: { owner: {
id: 'owner', id: 'owner',
label: 'Owner', label: 'Owner',
...@@ -125,6 +126,7 @@ const manuscriptsFacets = (state = INITIAL_STATE, action) => { ...@@ -125,6 +126,7 @@ const manuscriptsFacets = (state = INITIAL_STATE, action) => {
case UPDATE_FACET_VALUES: case UPDATE_FACET_VALUES:
return updateFacetValues(state, action); return updateFacetValues(state, action);
case UPDATE_FACET_OPTION: case UPDATE_FACET_OPTION:
// console.log(action)
return updateFacetOption(state, action); return updateFacetOption(state, action);
default: default:
return state; return state;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment