removed unnecessary local page state

This commit is contained in:
Florian Scholdei
2019-04-17 15:46:49 +02:00
parent 9bc9e133cc
commit 53ce955f66
3 changed files with 21 additions and 70 deletions

View File

@@ -2,7 +2,6 @@
import React from "react"; import React from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import { compose } from "redux";
import type { History } from "history"; import type { History } from "history";
import queryString from "query-string"; import queryString from "query-string";
import type { Group, PagedCollection } from "@scm-manager/ui-types"; import type { Group, PagedCollection } from "@scm-manager/ui-types";
@@ -46,41 +45,21 @@ type Props = {
fetchGroupsByLink: (link: string) => void fetchGroupsByLink: (link: string) => void
}; };
type State = { class Groups extends React.Component<Props> {
page: number
};
class Groups extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
page: -1
};
}
componentDidMount() { componentDidMount() {
const { fetchGroupsByPage, groupLink, page } = this.props; const { fetchGroupsByPage, groupLink, page } = this.props;
fetchGroupsByPage(groupLink, page, this.getQueryString()); fetchGroupsByPage(groupLink, page, this.getQueryString());
this.setState({ page: page });
} }
onPageChange = (link: string) => {
this.props.fetchGroupsByLink(link);
};
/**
* reflect page transitions in the uri
*/
componentDidUpdate = (prevProps: Props) => { componentDidUpdate = (prevProps: Props) => {
const { list, page, location, fetchGroupsByPage, groupLink } = this.props; const { list, page, loading, location, fetchGroupsByPage, groupLink } = this.props;
if (list && page) { if (list && page && !loading) {
const statePage: number = list.page + 1;
if ( if (
page !== this.state.page || page !== statePage ||
prevProps.location.search !== location.search prevProps.location.search !== location.search
) { ) {
fetchGroupsByPage(groupLink, page, this.getQueryString()); fetchGroupsByPage(groupLink, page, this.getQueryString());
this.setState({ page: page });
} }
} }
}; };
@@ -192,9 +171,7 @@ const mapDispatchToProps = dispatch => {
}; };
}; };
export default compose( export default connect(
connect(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)
)(translate("groups")(Groups)); )(translate("groups")(Groups));

View File

@@ -1,9 +1,11 @@
// @flow // @flow
import React from "react"; import React from "react";
import type { RepositoryCollection } from "@scm-manager/ui-types";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { translate } from "react-i18next";
import type { History } from "history";
import queryString from "query-string";
import { withRouter } from "react-router-dom";
import type { RepositoryCollection } from "@scm-manager/ui-types";
import { import {
fetchRepos, fetchRepos,
fetchReposByLink, fetchReposByLink,
@@ -13,7 +15,6 @@ import {
isAbleToCreateRepos, isAbleToCreateRepos,
isFetchReposPending isFetchReposPending
} from "../modules/repos"; } from "../modules/repos";
import { translate } from "react-i18next";
import { import {
Page, Page,
PageActions, PageActions,
@@ -24,10 +25,7 @@ import {
getPageFromMatch getPageFromMatch
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import RepositoryList from "../components/list"; import RepositoryList from "../components/list";
import { withRouter } from "react-router-dom";
import type { History } from "history";
import { getRepositoriesLink } from "../../modules/indexResource"; import { getRepositoriesLink } from "../../modules/indexResource";
import queryString from "query-string";
type Props = { type Props = {
page: number, page: number,
@@ -48,40 +46,28 @@ type Props = {
fetchReposByLink: string => void fetchReposByLink: string => void
}; };
type State = { class Overview extends React.Component<Props> {
page: number
};
class Overview extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
page: -1
};
}
componentDidMount() { componentDidMount() {
const { fetchReposByPage, reposLink, page } = this.props; const { fetchReposByPage, reposLink, page } = this.props;
fetchReposByPage(reposLink, page, this.getQueryString()); fetchReposByPage(reposLink, page, this.getQueryString());
this.setState({ page: page });
} }
componentDidUpdate = (prevProps: Props) => { componentDidUpdate = (prevProps: Props) => {
const { const {
collection, collection,
page, page,
loading,
location, location,
fetchReposByPage, fetchReposByPage,
reposLink reposLink
} = this.props; } = this.props;
if (collection && page) { if (collection && page && !loading) {
const statePage: number = collection.page + 1;
if ( if (
page !== this.state.page || page !== statePage ||
prevProps.location.search !== location.search prevProps.location.search !== location.search
) { ) {
fetchReposByPage(reposLink, page, this.getQueryString()); fetchReposByPage(reposLink, page, this.getQueryString());
this.setState({ page: page });
} }
} }
}; };

View File

@@ -45,34 +45,22 @@ type Props = {
fetchUsersByLink: (link: string) => void fetchUsersByLink: (link: string) => void
}; };
type State = { class Users extends React.Component<Props> {
page: number
};
class Users extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
page: -1
};
}
componentDidMount() { componentDidMount() {
const { fetchUsersByPage, usersLink, page } = this.props; const { fetchUsersByPage, usersLink, page } = this.props;
fetchUsersByPage(usersLink, page, this.getQueryString()); fetchUsersByPage(usersLink, page, this.getQueryString());
this.setState({ page: page });
} }
componentDidUpdate = (prevProps: Props) => { componentDidUpdate = (prevProps: Props) => {
const { list, page, location, fetchUsersByPage, usersLink } = this.props; const { list, page, loading, location, fetchUsersByPage, usersLink } = this.props;
if (list && page) { if (list && page && !loading) {
const statePage: number = list.page + 1;
if ( if (
page !== this.state.page || page !== statePage ||
prevProps.location.search !== location.search prevProps.location.search !== location.search
) { ) {
fetchUsersByPage(usersLink, page, this.getQueryString()); fetchUsersByPage(usersLink, page, this.getQueryString());
this.setState({ page: page });
} }
} }
}; };