From: Dessalines Date: Sun, 18 Jul 2021 16:17:50 +0000 (-0400) Subject: Adding Listing type to communities page, default local. #190 (#342) X-Git-Url: http://these/git/%7BpictrsAvatarThumbnail%28this.props.site.site.icon%29%7D?a=commitdiff_plain;h=5f8809c344498124d6ce72f3f37bcaf70c006651;p=lemmy-ui.git Adding Listing type to communities page, default local. #190 (#342) --- diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index d57f1a4..c3dab58 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -13,13 +13,15 @@ import { import { Subscription } from "rxjs"; import { InitialFetchRequest } from "shared/interfaces"; import { i18n } from "../../i18next"; -import { WebSocketService } from "../../services"; +import { UserService, WebSocketService } from "../../services"; import { authField, + getListingTypeFromProps, getPageFromProps, isBrowser, setIsoData, setOptionalAuth, + showLocal, toast, wsClient, wsJsonToRes, @@ -28,6 +30,7 @@ import { } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; +import { ListingTypeSelect } from "../common/listing-type-select"; import { Paginator } from "../common/paginator"; import { CommunityLink } from "./community-link"; @@ -39,10 +42,12 @@ interface CommunitiesState { loading: boolean; site_view: SiteView; searchText: string; + listingType: ListingType; } interface CommunitiesProps { - page: number; + listingType?: ListingType; + page?: number; } export class Communities extends Component { @@ -52,6 +57,7 @@ export class Communities extends Component { communities: [], loading: true, page: getPageFromProps(this.props), + listingType: getListingTypeFromProps(this.props), site_view: this.isoData.site_res.site_view, searchText: "", }; @@ -60,6 +66,7 @@ export class Communities extends Component { super(props, context); this.state = this.emptyState; this.handlePageChange = this.handlePageChange.bind(this); + this.handleListingTypeChange = this.handleListingTypeChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); @@ -81,12 +88,16 @@ export class Communities extends Component { static getDerivedStateFromProps(props: any): CommunitiesProps { return { + listingType: getListingTypeFromProps(props), page: getPageFromProps(props), }; } componentDidUpdate(_: any, lastState: CommunitiesState) { - if (lastState.page !== this.state.page) { + if ( + lastState.page !== this.state.page || + lastState.listingType !== this.state.listingType + ) { this.setState({ loading: true }); this.refetch(); } @@ -112,6 +123,13 @@ export class Communities extends Component {

{i18n.t("list_of_communities")}

+ + +
{this.searchForm()}
@@ -216,13 +234,23 @@ export class Communities extends Component { updateUrl(paramUpdates: CommunitiesProps) { const page = paramUpdates.page || this.state.page; - this.props.history.push(`/communities/page/${page}`); + const listingTypeStr = paramUpdates.listingType || this.state.listingType; + this.props.history.push( + `/communities/listing_type/${listingTypeStr}/page/${page}` + ); } handlePageChange(page: number) { this.updateUrl({ page }); } + handleListingTypeChange(val: ListingType) { + this.updateUrl({ + listingType: val, + page: 1, + }); + } + handleUnsubscribe(communityId: number) { let form: FollowCommunity = { community_id: communityId, @@ -254,7 +282,7 @@ export class Communities extends Component { refetch() { let listCommunitiesForm: ListCommunities = { - type_: ListingType.All, + type_: this.state.listingType, sort: SortType.TopMonth, limit: communityLimit, page: this.state.page, @@ -268,9 +296,16 @@ export class Communities extends Component { static fetchInitialData(req: InitialFetchRequest): Promise[] { let pathSplit = req.path.split("/"); - let page = pathSplit[3] ? Number(pathSplit[3]) : 1; + let type_: ListingType = pathSplit[3] + ? ListingType[pathSplit[3]] + : UserService.Instance.localUserView + ? Object.values(ListingType)[ + UserService.Instance.localUserView.local_user.default_listing_type + ] + : ListingType.Local; + let page = pathSplit[5] ? Number(pathSplit[5]) : 1; let listCommunitiesForm: ListCommunities = { - type_: ListingType.All, + type_, sort: SortType.TopMonth, limit: communityLimit, page, diff --git a/src/shared/routes.ts b/src/shared/routes.ts index ff5d6a1..7bfc8f9 100644 --- a/src/shared/routes.ts +++ b/src/shared/routes.ts @@ -52,7 +52,7 @@ export const routes: IRoutePropsWithFetch[] = [ fetchInitialData: req => CreatePrivateMessage.fetchInitialData(req), }, { - path: `/communities/page/:page`, + path: `/communities/listing_type/:listing_type/page/:page`, component: Communities, fetchInitialData: req => Communities.fetchInitialData(req), },