packages/web/src/components/range/MultiDropdownRange.js
import React, { Component } from 'react';
import {
addComponent,
removeComponent,
watchComponent,
updateQuery,
setQueryListener,
} from '@appbaseio/reactivecore/lib/actions';
import {
isEqual,
checkValueChange,
checkSomePropChange,
checkPropChange,
getClassName,
} from '@appbaseio/reactivecore/lib/utils/helper';
import types from '@appbaseio/reactivecore/lib/utils/types';
import Title from '../../styles/Title';
import Container from '../../styles/Container';
import Dropdown from '../shared/Dropdown';
import { connect } from '../../utils';
class MultiDropdownRange extends Component {
constructor(props) {
super(props);
this.state = {
currentValue: [],
};
// selectedValues hold the selected items as keys for O(1) complexity
this.selectedValues = {};
this.type = 'range';
this.locked = false;
props.setQueryListener(props.componentId, props.onQueryChange, null);
}
componentWillMount() {
this.props.addComponent(this.props.componentId);
this.setReact(this.props);
if (this.props.selectedValue) {
this.selectItem(this.props.selectedValue, true);
} else if (this.props.defaultSelected) {
this.selectItem(this.props.defaultSelected, true);
}
}
componentWillReceiveProps(nextProps) {
checkPropChange(this.props.react, nextProps.react, () => this.setReact(nextProps));
checkSomePropChange(this.props, nextProps, ['dataField', 'nestedField'], () => {
this.updateQuery(this.state.currentValue, nextProps);
});
if (!isEqual(this.props.defaultSelected, nextProps.defaultSelected)) {
this.selectItem(nextProps.defaultSelected, true);
} else if (
!isEqual(this.state.currentValue, nextProps.selectedValue)
&& (nextProps.selectedValue || nextProps.selectedValue === null)
) {
this.selectItem(nextProps.selectedValue, true);
}
}
componentWillUnmount() {
this.props.removeComponent(this.props.componentId);
}
setReact(props) {
if (props.react) {
props.watchComponent(props.componentId, props.react);
}
}
// parses range label to get start and end
static parseValue = (value, props) =>
(value ? props.data.filter(item => value.includes(item.label)) : null);
static defaultQuery = (values, props) => {
const generateRangeQuery = (dataField, items) => {
if (items.length > 0) {
return items.map(value => ({
range: {
[dataField]: {
gte: value.start,
lte: value.end,
boost: 2.0,
},
},
}));
}
return null;
};
let query = null;
if (values && values.length) {
query = {
bool: {
should: generateRangeQuery(props.dataField, values),
minimum_should_match: 1,
boost: 1.0,
},
};
}
if (query && props.nestedField) {
return {
query: {
nested: {
path: props.nestedField,
query,
},
},
};
}
return query;
};
selectItem = (item, isDefaultValue = false, props = this.props) => {
// ignore state updates when component is locked
if (props.beforeValueChange && this.locked) {
return;
}
this.locked = true;
let { currentValue } = this.state;
if (!item) {
currentValue = [];
this.selectedValues = {};
} else if (isDefaultValue) {
// checking if the items in defaultSeleted exist in the data prop
currentValue = MultiDropdownRange.parseValue(item, props);
currentValue.forEach((value) => {
this.selectedValues = {
...this.selectedValues,
[value.label]: true,
};
});
} else if (this.selectedValues[item.label]) {
currentValue = currentValue.filter(value => value.label !== item.label);
const { [item.label]: del, ...selectedValues } = this.selectedValues;
this.selectedValues = selectedValues;
} else {
currentValue = [...currentValue, item];
this.selectedValues = {
...this.selectedValues,
[item.label]: true,
};
}
const performUpdate = () => {
this.setState(
{
currentValue,
},
() => {
this.updateQuery(currentValue, props);
this.locked = false;
if (props.onValueChange) props.onValueChange(currentValue);
},
);
};
checkValueChange(props.componentId, currentValue, props.beforeValueChange, performUpdate);
};
updateQuery = (value, props) => {
const query = props.customQuery || MultiDropdownRange.defaultQuery;
props.updateQuery({
componentId: props.componentId,
query: query(value, props),
value,
label: props.filterLabel,
showFilter: props.showFilter,
URLParams: props.URLParams,
componentType: 'MULTIDROPDOWNRANGE',
});
};
render() {
return (
<Container style={this.props.style} className={this.props.className}>
{this.props.title && (
<Title className={getClassName(this.props.innerClass, 'title') || null}>
{this.props.title}
</Title>
)}
<Dropdown
innerClass={this.props.innerClass}
items={this.props.data}
onChange={this.selectItem}
selectedItem={this.state.currentValue}
placeholder={this.props.placeholder}
keyField="label"
multi
returnsObject
themePreset={this.props.themePreset}
/>
</Container>
);
}
}
MultiDropdownRange.propTypes = {
addComponent: types.funcRequired,
removeComponent: types.funcRequired,
setQueryListener: types.funcRequired,
updateQuery: types.funcRequired,
watchComponent: types.funcRequired,
selectedValue: types.selectedValue,
// component props
beforeValueChange: types.func,
className: types.string,
componentId: types.stringRequired,
customQuery: types.func,
data: types.data,
dataField: types.stringRequired,
defaultSelected: types.stringArray,
filterLabel: types.filterLabel,
innerClass: types.style,
onQueryChange: types.func,
nestedField: types.string,
onValueChange: types.func,
placeholder: types.string,
react: types.react,
showFilter: types.bool,
style: types.style,
title: types.title,
themePreset: types.themePreset,
URLParams: types.bool,
};
MultiDropdownRange.defaultProps = {
className: null,
placeholder: 'Select a value',
showFilter: true,
style: {},
URLParams: false,
};
const mapStateToProps = (state, props) => ({
selectedValue: state.selectedValues[props.componentId]
? state.selectedValues[props.componentId].value
: null,
themePreset: state.config.themePreset,
});
const mapDispatchtoProps = dispatch => ({
addComponent: component => dispatch(addComponent(component)),
removeComponent: component => dispatch(removeComponent(component)),
updateQuery: updateQueryObject => dispatch(updateQuery(updateQueryObject)),
watchComponent: (component, react) => dispatch(watchComponent(component, react)),
setQueryListener: (component, onQueryChange, beforeQueryChange) =>
dispatch(setQueryListener(component, onQueryChange, beforeQueryChange)),
});
export default connect(
mapStateToProps,
mapDispatchtoProps,
)(MultiDropdownRange);