import React from 'react';
import PropTypes from 'prop-types';

import {CommonListSorter} from './common/CommonListSorter';

export class PureListSorter extends React.Component {
    static propTypes = {
        currentSortingDirection: PropTypes.string.isRequired,
        onChangeSorting: PropTypes.func.isRequired,
        property: PropTypes.string.isRequired,
        currentSortingProperty: PropTypes.string.isRequired
    };

    handleChangeSorting = (property, direction) => {
        this.props.onChangeSorting(property, direction);
    };

    getDirection() {
        const {currentSortingProperty, currentSortingDirection, property} = this.props;

        if (currentSortingProperty !== property) {
            return null;
        }

        return currentSortingDirection;
    }

    render() {
        const {currentSortingProperty, currentSortingDirection, ...props} = this.props;

        return (
            <CommonListSorter
                {...props}
                onChangeSorting={this.handleChangeSorting}
                direction={this.getDirection()}
            />
        );
    }
}
