ListCommunities,
ListCommunitiesResponse,
ListingType,
+ SortType,
} from "lemmy-js-client";
import { InitialFetchRequest } from "../../interfaces";
import { FirstLoadService, I18NextService } from "../../services";
import { Spinner } from "../common/icon";
import { ListingTypeSelect } from "../common/listing-type-select";
import { Paginator } from "../common/paginator";
+import { SortSelect } from "../common/sort-select";
import { CommunityLink } from "./community-link";
const communityLimit = 50;
interface CommunitiesProps {
listingType: ListingType;
+ sort: SortType;
page: number;
}
return listingType ? (listingType as ListingType) : "Local";
}
+function getSortTypeFromQuery(type?: string): SortType {
+ return type ? (type as SortType) : "TopMonth";
+}
+
export class Communities extends Component<any, CommunitiesState> {
private isoData = setIsoData<CommunitiesData>(this.context);
state: CommunitiesState = {
constructor(props: any, context: any) {
super(props, context);
this.handlePageChange = this.handlePageChange.bind(this);
+ this.handleSortChange = this.handleSortChange.bind(this);
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
// Only fetch the data if coming from another route
</h5>
);
case "success": {
- const { listingType, page } = this.getCommunitiesQueryParams();
+ const { listingType, sort, page } = this.getCommunitiesQueryParams();
return (
<div>
<h1 className="h4 mb-4">
{I18NextService.i18n.t("list_of_communities")}
</h1>
- <div className="row g-2 justify-content-between">
+ <div className="row g-3 align-items-center mb-2">
<div className="col-auto">
<ListingTypeSelect
type_={listingType}
onChange={this.handleListingTypeChange}
/>
</div>
+ <div className="col-auto me-auto">
+ <SortSelect sort={sort} onChange={this.handleSortChange} />
+ </div>
<div className="col-auto">{this.searchForm()}</div>
</div>
searchForm() {
return (
- <form
- className="row mb-2"
- onSubmit={linkEvent(this, this.handleSearchSubmit)}
- >
+ <form className="row" onSubmit={linkEvent(this, this.handleSearchSubmit)}>
<div className="col-auto">
<input
type="text"
);
}
- async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
- const { listingType: urlListingType, page: urlPage } =
- this.getCommunitiesQueryParams();
+ async updateUrl({ listingType, sort, page }: Partial<CommunitiesProps>) {
+ const {
+ listingType: urlListingType,
+ sort: urlSort,
+ page: urlPage,
+ } = this.getCommunitiesQueryParams();
const queryParams: QueryParams<CommunitiesProps> = {
listingType: listingType ?? urlListingType,
+ sort: sort ?? urlSort,
page: (page ?? urlPage)?.toString(),
};
this.updateUrl({ page });
}
+ handleSortChange(val: SortType) {
+ this.updateUrl({ sort: val, page: 1 });
+ }
+
handleListingTypeChange(val: ListingType) {
this.updateUrl({
listingType: val,
}
static async fetchInitialData({
- query: { listingType, page },
+ query: { listingType, sort, page },
client,
auth,
}: InitialFetchRequest<
>): Promise<CommunitiesData> {
const listCommunitiesForm: ListCommunities = {
type_: getListingTypeFromQuery(listingType),
- sort: "TopMonth",
+ sort: getSortTypeFromQuery(sort),
limit: communityLimit,
page: getPageFromString(page),
auth: auth,
getCommunitiesQueryParams() {
return getQueryParams<CommunitiesProps>({
listingType: getListingTypeFromQuery,
+ sort: getSortTypeFromQuery,
page: getPageFromString,
});
}
async refetch() {
this.setState({ listCommunitiesResponse: { state: "loading" } });
- const { listingType, page } = this.getCommunitiesQueryParams();
+ const { listingType, sort, page } = this.getCommunitiesQueryParams();
this.setState({
listCommunitiesResponse: await HttpService.client.listCommunities({
type_: listingType,
- sort: "TopMonth",
+ sort: sort,
limit: communityLimit,
page,
auth: myAuth(),