2018-07-02 16:05:58 +02:00
|
|
|
// @flow
|
2018-07-10 08:38:38 +02:00
|
|
|
import React from "react";
|
2018-07-26 15:30:28 +02:00
|
|
|
import type { History } from "history";
|
2018-07-10 08:38:38 +02:00
|
|
|
import { connect } from "react-redux";
|
2018-07-24 16:04:55 +02:00
|
|
|
import { translate } from "react-i18next";
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-26 15:30:28 +02:00
|
|
|
import {
|
|
|
|
|
fetchUsersByPage,
|
|
|
|
|
fetchUsersByLink,
|
|
|
|
|
getUsersFromState,
|
2018-07-27 08:04:48 +02:00
|
|
|
selectListAsCollection,
|
2018-07-30 13:38:15 +02:00
|
|
|
isPermittedToCreateUsers,
|
|
|
|
|
isFetchUsersPending,
|
|
|
|
|
getFetchUsersFailure
|
2018-07-26 15:30:28 +02:00
|
|
|
} from "../modules/users";
|
|
|
|
|
|
2018-09-05 14:32:49 +02:00
|
|
|
import { Page, Paginator } from "@scm-manager/ui-components";
|
2018-07-25 15:04:19 +02:00
|
|
|
import { UserTable } from "./../components/table";
|
2018-09-05 14:32:49 +02:00
|
|
|
import type { User, PagedCollection } from "@scm-manager/ui-types";
|
2018-07-26 15:30:28 +02:00
|
|
|
import CreateUserButton from "../components/buttons/CreateUserButton";
|
2018-10-09 16:40:44 +02:00
|
|
|
import { getUsersLink } from "../../modules/indexResource";
|
2018-07-12 16:02:37 +02:00
|
|
|
|
2018-07-02 16:05:58 +02:00
|
|
|
type Props = {
|
2018-07-30 13:38:15 +02:00
|
|
|
users: User[],
|
|
|
|
|
loading: boolean,
|
|
|
|
|
error: Error,
|
2018-07-26 15:30:28 +02:00
|
|
|
canAddUsers: boolean,
|
2018-07-30 13:38:15 +02:00
|
|
|
list: PagedCollection,
|
2018-07-27 08:04:48 +02:00
|
|
|
page: number,
|
2018-10-09 16:40:44 +02:00
|
|
|
usersLink: string,
|
2018-07-27 08:04:48 +02:00
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
t: string => string,
|
2018-07-26 15:30:28 +02:00
|
|
|
history: History,
|
2018-07-27 08:04:48 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2018-10-09 16:40:44 +02:00
|
|
|
fetchUsersByPage: (link: string, page: number) => void,
|
2018-07-27 08:04:48 +02:00
|
|
|
fetchUsersByLink: (link: string) => void
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-27 08:04:48 +02:00
|
|
|
class Users extends React.Component<Props> {
|
2018-07-11 12:02:53 +02:00
|
|
|
componentDidMount() {
|
2018-10-09 16:40:44 +02:00
|
|
|
this.props.fetchUsersByPage(this.props.usersLink, this.props.page);
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-26 15:30:28 +02:00
|
|
|
onPageChange = (link: string) => {
|
|
|
|
|
this.props.fetchUsersByLink(link);
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-27 08:04:48 +02:00
|
|
|
/**
|
|
|
|
|
* reflect page transitions in the uri
|
|
|
|
|
*/
|
2018-08-01 14:58:52 +02:00
|
|
|
componentDidUpdate() {
|
2018-07-26 15:30:28 +02:00
|
|
|
const { page, list } = this.props;
|
2018-08-01 18:23:16 +02:00
|
|
|
if (list && (list.page || list.page === 0)) {
|
2018-07-27 08:04:48 +02:00
|
|
|
// backend starts paging by 0
|
2018-07-30 13:38:15 +02:00
|
|
|
const statePage: number = list.page + 1;
|
2018-07-26 15:30:28 +02:00
|
|
|
if (page !== statePage) {
|
|
|
|
|
this.props.history.push(`/users/${statePage}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-08 14:59:39 +02:00
|
|
|
}
|
2018-07-26 15:30:28 +02:00
|
|
|
|
2018-07-02 16:05:58 +02:00
|
|
|
render() {
|
2018-07-30 13:38:15 +02:00
|
|
|
const { users, loading, error, t } = this.props;
|
2018-07-18 09:35:16 +02:00
|
|
|
return (
|
2018-07-20 08:28:36 +02:00
|
|
|
<Page
|
2018-07-24 16:04:55 +02:00
|
|
|
title={t("users.title")}
|
|
|
|
|
subtitle={t("users.subtitle")}
|
2018-07-30 13:38:15 +02:00
|
|
|
loading={loading || !users}
|
|
|
|
|
error={error}
|
2018-07-20 08:28:36 +02:00
|
|
|
>
|
2018-07-30 13:38:15 +02:00
|
|
|
<UserTable users={users} />
|
2018-07-26 15:30:28 +02:00
|
|
|
{this.renderPaginator()}
|
2018-07-27 08:04:48 +02:00
|
|
|
{this.renderCreateButton()}
|
2018-07-20 08:28:36 +02:00
|
|
|
</Page>
|
2018-07-18 09:35:16 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 15:30:28 +02:00
|
|
|
renderPaginator() {
|
|
|
|
|
const { list } = this.props;
|
2018-07-30 13:38:15 +02:00
|
|
|
if (list) {
|
|
|
|
|
return <Paginator collection={list} onPageChange={this.onPageChange} />;
|
2018-07-26 15:30:28 +02:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 08:04:48 +02:00
|
|
|
renderCreateButton() {
|
2018-07-26 15:30:28 +02:00
|
|
|
if (this.props.canAddUsers) {
|
|
|
|
|
return <CreateUserButton />;
|
2018-07-19 12:05:50 +02:00
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 08:04:48 +02:00
|
|
|
const getPageFromProps = props => {
|
|
|
|
|
let page = props.match.params.page;
|
2018-07-26 15:30:28 +02:00
|
|
|
if (page) {
|
|
|
|
|
page = parseInt(page, 10);
|
|
|
|
|
} else {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
2018-07-27 08:04:48 +02:00
|
|
|
return page;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-07-30 13:38:15 +02:00
|
|
|
const users = getUsersFromState(state);
|
|
|
|
|
const loading = isFetchUsersPending(state);
|
|
|
|
|
const error = getFetchUsersFailure(state);
|
|
|
|
|
|
2018-10-09 16:40:44 +02:00
|
|
|
const usersLink = getUsersLink(state);
|
|
|
|
|
|
2018-07-27 08:04:48 +02:00
|
|
|
const page = getPageFromProps(ownProps);
|
|
|
|
|
const canAddUsers = isPermittedToCreateUsers(state);
|
|
|
|
|
const list = selectListAsCollection(state);
|
2018-07-30 13:38:15 +02:00
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
return {
|
2018-07-30 13:38:15 +02:00
|
|
|
users,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
2018-07-26 15:30:28 +02:00
|
|
|
canAddUsers,
|
|
|
|
|
list,
|
2018-10-09 16:40:44 +02:00
|
|
|
page,
|
|
|
|
|
usersLink
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
const mapDispatchToProps = dispatch => {
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
2018-10-09 16:40:44 +02:00
|
|
|
fetchUsersByPage: (link: string, page: number) => {
|
|
|
|
|
dispatch(fetchUsersByPage(link, page));
|
2018-07-26 15:30:28 +02:00
|
|
|
},
|
|
|
|
|
fetchUsersByLink: (link: string) => {
|
|
|
|
|
dispatch(fetchUsersByLink(link));
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-07-24 16:04:55 +02:00
|
|
|
)(translate("users")(Users));
|