InstitutePicker.tsx 5.27 KB
Newer Older
1
import * as React from 'react';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
2 3
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
4 5
import { translate } from 'react-i18next';

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
6 7
import {autocompleteWiewsTerm, loadWiewsTerm} from 'vocabulary/actions/public';

8 9 10 11 12 13 14 15 16 17 18 19
import { Field } from 'redux-form';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import Input from '@material-ui/core/Input';
import TextField from '@material-ui/core/TextField';
import FormControl from 'ui/common/forms/FormControl';
import Select from '@material-ui/core/Select';
import Markdown from 'ui/common/markdown';

interface ISelectFaoInstituteInternal extends React.ClassAttributes<any> {
  input: any;
  label: string;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20 21
  loadWiewsTerm: (code) => Promise<any>;
  autocompleteWiewsTerm: (code) => Promise<any>;
22 23 24 25 26 27 28
  meta?: any;
  t: any;
}

class SelectFaoInstituteInternal extends React.Component<ISelectFaoInstituteInternal, any> {

  protected handleInputChange = (event) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
29
    const { autocompleteWiewsTerm } = this.props;
30 31
    this.setState({searchText: event.target.value});
    if (event.target.value) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
32
      autocompleteWiewsTerm(event.target.value).then((suggestions) => {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        this.setState({suggestions});
      });
    }
  }
  protected handleClick = (event) => {
    this.setState({open: true, anchorEl: event.currentTarget});
  }
  protected handleRequestClose = () => {
    this.setState({open: false});
  }
  protected select = (value) => (event) => {
    const {input} = this.props;
    const institute = this.state.suggestions.find((inst) => inst.code === value);
    if (institute) {
      input.onChange({code: institute.code});
      this.setState({
        open: false,
        pickerList: {code: institute.code, title: institute.title},
      });
    } else {
      input.onChange('');
      this.setState({
        open: false,
        pickerList: null,
      });
    }
  }

  private constructor(props, context) {
    super(props, context);
    this.state = {
      disabled: false,
      pickerList: null,
      open: false,
      anchorEl: null,
      searchText: '',
      suggestions: [],
    };
  }

  public componentWillMount() {
    const {input: {value}} = this.props;

    if (value) {
      this.setState({
        pickerList: {
          code:  value.code,
          title: value.fullName || value.code,
        },
        disabled: true,
      });
    }
  }

  public componentWillReceiveProps(nextProps) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
88
    const { loadWiewsTerm } = this.props;
89 90 91 92 93 94 95 96 97
    if (! nextProps.input.value) {
      return this.setState({pickerList: null});
    }
    if (nextProps.input.value !== this.props.input.value) {
      const code = nextProps.input.value.code;
      const institute = this.state.suggestions.find((inst) => inst.code === code);
      if (institute) {
        this.setState({pickerList: {code: institute.code, title: institute.title}});
      } else {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
98
        loadWiewsTerm(code).then((inst) => {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          this.setState({pickerList: {code: inst.code, title: inst.title}});
        });
      }
    }
  }

  public render() {
    const {label, meta, t} = this.props;
    const {pickerList: inputValue, disabled} = this.state;
    return (
      <FormControl fullWidth meta={ meta } label={ label } disabled={ disabled }>
        <Select
          onClick={ !disabled ? this.handleClick : null }
          value={ inputValue }
          input={ <Input/> }
          MenuProps={ {
            open: false,
          } }
        >
          {
            this.state.pickerList ? (
              <MenuItem value={ inputValue }>
                <Markdown basic source={ this.state.pickerList.title }/>
              </MenuItem>) : null
          }
        </Select>
        <Menu
          anchorEl={ this.state.anchorEl }
          open={ this.state.open }
          onClose={ this.handleRequestClose }
        >
          <MenuItem>
            <TextField
              fullWidth
              value={ this.state.searchText }
              label={ t('partners.admin.c.institutePicker.search') }
              onChange={ this.handleInputChange }
            />
          </MenuItem>
          {
            this.state.suggestions.map((inst, index) => (
              <MenuItem onClick={ this.select(inst.code) } key={ `fao-institute-${index}` }>
                <Markdown basic source={ `${inst.code} - ${inst.title}` }/>
              </MenuItem>
            ))
          }
        </Menu>
      </FormControl>
    );
  }
}

interface IFaoInstitutePicker extends React.ClassAttributes<any> {
  name: string;
  label: string;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
154 155
  loadWiewsTerm: (code) => Promise<any>;
  autocompleteWiewsTerm: (code) => Promise<any>;
156 157 158 159 160 161 162
  className?: string;
  t: any;
}

class FaoInstitutePicker extends React.Component<IFaoInstitutePicker, any> {

  public render() {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
163
    const {name, label, className, loadWiewsTerm, autocompleteWiewsTerm, t} = this.props;
164 165 166 167 168 169 170

    return (
      <div className={ className }>
        <Field
          name={ name }
          component={ SelectFaoInstituteInternal }
          label={ label }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
171 172
          loadWiewsTerm={ loadWiewsTerm }
          autocompleteWiewsTerm={ autocompleteWiewsTerm }
173 174 175 176 177 178 179
          t={ t }
        />
      </div>
    );
  }
}

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
180 181 182 183 184 185
const mapDispatchToProps = (dispatch) => bindActionCreators({
  loadWiewsTerm,
  autocompleteWiewsTerm,
}, dispatch);

export default translate()(connect(null, mapDispatchToProps)(FaoInstitutePicker));