1 import { Component, linkEvent } from 'inferno';
2 import { ListingType } from 'lemmy-js-client';
3 import { UserService } from '../services';
4 import { randomStr } from '../utils';
5 import { i18n } from '../i18next';
7 interface ListingTypeSelectProps {
10 onChange?(val: ListingType): any;
13 interface ListingTypeSelectState {
17 export class ListingTypeSelect extends Component<
18 ListingTypeSelectProps,
19 ListingTypeSelectState
21 private id = `listing-type-input-${randomStr()}`;
23 private emptyState: ListingTypeSelectState = {
24 type_: this.props.type_,
27 constructor(props: any, context: any) {
28 super(props, context);
29 this.state = this.emptyState;
32 static getDerivedStateFromProps(props: any): ListingTypeSelectProps {
35 showLocal: props.showLocal,
41 <div class="btn-group btn-group-toggle flex-wrap mb-2">
43 className={`btn btn-outline-secondary
44 ${this.state.type_ == ListingType.Subscribed && 'active'}
45 ${UserService.Instance.user == undefined ? 'disabled' : 'pointer'}
49 id={`${this.id}-subscribed`}
51 value={ListingType.Subscribed}
52 checked={this.state.type_ == ListingType.Subscribed}
53 onChange={linkEvent(this, this.handleTypeChange)}
54 disabled={UserService.Instance.user == undefined}
56 {i18n.t('subscribed')}
58 {this.props.showLocal && (
60 className={`pointer btn btn-outline-secondary ${
61 this.state.type_ == ListingType.Local && 'active'
65 id={`${this.id}-local`}
67 value={ListingType.Local}
68 checked={this.state.type_ == ListingType.Local}
69 onChange={linkEvent(this, this.handleTypeChange)}
75 className={`pointer btn btn-outline-secondary ${
76 this.state.type_ == ListingType.All && 'active'
82 value={ListingType.All}
83 checked={this.state.type_ == ListingType.All}
84 onChange={linkEvent(this, this.handleTypeChange)}
92 handleTypeChange(i: ListingTypeSelect, event: any) {
93 i.props.onChange(event.target.value);