appbaseio/reactivesearch

View on GitHub
packages/web/src/components/range/SingleRange.js

Summary

Maintainability
D
2 days
Test Coverage
import React, { Component } from 'react';

import {
    addComponent,
    removeComponent,
    watchComponent,
    updateQuery,
    setQueryListener,
} from '@appbaseio/reactivecore/lib/actions';
import {
    isEqual,
    checkValueChange,
    checkPropChange,
    checkSomePropChange,
    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 { UL, Radio } from '../../styles/FormControlList';
import { connect } from '../../utils';

class SingleRange extends Component {
    constructor(props) {
        super(props);

        this.state = {
            currentValue: null,
        };
        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.setValue(this.props.selectedValue);
        } else if (this.props.defaultSelected) {
            this.setValue(this.props.defaultSelected);
        }
    }

    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.setValue(nextProps.defaultSelected);
        } else if (!isEqual(this.state.currentValue, nextProps.selectedValue)) {
            this.setValue(nextProps.selectedValue);
        }
    }

    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) => props.data.find(item => item.label === value) || null;

    static defaultQuery = (value, props) => {
        let query = null;
        if (value) {
            query = {
                range: {
                    [props.dataField]: {
                        gte: value.start,
                        lte: value.end,
                        boost: 2.0,
                    },
                },
            };
        }
        if (query && props.nestedField) {
            return {
                query: {
                    nested: {
                        path: props.nestedField,
                        query,
                    },
                },
            };
        }

        return query;
    };

    setValue = (value, props = this.props) => {
        // ignore state updates when component is locked
        if (props.beforeValueChange && this.locked) {
            return;
        }

        this.locked = true;
        const currentValue = SingleRange.parseValue(value, props);

        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 || SingleRange.defaultQuery;

        props.updateQuery({
            componentId: props.componentId,
            query: query(value, props),
            value,
            label: props.filterLabel,
            showFilter: props.showFilter,
            URLParams: props.URLParams,
            componentType: 'SINGLERANGE',
        });
    };

    handleClick = (e) => {
        this.setValue(e.target.value);
    };

    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>
                )}
                <UL className={getClassName(this.props.innerClass, 'list') || null}>
                    {this.props.data.map((item) => {
                        const selected
                            = !!this.state.currentValue
                            && this.state.currentValue.label === item.label;
                        return (
                            <li key={item.label} className={`${selected ? 'active' : ''}`}>
                                <Radio
                                    className={getClassName(this.props.innerClass, 'radio')}
                                    id={`${this.props.componentId}-${item.label}`}
                                    name={this.props.componentId}
                                    value={item.label}
                                    onChange={this.handleClick}
                                    checked={selected}
                                    show={this.props.showRadio}
                                />
                                <label
                                    className={getClassName(this.props.innerClass, 'label') || null}
                                    htmlFor={`${this.props.componentId}-${item.label}`}
                                >
                                    {item.label}
                                </label>
                            </li>
                        );
                    })}
                </UL>
            </Container>
        );
    }
}

SingleRange.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.string,
    filterLabel: types.string,
    innerClass: types.style,
    onQueryChange: types.func,
    onValueChange: types.func,
    react: types.react,
    showFilter: types.bool,
    showRadio: types.boolRequired,
    style: types.style,
    title: types.title,
    URLParams: types.bool,
    nestedField: types.string,
};

SingleRange.defaultProps = {
    className: null,
    showFilter: true,
    showRadio: true,
    style: {},
    URLParams: false,
};

const mapStateToProps = (state, props) => ({
    selectedValue:
        (state.selectedValues[props.componentId]
            && state.selectedValues[props.componentId].value)
        || null,
});

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,
)(SingleRange);