2019-01-23 11:14:30 +01:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { translate } from "react-i18next";
|
2019-04-17 12:50:06 +02:00
|
|
|
import type { History } from "history";
|
|
|
|
|
import type { User, PagedCollection } from "@scm-manager/ui-types";
|
2019-01-23 11:14:30 +01:00
|
|
|
import {
|
|
|
|
|
fetchUsersByPage,
|
|
|
|
|
getUsersFromState,
|
|
|
|
|
selectListAsCollection,
|
|
|
|
|
isPermittedToCreateUsers,
|
|
|
|
|
isFetchUsersPending,
|
|
|
|
|
getFetchUsersFailure
|
|
|
|
|
} from "../modules/users";
|
2019-02-20 12:57:47 +01:00
|
|
|
import {
|
|
|
|
|
Page,
|
2019-04-19 13:22:17 +02:00
|
|
|
OverviewPageActions,
|
2019-04-17 14:27:17 +02:00
|
|
|
Notification,
|
2019-04-17 12:50:06 +02:00
|
|
|
LinkPaginator,
|
2019-04-19 13:22:17 +02:00
|
|
|
urls
|
2019-02-20 12:57:47 +01:00
|
|
|
} from "@scm-manager/ui-components";
|
2019-01-23 11:14:30 +01:00
|
|
|
import { UserTable } from "./../components/table";
|
2019-04-23 15:55:26 +02:00
|
|
|
import CreateUserButton from "../components/buttons/CreateUserButton";
|
2019-01-23 11:14:30 +01:00
|
|
|
import { getUsersLink } from "../../modules/indexResource";
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
users: User[],
|
|
|
|
|
loading: boolean,
|
|
|
|
|
error: Error,
|
|
|
|
|
canAddUsers: boolean,
|
|
|
|
|
list: PagedCollection,
|
|
|
|
|
page: number,
|
|
|
|
|
usersLink: string,
|
|
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
t: string => string,
|
|
|
|
|
history: History,
|
2019-04-17 12:50:06 +02:00
|
|
|
location: any,
|
2019-01-23 11:14:30 +01:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2019-04-18 10:02:22 +02:00
|
|
|
fetchUsersByPage: (link: string, page: number, filter?: string) => void
|
2019-01-23 11:14:30 +01:00
|
|
|
};
|
|
|
|
|
|
2019-04-17 16:35:17 +02:00
|
|
|
class Users extends React.Component<Props> {
|
2019-04-17 12:50:06 +02:00
|
|
|
componentDidMount() {
|
2019-04-18 11:42:09 +02:00
|
|
|
const { fetchUsersByPage, usersLink, page, location } = this.props;
|
2019-04-19 13:22:17 +02:00
|
|
|
fetchUsersByPage(
|
|
|
|
|
usersLink,
|
|
|
|
|
page,
|
|
|
|
|
urls.getQueryStringFromLocation(location)
|
|
|
|
|
);
|
2019-04-17 12:50:06 +02:00
|
|
|
}
|
2019-01-23 11:14:30 +01:00
|
|
|
|
2019-04-17 12:50:06 +02:00
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
2019-04-17 16:35:17 +02:00
|
|
|
const {
|
2019-04-19 13:22:17 +02:00
|
|
|
loading,
|
2019-04-17 16:35:17 +02:00
|
|
|
list,
|
|
|
|
|
page,
|
2019-04-19 13:22:17 +02:00
|
|
|
usersLink,
|
2019-04-17 16:35:17 +02:00
|
|
|
location,
|
2019-04-19 13:22:17 +02:00
|
|
|
fetchUsersByPage
|
2019-04-17 16:35:17 +02:00
|
|
|
} = this.props;
|
2019-04-17 15:46:49 +02:00
|
|
|
if (list && page && !loading) {
|
|
|
|
|
const statePage: number = list.page + 1;
|
2019-04-17 16:35:17 +02:00
|
|
|
if (page !== statePage || prevProps.location.search !== location.search) {
|
2019-04-19 13:22:17 +02:00
|
|
|
fetchUsersByPage(
|
|
|
|
|
usersLink,
|
|
|
|
|
page,
|
|
|
|
|
urls.getQueryStringFromLocation(location)
|
|
|
|
|
);
|
2019-01-23 11:14:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 12:50:06 +02:00
|
|
|
};
|
2019-01-23 11:14:30 +01:00
|
|
|
|
|
|
|
|
render() {
|
2019-04-19 13:22:17 +02:00
|
|
|
const { users, loading, error, canAddUsers, t } = this.props;
|
2019-01-23 11:14:30 +01:00
|
|
|
return (
|
|
|
|
|
<Page
|
|
|
|
|
title={t("users.title")}
|
|
|
|
|
subtitle={t("users.subtitle")}
|
|
|
|
|
loading={loading || !users}
|
|
|
|
|
error={error}
|
|
|
|
|
>
|
2019-04-09 14:59:19 +02:00
|
|
|
{this.renderUserTable()}
|
2019-01-23 11:14:30 +01:00
|
|
|
{this.renderCreateButton()}
|
2019-04-19 13:22:17 +02:00
|
|
|
<OverviewPageActions
|
|
|
|
|
showCreateButton={canAddUsers}
|
2019-04-23 15:55:26 +02:00
|
|
|
link="users/add"
|
2019-04-19 13:22:17 +02:00
|
|
|
label={t("users.createButton")}
|
|
|
|
|
/>
|
2019-01-23 11:14:30 +01:00
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 14:59:19 +02:00
|
|
|
renderUserTable() {
|
2019-04-19 13:22:17 +02:00
|
|
|
const { users, list, page, location, t } = this.props;
|
2019-04-09 14:59:19 +02:00
|
|
|
if (users && users.length > 0) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<UserTable users={users} />
|
2019-04-19 13:22:17 +02:00
|
|
|
<LinkPaginator
|
|
|
|
|
collection={list}
|
|
|
|
|
page={page}
|
|
|
|
|
filter={urls.getQueryStringFromLocation(location)}
|
|
|
|
|
/>
|
2019-04-09 14:59:19 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return <Notification type="info">{t("users.noUsers")}</Notification>;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 11:14:30 +01:00
|
|
|
renderCreateButton() {
|
2019-04-23 15:55:26 +02:00
|
|
|
if (this.props.canAddUsers) {
|
|
|
|
|
return <CreateUserButton />;
|
2019-02-26 10:34:56 +01:00
|
|
|
}
|
2019-04-17 12:50:06 +02:00
|
|
|
return null;
|
2019-02-26 10:34:56 +01:00
|
|
|
}
|
2019-04-17 12:50:06 +02:00
|
|
|
}
|
2019-01-23 11:14:30 +01:00
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2019-04-17 12:50:06 +02:00
|
|
|
const { match } = ownProps;
|
2019-01-23 11:14:30 +01:00
|
|
|
const users = getUsersFromState(state);
|
|
|
|
|
const loading = isFetchUsersPending(state);
|
|
|
|
|
const error = getFetchUsersFailure(state);
|
2019-04-18 11:42:09 +02:00
|
|
|
const page = urls.getPageFromMatch(match);
|
2019-01-23 11:14:30 +01:00
|
|
|
const canAddUsers = isPermittedToCreateUsers(state);
|
|
|
|
|
const list = selectListAsCollection(state);
|
2019-04-17 12:50:06 +02:00
|
|
|
const usersLink = getUsersLink(state);
|
2019-01-23 11:14:30 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
users,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
canAddUsers,
|
|
|
|
|
list,
|
|
|
|
|
page,
|
|
|
|
|
usersLink
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
2019-04-17 16:42:27 +02:00
|
|
|
fetchUsersByPage: (link: string, page: number, filter?: string) => {
|
2019-04-10 14:35:30 +02:00
|
|
|
dispatch(fetchUsersByPage(link, page, filter));
|
2019-01-23 11:14:30 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2019-04-19 13:22:17 +02:00
|
|
|
)(translate("users")(Users));
|