improved filter/search function and used new pagination for Users

This commit is contained in:
Florian Scholdei
2019-04-17 12:50:06 +02:00
parent fc48e1bbd7
commit 1ed805f16e
3 changed files with 77 additions and 70 deletions

View File

@@ -2,20 +2,10 @@
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 type { Group } from "@scm-manager/ui-types"; import { compose } from "redux";
import type { PagedCollection } from "@scm-manager/ui-types";
import type { History } from "history"; import type { History } from "history";
import queryString from "query-string"; import queryString from "query-string";
import { import type { Group, PagedCollection } from "@scm-manager/ui-types";
Page,
PageActions,
Button,
LinkPaginator,
getPageFromMatch
} from "@scm-manager/ui-components";
import { GroupTable } from "./../components/table";
import CreateGroupButton from "../components/buttons/CreateGroupButton";
import { import {
fetchGroupsByPage, fetchGroupsByPage,
fetchGroupsByLink, fetchGroupsByLink,
@@ -25,8 +15,16 @@ import {
isPermittedToCreateGroups, isPermittedToCreateGroups,
selectListAsCollection selectListAsCollection
} from "../modules/groups"; } from "../modules/groups";
import {
Page,
PageActions,
Button,
LinkPaginator,
getPageFromMatch
} from "@scm-manager/ui-components";
import { GroupTable } from "./../components/table";
import CreateGroupButton from "../components/buttons/CreateGroupButton";
import { getGroupsLink } from "../../modules/indexResource"; import { getGroupsLink } from "../../modules/indexResource";
import { compose } from "redux";
type Props = { type Props = {
groups: Group[], groups: Group[],

View File

@@ -1,9 +1,10 @@
// @flow // @flow
import React from "react"; import React from "react";
import type { History } from "history";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import type { History } from "history";
import queryString from "query-string";
import type { User, PagedCollection } from "@scm-manager/ui-types";
import { import {
fetchUsersByPage, fetchUsersByPage,
fetchUsersByLink, fetchUsersByLink,
@@ -13,16 +14,15 @@ import {
isFetchUsersPending, isFetchUsersPending,
getFetchUsersFailure getFetchUsersFailure
} from "../modules/users"; } from "../modules/users";
import { import {
Page, Page,
PageActions, PageActions,
Button, Button,
CreateButton, CreateButton,
Paginator LinkPaginator,
getPageFromMatch
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import { UserTable } from "./../components/table"; import { UserTable } from "./../components/table";
import type { User, PagedCollection } from "@scm-manager/ui-types";
import { getUsersLink } from "../../modules/indexResource"; import { getUsersLink } from "../../modules/indexResource";
type Props = { type Props = {
@@ -37,37 +37,47 @@ type Props = {
// context objects // context objects
t: string => string, t: string => string,
history: History, history: History,
location: any,
// dispatch functions // dispatch functions
fetchUsersByPage: (link: string, page: number, filter?: string) => void, fetchUsersByPage: (link: string, page: number, filter?: any) => void,
fetchUsersByLink: (link: string) => void fetchUsersByLink: (link: string) => void
}; };
class Users extends React.Component<Props> { type State = {
componentDidMount() { page: number
this.props.fetchUsersByPage(this.props.usersLink, this.props.page); };
class Users extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
page: -1
};
} }
onPageChange = (link: string) => { componentDidMount() {
this.props.fetchUsersByLink(link); const { fetchUsersByPage, usersLink, page } = this.props;
}; fetchUsersByPage(usersLink, page, this.getQueryString());
this.setState({ page: page });
}
/** componentDidUpdate = (prevProps: Props) => {
* reflect page transitions in the uri const { list, page, location, fetchUsersByPage, usersLink } = this.props;
*/ if (list && page) {
componentDidUpdate() { if (
const { page, list } = this.props; page !== this.state.page ||
if (list && (list.page || list.page === 0)) { prevProps.location.search !== location.search
// backend starts paging by 0 ) {
const statePage: number = list.page + 1; fetchUsersByPage(usersLink, page, this.getQueryString());
if (page !== statePage) { this.setState({ page: page });
this.props.history.push(`/users/${statePage}`);
} }
} }
} };
render() { render() {
const { users, loading, error, t } = this.props; const { users, loading, error, history, t } = this.props;
return ( return (
<Page <Page
title={t("users.title")} title={t("users.title")}
@@ -75,7 +85,7 @@ class Users extends React.Component<Props> {
loading={loading || !users} loading={loading || !users}
error={error} error={error}
filter={filter => { filter={filter => {
this.props.fetchUsersByPage(this.props.usersLink, this.props.page, filter); history.push("/users/?q=" + filter);
}} }}
> >
<UserTable users={users} /> <UserTable users={users} />
@@ -86,26 +96,31 @@ class Users extends React.Component<Props> {
); );
} }
renderPaginator() { renderPaginator = () => {
const { list } = this.props; const { list, page } = this.props;
if (list) { if (list) {
return <Paginator collection={list} onPageChange={this.onPageChange} />; return (
<LinkPaginator
collection={list}
page={page}
filter={this.getQueryString()}
/>
);
}
return null;
};
renderCreateButton() {
const { canAddUsers, t } = this.props;
if (canAddUsers) {
return <CreateButton label={t("users.createButton")} link="/users/add" />;
} }
return null; return null;
} }
renderCreateButton() {
const { t } = this.props;
if (this.props.canAddUsers) {
return <CreateButton label={t("users.createButton")} link="/users/add" />;
} else {
return;
}
}
renderPageActionCreateButton() { renderPageActionCreateButton() {
const { t } = this.props; const { canAddUsers, t } = this.props;
if (this.props.canAddUsers) { if (canAddUsers) {
return ( return (
<PageActions> <PageActions>
<Button <Button
@@ -115,32 +130,26 @@ class Users extends React.Component<Props> {
/> />
</PageActions> </PageActions>
); );
} else {
return;
} }
return null;
} }
getQueryString = () => {
const { location } = this.props;
return location.search ? queryString.parse(location.search).q : null;
};
} }
const getPageFromProps = props => {
let page = props.match.params.page;
if (page) {
page = parseInt(page, 10);
} else {
page = 1;
}
return page;
};
const mapStateToProps = (state, ownProps) => { const mapStateToProps = (state, ownProps) => {
const { match } = ownProps;
const users = getUsersFromState(state); const users = getUsersFromState(state);
const loading = isFetchUsersPending(state); const loading = isFetchUsersPending(state);
const error = getFetchUsersFailure(state); const error = getFetchUsersFailure(state);
const page = getPageFromMatch(match);
const usersLink = getUsersLink(state);
const page = getPageFromProps(ownProps);
const canAddUsers = isPermittedToCreateUsers(state); const canAddUsers = isPermittedToCreateUsers(state);
const list = selectListAsCollection(state); const list = selectListAsCollection(state);
const usersLink = getUsersLink(state);
return { return {
users, users,
@@ -155,7 +164,7 @@ const mapStateToProps = (state, ownProps) => {
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
fetchUsersByPage: (link: string, page: number, filter?: string) => { fetchUsersByPage: (link: string, page: number, filter?: any) => {
dispatch(fetchUsersByPage(link, page, filter)); dispatch(fetchUsersByPage(link, page, filter));
}, },
fetchUsersByLink: (link: string) => { fetchUsersByLink: (link: string) => {

View File

@@ -43,7 +43,7 @@ export function fetchUsers(link: string) {
return fetchUsersByLink(link); return fetchUsersByLink(link);
} }
export function fetchUsersByPage(link: string, page: number, filter?: string) { export function fetchUsersByPage(link: string, page: number, filter?: any) {
// backend start counting by 0 // backend start counting by 0
if (filter) { if (filter) {
return fetchUsersByLink( return fetchUsersByLink(