diff --git a/ui/src/components/communities.tsx b/ui/src/components/communities.tsx index 10a3ab80..ba362acc 100644 --- a/ui/src/components/communities.tsx +++ b/ui/src/components/communities.tsx @@ -13,7 +13,7 @@ import { GetSiteResponse, } from '../interfaces'; import { WebSocketService } from '../services'; -import { wsJsonToRes, toast } from '../utils'; +import { wsJsonToRes, toast, getPageFromProps } from '../utils'; import { CommunityLink } from './community-link'; import { i18n } from '../i18next'; @@ -27,12 +27,16 @@ interface CommunitiesState { loading: boolean; } +interface CommunitiesProps { + page: number; +} + export class Communities extends Component { private subscription: Subscription; private emptyState: CommunitiesState = { communities: [], loading: true, - page: this.getPageFromProps(this.props), + page: getPageFromProps(this.props), }; constructor(props: any, context: any) { @@ -50,19 +54,19 @@ export class Communities extends Component { WebSocketService.Instance.getSite(); } - getPageFromProps(props: any): number { - return props.match.params.page ? Number(props.match.params.page) : 1; - } - componentWillUnmount() { this.subscription.unsubscribe(); } - // Necessary for back button for some reason - componentWillReceiveProps(nextProps: any) { - if (nextProps.history.action == 'POP') { - this.state = this.emptyState; - this.state.page = this.getPageFromProps(nextProps); + static getDerivedStateFromProps(props: any): CommunitiesProps { + return { + page: getPageFromProps(props), + }; + } + + componentDidUpdate(_: any, lastState: CommunitiesState) { + if (lastState.page !== this.state.page) { + this.setState({ loading: true }); this.refetch(); } } @@ -172,22 +176,17 @@ export class Communities extends Component { ); } - updateUrl() { - this.props.history.push(`/communities/page/${this.state.page}`); + updateUrl(paramUpdates: CommunitiesProps) { + const page = paramUpdates.page || this.state.page; + this.props.history.push(`/communities/page/${page}`); } nextPage(i: Communities) { - i.state.page++; - i.setState(i.state); - i.updateUrl(); - i.refetch(); + i.updateUrl({ page: i.state.page + 1 }); } prevPage(i: Communities) { - i.state.page--; - i.setState(i.state); - i.updateUrl(); - i.refetch(); + i.updateUrl({ page: i.state.page - 1 }); } handleUnsubscribe(communityId: number) { diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index fc999b25..99b692ca 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -65,6 +65,18 @@ interface State { site: Site; } +interface CommunityProps { + dataType: DataType; + sort: SortType; + page: number; +} + +interface UrlParams { + dataType?: string; + sort?: string; + page?: number; +} + export class Community extends Component { private subscription: Subscription; private emptyState: State = { @@ -143,16 +155,21 @@ export class Community extends Component { this.subscription.unsubscribe(); } - // Necessary for back button for some reason - componentWillReceiveProps(nextProps: any) { + static getDerivedStateFromProps(props: any): CommunityProps { + return { + dataType: getDataTypeFromProps(props), + sort: getSortTypeFromProps(props), + page: getPageFromProps(props), + }; + } + + componentDidUpdate(_: any, lastState: State) { if ( - nextProps.history.action == 'POP' || - nextProps.history.action == 'PUSH' + lastState.dataType !== this.state.dataType || + lastState.sort !== this.state.sort || + lastState.page !== this.state.page ) { - this.state.dataType = getDataTypeFromProps(nextProps); - this.state.sort = getSortTypeFromProps(nextProps); - this.state.page = getPageFromProps(nextProps); - this.setState(this.state); + this.setState({ loading: true }); this.fetchData(); } } @@ -273,46 +290,33 @@ export class Community extends Component { } nextPage(i: Community) { - i.state.page++; - i.setState(i.state); - i.updateUrl(); - i.fetchData(); + i.updateUrl({ page: i.state.page + 1 }); window.scrollTo(0, 0); } prevPage(i: Community) { - i.state.page--; - i.setState(i.state); - i.updateUrl(); - i.fetchData(); + i.updateUrl({ page: i.state.page - 1 }); window.scrollTo(0, 0); } handleSortChange(val: SortType) { - this.state.sort = val; - this.state.page = 1; - this.state.loading = true; - this.setState(this.state); - this.updateUrl(); - this.fetchData(); + this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 }); window.scrollTo(0, 0); } handleDataTypeChange(val: DataType) { - this.state.dataType = val; - this.state.page = 1; - this.state.loading = true; - this.setState(this.state); - this.updateUrl(); - this.fetchData(); + this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 }); window.scrollTo(0, 0); } - updateUrl() { - let dataTypeStr = DataType[this.state.dataType].toLowerCase(); - let sortStr = SortType[this.state.sort].toLowerCase(); + updateUrl(paramUpdates: UrlParams) { + const dataTypeStr = + paramUpdates.dataType || DataType[this.state.dataType].toLowerCase(); + const sortStr = + paramUpdates.sort || SortType[this.state.sort].toLowerCase(); + const page = paramUpdates.page || this.state.page; this.props.history.push( - `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}` + `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}` ); } diff --git a/ui/src/components/data-type-select.tsx b/ui/src/components/data-type-select.tsx index f2539c81..8e905bf4 100644 --- a/ui/src/components/data-type-select.tsx +++ b/ui/src/components/data-type-select.tsx @@ -25,6 +25,12 @@ export class DataTypeSelect extends Component< this.state = this.emptyState; } + static getDerivedStateFromProps(props) { + return { + type_: props.type_, + }; + } + render() { return (
@@ -42,8 +48,9 @@ export class DataTypeSelect extends Component< {i18n.t('posts')}
{this.userInfo()} @@ -268,52 +258,52 @@ export class User extends Component {