]> Untitled Git - lemmy.git/blob - ui/src/components/sort-select.tsx
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / ui / src / components / sort-select.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { SortType } from 'lemmy-js-client';
3 import { sortingHelpUrl, randomStr } from '../utils';
4 import { i18n } from '../i18next';
5
6 interface SortSelectProps {
7   sort: SortType;
8   onChange?(val: SortType): any;
9   hideHot?: boolean;
10 }
11
12 interface SortSelectState {
13   sort: SortType;
14 }
15
16 export class SortSelect extends Component<SortSelectProps, SortSelectState> {
17   private id = `sort-select-${randomStr()}`;
18   private emptyState: SortSelectState = {
19     sort: this.props.sort,
20   };
21
22   constructor(props: any, context: any) {
23     super(props, context);
24     this.state = this.emptyState;
25   }
26
27   static getDerivedStateFromProps(props: any): SortSelectState {
28     return {
29       sort: props.sort,
30     };
31   }
32
33   render() {
34     return (
35       <>
36         <select
37           id={this.id}
38           name={this.id}
39           value={this.state.sort}
40           onChange={linkEvent(this, this.handleSortChange)}
41           class="custom-select w-auto mr-2 mb-2"
42         >
43           <option disabled>{i18n.t('sort_type')}</option>
44           {!this.props.hideHot && (
45             <>
46               <option value={SortType.Active}>{i18n.t('active')}</option>
47               <option value={SortType.Hot}>{i18n.t('hot')}</option>
48             </>
49           )}
50           <option value={SortType.New}>{i18n.t('new')}</option>
51           <option disabled>─────</option>
52           <option value={SortType.TopDay}>{i18n.t('top_day')}</option>
53           <option value={SortType.TopWeek}>{i18n.t('week')}</option>
54           <option value={SortType.TopMonth}>{i18n.t('month')}</option>
55           <option value={SortType.TopYear}>{i18n.t('year')}</option>
56           <option value={SortType.TopAll}>{i18n.t('all')}</option>
57         </select>
58         <a
59           className="text-muted"
60           href={sortingHelpUrl}
61           target="_blank"
62           rel="noopener"
63           title={i18n.t('sorting_help')}
64         >
65           <svg class={`icon icon-inline`}>
66             <use xlinkHref="#icon-help-circle"></use>
67           </svg>
68         </a>
69       </>
70     );
71   }
72
73   handleSortChange(i: SortSelect, event: any) {
74     i.props.onChange(event.target.value);
75   }
76 }