summaryrefslogtreecommitdiffstats
path: root/ui/src/components/data-type-select.tsx
blob: d16c785d8bafeac01c3446ded7ffb598f18956a0 (plain)
1
2
3
4
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
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
import { Component, linkEvent } from 'inferno';
import { DataType } from '../interfaces';

import { i18n } from '../i18next';

interface DataTypeSelectProps {
  type_: DataType;
  onChange?(val: DataType): any;
}

interface DataTypeSelectState {
  type_: DataType;
}

export class DataTypeSelect extends Component<
  DataTypeSelectProps,
  DataTypeSelectState
> {
  private emptyState: DataTypeSelectState = {
    type_: this.props.type_,
  };

  constructor(props: any, context: any) {
    super(props, context);
    this.state = this.emptyState;
  }

  static getDerivedStateFromProps(props: any): DataTypeSelectProps {
    return {
      type_: props.type_,
    };
  }

  render() {
    return (
      <div class="btn-group btn-group-toggle">
        <label
          className={`pointer btn btn-sm btn-secondary 
            ${this.state.type_ == DataType.Post && 'active'}
          `}
        >
          <input
            type="radio"
            value={DataType.Post}
            checked={this.state.type_ == DataType.Post}
            onChange={linkEvent(this, this.handleTypeChange)}
          />
          {i18n.t('posts')}
        </label>
        <label
          className={`pointer btn btn-sm btn-secondary ${
            this.state.type_ == DataType.Comment && 'active'
          }`}
        >
          <input
            type="radio"
            value={DataType.Comment}
            checked={this.state.type_ == DataType.Comment}
            onChange={linkEvent(this, this.handleTypeChange)}
          />
          {i18n.t('comments')}
        </label>
      </div>
    );
  }

  handleTypeChange(i: DataTypeSelect, event: any) {
    i.props.onChange(Number(event.target.value));
  }
}