CropFilter.tsx 1.08 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1 2 3
import * as React from 'react';
import { connect } from 'react-redux';

4
import Crop from 'model/genesys/Crop';
Matija Obreza's avatar
Matija Obreza committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

import StringArrFilter from 'ui/common/filter/StringArrFilter';

interface IProps extends React.ClassAttributes<any> {
  crops: Crop[];
}

class CropFilter extends React.Component<IProps, any> {
  private options: object = {};

  public componentWillMount() {
    const { crops } = this.props;
    if (crops && crops.length > 0) {
      this.options = {};
      crops.sort((a, b) => a.name.localeCompare(b.name)).forEach((crop) => {
        this.options[crop.shortName] = crop.name;
      });
    }
  }

  public componentWillReceiveProps(nextProps) {
    const { crops } = nextProps;
    if (crops && crops.length > 0) {
      this.options = {};
      crops.sort((a, b) => a.name.localeCompare(b.name)).forEach((crop) => {
        this.options[crop.shortName] = crop.name;
      });
    }
  }

  public render() {
    return (
      <StringArrFilter name="crop" options={ this.options } />
    );
  }
}

export default connect((state) => ({
Matija Obreza's avatar
Matija Obreza committed
43
  crops: state.crop.public.list,
Matija Obreza's avatar
Matija Obreza committed
44
}), null)(CropFilter);