]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/listing-type-select.tsx
component classes v2
[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): void;
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   state: ListingTypeSelectState = {
25     type_: this.props.type_,
26   };
27
28   constructor(props: any, context: any) {
29     super(props, context);
30   }
31
32   static getDerivedStateFromProps(
33     props: ListingTypeSelectProps
34   ): ListingTypeSelectState {
35     return {
36       type_: props.type_,
37     };
38   }
39
40   render() {
41     return (
42       <div className="listing-type-select btn-group btn-group-toggle flex-wrap">
43         {this.props.showSubscribed && (
44           <label
45             title={i18n.t("subscribed_description")}
46             className={`btn btn-outline-secondary 
47             ${this.state.type_ == "Subscribed" && "active"}
48             ${!UserService.Instance.myUserInfo ? "disabled" : "pointer"}
49           `}
50           >
51             <input
52               id={`${this.id}-subscribed`}
53               type="radio"
54               className="btn-check"
55               value={"Subscribed"}
56               checked={this.state.type_ == "Subscribed"}
57               onChange={linkEvent(this, this.handleTypeChange)}
58               disabled={!UserService.Instance.myUserInfo}
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_ == "Local" && "active"
68             }`}
69           >
70             <input
71               id={`${this.id}-local`}
72               type="radio"
73               className="btn-check"
74               value={"Local"}
75               checked={this.state.type_ == "Local"}
76               onChange={linkEvent(this, this.handleTypeChange)}
77             />
78             {i18n.t("local")}
79           </label>
80         )}
81         <label
82           title={i18n.t("all_description")}
83           className={`pointer btn btn-outline-secondary ${
84             (this.state.type_ == "All" && "active") ||
85             (!this.props.showLocal && this.state.type_ == "Local" && "active")
86           }`}
87         >
88           <input
89             id={`${this.id}-all`}
90             type="radio"
91             className="btn-check"
92             value={"All"}
93             checked={this.state.type_ == "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 }