]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/listing-type-select.tsx
Upgrade inferno v8.0.0 try2 (#790)
[lemmy-ui.git] / src / shared / components / common / listing-type-select.tsx
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";
6
7 interface ListingTypeSelectProps {
8   type_: ListingType;
9   showLocal: boolean;
10   showSubscribed: boolean;
11   onChange?(val: ListingType): any;
12 }
13
14 interface ListingTypeSelectState {
15   type_: ListingType;
16 }
17
18 export class ListingTypeSelect extends Component<
19   ListingTypeSelectProps,
20   ListingTypeSelectState
21 > {
22   private id = `listing-type-input-${randomStr()}`;
23
24   private emptyState: ListingTypeSelectState = {
25     type_: this.props.type_,
26   };
27
28   constructor(props: any, context: any) {
29     super(props, context);
30     this.state = this.emptyState;
31   }
32
33   static getDerivedStateFromProps(props: any): ListingTypeSelectProps {
34     return {
35       type_: props.type_,
36       showLocal: props.showLocal,
37       showSubscribed: props.showSubscribed,
38     };
39   }
40
41   render() {
42     return (
43       <div className="btn-group btn-group-toggle flex-wrap mb-2">
44         {this.props.showSubscribed && (
45           <label
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"}
50           `}
51           >
52             <input
53               id={`${this.id}-subscribed`}
54               type="radio"
55               value={ListingType.Subscribed}
56               checked={this.state.type_ == ListingType.Subscribed}
57               onChange={linkEvent(this, this.handleTypeChange)}
58               disabled={UserService.Instance.myUserInfo.isNone()}
59             />
60             {i18n.t("subscribed")}
61           </label>
62         )}
63         {this.props.showLocal && (
64           <label
65             title={i18n.t("local_description")}
66             className={`pointer btn btn-outline-secondary ${
67               this.state.type_ == ListingType.Local && "active"
68             }`}
69           >
70             <input
71               id={`${this.id}-local`}
72               type="radio"
73               value={ListingType.Local}
74               checked={this.state.type_ == ListingType.Local}
75               onChange={linkEvent(this, this.handleTypeChange)}
76             />
77             {i18n.t("local")}
78           </label>
79         )}
80         <label
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 &&
86               "active")
87           }`}
88         >
89           <input
90             id={`${this.id}-all`}
91             type="radio"
92             value={ListingType.All}
93             checked={this.state.type_ == ListingType.All}
94             onChange={linkEvent(this, this.handleTypeChange)}
95           />
96           {i18n.t("all")}
97         </label>
98       </div>
99     );
100   }
101
102   handleTypeChange(i: ListingTypeSelect, event: any) {
103     i.props.onChange(event.target.value);
104   }
105 }