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,
|
|
|
|
|
selectList
|
|
|
|
|
} from "../modules/users";
|
|
|
|
|
|
2018-07-25 14:29:43 +02:00
|
|
|
import { Page } from "../../components/layout";
|
2018-07-25 15:04:19 +02:00
|
|
|
import { UserTable } from "./../components/table";
|
2018-07-11 12:02:53 +02:00
|
|
|
import type { User } from "../types/User";
|
2018-07-25 14:29:43 +02:00
|
|
|
import { AddButton } from "../../components/buttons";
|
2018-07-17 11:29:58 +02:00
|
|
|
import type { UserEntry } from "../types/UserEntry";
|
2018-07-26 15:30:28 +02:00
|
|
|
import type { PagedCollection } from "../../types/Collection";
|
|
|
|
|
import Paginator from "../../components/Paginator";
|
|
|
|
|
import CreateUserButton from "../components/buttons/CreateUserButton";
|
2018-07-12 16:02:37 +02:00
|
|
|
|
2018-07-02 16:05:58 +02:00
|
|
|
type Props = {
|
2018-07-20 08:28:36 +02:00
|
|
|
loading?: boolean,
|
2018-07-11 12:02:53 +02:00
|
|
|
error: Error,
|
2018-07-24 16:04:55 +02:00
|
|
|
t: string => string,
|
2018-07-12 16:02:37 +02:00
|
|
|
userEntries: Array<UserEntry>,
|
2018-07-26 15:30:28 +02:00
|
|
|
fetchUsersByPage: (page: number) => void,
|
|
|
|
|
fetchUsersByLink: (link: string) => void,
|
|
|
|
|
canAddUsers: boolean,
|
|
|
|
|
list?: PagedCollection,
|
|
|
|
|
history: History,
|
|
|
|
|
match: any,
|
|
|
|
|
page: number
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-17 08:39:51 +02:00
|
|
|
class Users extends React.Component<Props, User> {
|
2018-07-11 12:02:53 +02:00
|
|
|
componentDidMount() {
|
2018-07-26 15:30:28 +02:00
|
|
|
this.props.fetchUsersByPage(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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
|
|
|
|
const { page, list } = this.props;
|
|
|
|
|
if (list) {
|
|
|
|
|
// backend starts with 0
|
|
|
|
|
const statePage: number = list.page + 1;
|
|
|
|
|
if (page !== statePage) {
|
|
|
|
|
this.props.history.push(`/users/${statePage}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-02 16:05:58 +02:00
|
|
|
render() {
|
2018-07-26 15:30:28 +02:00
|
|
|
const { userEntries, list, loading, t, error } = 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-20 08:28:36 +02:00
|
|
|
loading={loading || !userEntries}
|
|
|
|
|
error={error}
|
|
|
|
|
>
|
2018-07-25 13:21:49 +02:00
|
|
|
<UserTable entries={userEntries} />
|
2018-07-26 15:30:28 +02:00
|
|
|
{this.renderPaginator()}
|
2018-07-20 08:28:36 +02:00
|
|
|
{this.renderAddButton()}
|
|
|
|
|
</Page>
|
2018-07-18 09:35:16 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 15:30:28 +02:00
|
|
|
renderPaginator() {
|
|
|
|
|
const { list } = this.props;
|
|
|
|
|
if (list) {
|
|
|
|
|
return <Paginator collection={list} onPageChange={this.onPageChange} />;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 12:05:50 +02:00
|
|
|
renderAddButton() {
|
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-26 15:30:28 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
let page = ownProps.match.params.page;
|
|
|
|
|
if (page) {
|
|
|
|
|
page = parseInt(page, 10);
|
|
|
|
|
} else {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
2018-07-12 16:02:37 +02:00
|
|
|
const userEntries = getUsersFromState(state);
|
2018-07-19 11:23:20 +02:00
|
|
|
let error = null;
|
2018-07-20 08:28:36 +02:00
|
|
|
let loading = false;
|
2018-07-19 12:38:26 +02:00
|
|
|
let canAddUsers = false;
|
2018-07-25 14:34:00 +02:00
|
|
|
if (state.users && state.users.list) {
|
|
|
|
|
error = state.users.list.error;
|
|
|
|
|
canAddUsers = state.users.list.userCreatePermission;
|
|
|
|
|
loading = state.users.list.loading;
|
2018-07-19 11:23:20 +02:00
|
|
|
}
|
2018-07-26 15:30:28 +02:00
|
|
|
const list = selectList(state);
|
2018-07-10 08:38:38 +02:00
|
|
|
return {
|
2018-07-17 08:39:51 +02:00
|
|
|
userEntries,
|
2018-07-19 12:38:26 +02:00
|
|
|
error,
|
2018-07-20 08:28:36 +02:00
|
|
|
loading,
|
2018-07-26 15:30:28 +02:00
|
|
|
canAddUsers,
|
|
|
|
|
list,
|
|
|
|
|
page
|
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-07-26 15:30:28 +02:00
|
|
|
fetchUsersByPage: (page: number) => {
|
|
|
|
|
dispatch(fetchUsersByPage(page));
|
|
|
|
|
},
|
|
|
|
|
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));
|