Files
SCM-Manager/scm-ui/src/users/containers/Users.js

154 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-01-23 11:14:30 +01:00
// @flow
import React from "react";
import { connect } from "react-redux";
import { translate } from "react-i18next";
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";
import {
Page,
OverviewPageActions,
CreateButton,
2019-04-17 14:27:17 +02:00
Notification,
LinkPaginator,
urls
} from "@scm-manager/ui-components";
2019-01-23 11:14:30 +01:00
import { UserTable } from "./../components/table";
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,
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
};
class Users extends React.Component<Props> {
componentDidMount() {
const { fetchUsersByPage, usersLink, page, location } = this.props;
fetchUsersByPage(
usersLink,
page,
urls.getQueryStringFromLocation(location)
);
}
2019-01-23 11:14:30 +01:00
componentDidUpdate = (prevProps: Props) => {
const {
loading,
list,
page,
usersLink,
location,
fetchUsersByPage
} = this.props;
2019-04-17 15:46:49 +02:00
if (list && page && !loading) {
const statePage: number = list.page + 1;
if (page !== statePage || prevProps.location.search !== location.search) {
fetchUsersByPage(
usersLink,
page,
urls.getQueryStringFromLocation(location)
);
2019-01-23 11:14:30 +01:00
}
}
};
2019-01-23 11:14:30 +01:00
render() {
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()}
<OverviewPageActions
showCreateButton={canAddUsers}
link="users"
label={t("users.createButton")}
/>
2019-01-23 11:14:30 +01:00
</Page>
);
}
2019-04-09 14:59:19 +02:00
renderUserTable() {
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} />
<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() {
const { canAddUsers, t } = this.props;
if (canAddUsers) {
2019-02-26 10:34:56 +01:00
return <CreateButton label={t("users.createButton")} link="/users/add" />;
}
return null;
2019-02-26 10:34:56 +01:00
}
}
2019-01-23 11:14:30 +01:00
const mapStateToProps = (state, ownProps) => {
const { match } = ownProps;
2019-01-23 11:14:30 +01:00
const users = getUsersFromState(state);
const loading = isFetchUsersPending(state);
const error = getFetchUsersFailure(state);
const page = urls.getPageFromMatch(match);
2019-01-23 11:14:30 +01:00
const canAddUsers = isPermittedToCreateUsers(state);
const list = selectListAsCollection(state);
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) => {
dispatch(fetchUsersByPage(link, page, filter));
2019-01-23 11:14:30 +01:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("users")(Users));