1 import { Component, linkEvent } from "inferno";
2 import { ListingType } from "lemmy-js-client";
3 import { i18n } from "../../i18next";
4 import { UserService } from "../../services";
5 import { randomStr } from "../../utils";
7 interface ListingTypeSelectProps {
10 showSubscribed: boolean;
11 onChange?(val: ListingType): any;
14 interface ListingTypeSelectState {
18 export class ListingTypeSelect extends Component<
19 ListingTypeSelectProps,
20 ListingTypeSelectState
22 private id = `listing-type-input-${randomStr()}`;
24 private emptyState: ListingTypeSelectState = {
25 type_: this.props.type_,
28 constructor(props: any, context: any) {
29 super(props, context);
30 this.state = this.emptyState;
33 static getDerivedStateFromProps(props: any): ListingTypeSelectProps {
36 showLocal: props.showLocal,
37 showSubscribed: props.showSubscribed,
43 <div className="btn-group btn-group-toggle flex-wrap mb-2">
44 {this.props.showSubscribed && (
46 title={i18n.t("subscribed_description")}
47 className={`btn btn-outline-secondary
48 ${this.state.type_ == ListingType.Subscribed && "active"}
49 ${UserService.Instance.myUserInfo.isNone() ? "disabled" : "pointer"}
53 id={`${this.id}-subscribed`}
55 value={ListingType.Subscribed}
56 checked={this.state.type_ == ListingType.Subscribed}
57 onChange={linkEvent(this, this.handleTypeChange)}
58 disabled={UserService.Instance.myUserInfo.isNone()}
60 {i18n.t("subscribed")}
63 {this.props.showLocal && (
65 title={i18n.t("local_description")}
66 className={`pointer btn btn-outline-secondary ${
67 this.state.type_ == ListingType.Local && "active"
71 id={`${this.id}-local`}
73 value={ListingType.Local}
74 checked={this.state.type_ == ListingType.Local}
75 onChange={linkEvent(this, this.handleTypeChange)}
81 title={i18n.t("all_description")}
82 className={`pointer btn btn-outline-secondary ${
83 (this.state.type_ == ListingType.All && "active") ||
84 (!this.props.showLocal &&
85 this.state.type_ == ListingType.Local &&
92 value={ListingType.All}
93 checked={this.state.type_ == ListingType.All}
94 onChange={linkEvent(this, this.handleTypeChange)}
102 handleTypeChange(i: ListingTypeSelect, event: any) {
103 i.props.onChange(event.target.value);