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 class="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"}
50 UserService.Instance.myUserInfo == undefined
57 id={`${this.id}-subscribed`}
59 value={ListingType.Subscribed}
60 checked={this.state.type_ == ListingType.Subscribed}
61 onChange={linkEvent(this, this.handleTypeChange)}
62 disabled={UserService.Instance.myUserInfo == undefined}
64 {i18n.t("subscribed")}
67 {this.props.showLocal && (
69 title={i18n.t("local_description")}
70 className={`pointer btn btn-outline-secondary ${
71 this.state.type_ == ListingType.Local && "active"
75 id={`${this.id}-local`}
77 value={ListingType.Local}
78 checked={this.state.type_ == ListingType.Local}
79 onChange={linkEvent(this, this.handleTypeChange)}
85 title={i18n.t("all_description")}
86 className={`pointer btn btn-outline-secondary ${
87 (this.state.type_ == ListingType.All && "active") ||
88 (!this.props.showLocal &&
89 this.state.type_ == ListingType.Local &&
96 value={ListingType.All}
97 checked={this.state.type_ == ListingType.All}
98 onChange={linkEvent(this, this.handleTypeChange)}
106 handleTypeChange(i: ListingTypeSelect, event: any) {
107 i.props.onChange(event.target.value);