]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/listing-type-select.tsx
Change from using Link to NavLink. resolve #269
[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   onChange?(val: ListingType): any;
11 }
12
13 interface ListingTypeSelectState {
14   type_: ListingType;
15 }
16
17 export class ListingTypeSelect extends Component<
18   ListingTypeSelectProps,
19   ListingTypeSelectState
20 > {
21   private id = `listing-type-input-${randomStr()}`;
22
23   private emptyState: ListingTypeSelectState = {
24     type_: this.props.type_,
25   };
26
27   constructor(props: any, context: any) {
28     super(props, context);
29     this.state = this.emptyState;
30   }
31
32   static getDerivedStateFromProps(props: any): ListingTypeSelectProps {
33     return {
34       type_: props.type_,
35       showLocal: props.showLocal,
36     };
37   }
38
39   render() {
40     return (
41       <div class="btn-group btn-group-toggle flex-wrap mb-2">
42         <label
43           title={i18n.t("subscribed_description")}
44           className={`btn btn-outline-secondary 
45             ${this.state.type_ == ListingType.Subscribed && "active"}
46             ${
47               UserService.Instance.myUserInfo == undefined
48                 ? "disabled"
49                 : "pointer"
50             }
51           `}
52         >
53           <input
54             id={`${this.id}-subscribed`}
55             type="radio"
56             value={ListingType.Subscribed}
57             checked={this.state.type_ == ListingType.Subscribed}
58             onChange={linkEvent(this, this.handleTypeChange)}
59             disabled={UserService.Instance.myUserInfo == undefined}
60           />
61           {i18n.t("subscribed")}
62         </label>
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 }