From 0361b2c700c793e9c48ad9acdf770ab645d2188c Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 16 Jul 2021 15:40:56 -0400 Subject: [PATCH] Always show previous paginator, extract paginator component. - Fixes #304 --- src/shared/components/communities.tsx | 39 ++++--------------- src/shared/components/community.tsx | 41 +++++--------------- src/shared/components/inbox.tsx | 43 +++++---------------- src/shared/components/main.tsx | 42 +++++--------------- src/shared/components/modlog.tsx | 49 +++++++----------------- src/shared/components/paginator.tsx | 40 +++++++++++++++++++ src/shared/components/person-details.tsx | 41 ++++---------------- src/shared/components/search.tsx | 36 +++-------------- 8 files changed, 102 insertions(+), 229 deletions(-) create mode 100644 src/shared/components/paginator.tsx diff --git a/src/shared/components/communities.tsx b/src/shared/components/communities.tsx index efb92f9..1004da5 100644 --- a/src/shared/components/communities.tsx +++ b/src/shared/components/communities.tsx @@ -26,6 +26,7 @@ import { setOptionalAuth, } from "../utils"; import { CommunityLink } from "./community-link"; +import { Paginator } from "./paginator"; import { Spinner } from "./icon"; import { i18n } from "../i18next"; import { InitialFetchRequest } from "shared/interfaces"; @@ -58,6 +59,7 @@ export class Communities extends Component { constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; + this.handlePageChange = this.handlePageChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); @@ -178,7 +180,10 @@ export class Communities extends Component { - {this.paginator()} + )} @@ -211,41 +216,13 @@ export class Communities extends Component { ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - - {this.state.communities.length > 0 && ( - - )} -
- ); - } - updateUrl(paramUpdates: CommunitiesProps) { const page = paramUpdates.page || this.state.page; this.props.history.push(`/communities/page/${page}`); } - nextPage(i: Communities) { - i.updateUrl({ page: i.state.page + 1 }); - } - - prevPage(i: Communities) { - i.updateUrl({ page: i.state.page - 1 }); + handlePageChange(page: number) { + this.updateUrl({ page }); } handleUnsubscribe(communityId: number) { diff --git a/src/shared/components/community.tsx b/src/shared/components/community.tsx index b75a816..8852b67 100644 --- a/src/shared/components/community.tsx +++ b/src/shared/components/community.tsx @@ -1,4 +1,4 @@ -import { Component, linkEvent } from "inferno"; +import { Component } from "inferno"; import { Subscription } from "rxjs"; import { DataType, InitialFetchRequest } from "../interfaces"; import { @@ -30,6 +30,7 @@ import { Sidebar } from "./sidebar"; import { CommunityLink } from "./community-link"; import { BannerIconHeader } from "./banner-icon-header"; import { Icon, Spinner } from "./icon"; +import { Paginator } from "./paginator"; import { wsJsonToRes, fetchLimit, @@ -108,6 +109,7 @@ export class Community extends Component { this.state = this.emptyState; this.handleSortChange = this.handleSortChange.bind(this); this.handleDataTypeChange = this.handleDataTypeChange.bind(this); + this.handlePageChange = this.handlePageChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); @@ -255,7 +257,10 @@ export class Community extends Component { {this.communityInfo()} {this.selects()} {this.listings()} - {this.paginator()} +
{ ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - {this.state.posts.length > 0 && ( - - )} -
- ); - } - - nextPage(i: Community) { - i.updateUrl({ page: i.state.page + 1 }); - window.scrollTo(0, 0); - } - - prevPage(i: Community) { - i.updateUrl({ page: i.state.page - 1 }); + handlePageChange(page: number) { + this.updateUrl({ page }); window.scrollTo(0, 0); } diff --git a/src/shared/components/inbox.tsx b/src/shared/components/inbox.tsx index 677d905..86d7487 100644 --- a/src/shared/components/inbox.tsx +++ b/src/shared/components/inbox.tsx @@ -37,6 +37,7 @@ import { import { CommentNodes } from "./comment-nodes"; import { PrivateMessage } from "./private-message"; import { HtmlTags } from "./html-tags"; +import { Paginator } from "./paginator"; import { SortSelect } from "./sort-select"; import { Icon, Spinner } from "./icon"; import { i18n } from "../i18next"; @@ -100,6 +101,7 @@ export class Inbox extends Component { this.state = this.emptyState; this.handleSortChange = this.handleSortChange.bind(this); + this.handlePageChange = this.handlePageChange.bind(this); if (!UserService.Instance.localUserView && isBrowser()) { toast(i18n.t("not_logged_in"), "danger"); @@ -183,7 +185,10 @@ export class Inbox extends Component { this.mentions()} {this.state.messageType == MessageType.Messages && this.messages()} - {this.paginator()} +
)} @@ -429,39 +434,9 @@ export class Inbox extends Component { ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - {this.unreadCount() > 0 && ( - - )} -
- ); - } - - nextPage(i: Inbox) { - i.state.page++; - i.setState(i.state); - i.refetch(); - } - - prevPage(i: Inbox) { - i.state.page--; - i.setState(i.state); - i.refetch(); + handlePageChange(page: number) { + this.setState({ page }); + this.refetch(); } handleUnreadOrAllChange(i: Inbox, event: any) { diff --git a/src/shared/components/main.tsx b/src/shared/components/main.tsx index 6737974..99c52fe 100644 --- a/src/shared/components/main.tsx +++ b/src/shared/components/main.tsx @@ -65,6 +65,7 @@ import { import { i18n } from "../i18next"; import { T } from "inferno-i18next"; import { HtmlTags } from "./html-tags"; +import { Paginator } from "./paginator"; interface MainState { subscribedCommunities: CommunityFollowerView[]; @@ -119,6 +120,7 @@ export class Main extends Component { this.handleSortChange = this.handleSortChange.bind(this); this.handleListingTypeChange = this.handleListingTypeChange.bind(this); this.handleDataTypeChange = this.handleDataTypeChange.bind(this); + this.handlePageChange = this.handlePageChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); @@ -132,7 +134,8 @@ export class Main extends Component { } this.state.trendingCommunities = this.isoData.routeData[1].communities; if (UserService.Instance.localUserView) { - this.state.subscribedCommunities = this.isoData.routeData[2].communities; + this.state.subscribedCommunities = + this.isoData.routeData[2].communities; } this.state.loading = false; } else { @@ -553,7 +556,10 @@ export class Main extends Component {
{this.selects()} {this.listings()} - {this.paginator()} +
)} @@ -632,29 +638,6 @@ export class Main extends Component { ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - {this.state.posts.length > 0 && ( - - )} -
- ); - } - get canAdmin(): boolean { return ( UserService.Instance.localUserView && @@ -674,13 +657,8 @@ export class Main extends Component { this.setState(this.state); } - nextPage(i: Main) { - i.updateUrl({ page: i.state.page + 1 }); - window.scrollTo(0, 0); - } - - prevPage(i: Main) { - i.updateUrl({ page: i.state.page - 1 }); + handlePageChange(page: number) { + this.updateUrl({ page }); window.scrollTo(0, 0); } diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index eb0dac8..65c31ee 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -1,4 +1,4 @@ -import { Component, linkEvent } from "inferno"; +import { Component } from "inferno"; import { Link } from "inferno-router"; import { Subscription } from "rxjs"; import { @@ -38,6 +38,7 @@ import { InitialFetchRequest } from "shared/interfaces"; import { PersonListing } from "./person-listing"; import { CommunityLink } from "./community-link"; import { Spinner } from "./icon"; +import { Paginator } from "./paginator"; enum ModlogEnum { ModRemovePost, @@ -101,6 +102,8 @@ export class Modlog extends Component { super(props, context); this.state = this.emptyState; + this.handlePageChange = this.handlePageChange.bind(this); + this.state.communityId = this.props.match.params.community_id ? Number(this.props.match.params.community_id) : undefined; @@ -208,8 +211,9 @@ export class Modlog extends Component { combined.push(...banned); if (this.state.communityId && combined.length > 0) { - this.state.communityName = (combined[0] - .view as ModRemovePostView).community.name; + this.state.communityName = ( + combined[0].view as ModRemovePostView + ).community.name; } // Sort them by time @@ -429,7 +433,10 @@ export class Modlog extends Component { {this.combined()} - {this.paginator()} + )} @@ -437,37 +444,9 @@ export class Modlog extends Component { ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - -
- ); - } - - nextPage(i: Modlog) { - i.state.page++; - i.setState(i.state); - i.refetch(); - } - - prevPage(i: Modlog) { - i.state.page--; - i.setState(i.state); - i.refetch(); + handlePageChange(val: number) { + this.setState({ page: val }); + this.refetch(); } refetch() { diff --git a/src/shared/components/paginator.tsx b/src/shared/components/paginator.tsx new file mode 100644 index 0000000..055ff9f --- /dev/null +++ b/src/shared/components/paginator.tsx @@ -0,0 +1,40 @@ +import { Component, linkEvent } from "inferno"; +import { i18n } from "../i18next"; + +interface PaginatorProps { + page: number; + onChange(val: number): any; +} + +export class Paginator extends Component { + constructor(props: any, context: any) { + super(props, context); + } + render() { + return ( +
+ + +
+ ); + } + + handlePrev(i: Paginator) { + i.props.onChange(i.props.page - 1); + } + + handleNext(i: Paginator) { + i.props.onChange(i.props.page + 1); + } +} diff --git a/src/shared/components/person-details.tsx b/src/shared/components/person-details.tsx index c1d18bf..b729fb1 100644 --- a/src/shared/components/person-details.tsx +++ b/src/shared/components/person-details.tsx @@ -1,5 +1,4 @@ -import { Component, linkEvent } from "inferno"; -import { i18n } from "../i18next"; +import { Component } from "inferno"; import { PostView, CommentView, @@ -11,6 +10,7 @@ import { PersonDetailsView } from "../interfaces"; import { commentsToFlatNodes, setupTippy } from "../utils"; import { PostListing } from "./post-listing"; import { CommentNodes } from "./comment-nodes"; +import { Paginator } from "./paginator"; interface PersonDetailsProps { personRes: GetPersonDetailsResponse; @@ -39,6 +39,7 @@ type ItemType = { export class PersonDetails extends Component { constructor(props: any, context: any) { super(props, context); + this.handlePageChange = this.handlePageChange.bind(this); } // TODO needed here? @@ -60,7 +61,8 @@ export class PersonDetails extends Component { return (
{this.viewSelector(this.props.view)} - {this.paginator()} + +
); } @@ -182,36 +184,7 @@ export class PersonDetails extends Component { ); } - paginator() { - return ( -
- {this.props.page > 1 && ( - - )} - {this.props.personRes.comments.length + - this.props.personRes.posts.length > - 0 && ( - - )} -
- ); - } - - nextPage(i: PersonDetails) { - i.props.onPageChange(i.props.page + 1); - } - - prevPage(i: PersonDetails) { - i.props.onPageChange(i.props.page - 1); + handlePageChange(val: number) { + this.props.onPageChange(val); } } diff --git a/src/shared/components/search.tsx b/src/shared/components/search.tsx index dc8976c..78d8072 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -58,6 +58,7 @@ import { CommunityLink } from "./community-link"; import { SortSelect } from "./sort-select"; import { ListingTypeSelect } from "./listing-type-select"; import { CommentNodes } from "./comment-nodes"; +import { Paginator } from "./paginator"; import { i18n } from "../i18next"; import { InitialFetchRequest } from "shared/interfaces"; @@ -166,6 +167,7 @@ export class Search extends Component { this.state = this.emptyState; this.handleSortChange = this.handleSortChange.bind(this); this.handleListingTypeChange = this.handleListingTypeChange.bind(this); + this.handlePageChange = this.handlePageChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); @@ -328,7 +330,7 @@ export class Search extends Component { {this.state.type_ == SearchType.Users && this.users()} {this.state.type_ == SearchType.Url && this.posts()} {this.resultsCount() == 0 && {i18n.t("no_results")}} - {this.paginator()} + ); } @@ -605,30 +607,6 @@ export class Search extends Component { ); } - paginator() { - return ( -
- {this.state.page > 1 && ( - - )} - - {this.resultsCount() > 0 && ( - - )} -
- ); - } - resultsCount(): number { let res = this.state.searchResponse; return ( @@ -639,12 +617,8 @@ export class Search extends Component { ); } - nextPage(i: Search) { - i.updateUrl({ page: i.state.page + 1 }); - } - - prevPage(i: Search) { - i.updateUrl({ page: i.state.page - 1 }); + handlePageChange(page: number) { + this.updateUrl({ page }); } search() { -- 2.44.1