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

190 lines
4.4 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 classNames from "classnames";
import injectSheet from "react-jss";
2019-01-23 11:14:30 +01:00
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,
PageActions,
Button,
CreateButton,
2019-04-17 14:27:17 +02:00
Notification,
LinkPaginator,
urls,
FilterInput
} 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
classes: Object,
2019-01-23 11:14:30 +01:00
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
};
const styles = {
button: {
float: "right",
marginTop: "1.25rem"
}
};
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 {
list,
page,
loading,
location,
fetchUsersByPage,
usersLink
} = 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, 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()}
{this.renderPageActions()}
2019-01-23 11:14:30 +01:00
</Page>
);
}
2019-04-09 14:59:19 +02:00
renderUserTable() {
const { users, t } = this.props;
if (users && users.length > 0) {
return (
<>
<UserTable users={users} />
{this.renderPaginator()}
</>
);
}
return <Notification type="info">{t("users.noUsers")}</Notification>;
}
renderPaginator = () => {
const { list, page, location } = this.props;
2019-01-23 11:14:30 +01:00
if (list) {
return (
<LinkPaginator
collection={list}
page={page}
filter={urls.getQueryStringFromLocation(location)}
/>
);
2019-01-23 11:14:30 +01:00
}
return null;
};
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
}
renderPageActions() {
const { canAddUsers, history, classes, location, t } = this.props;
if (canAddUsers) {
return (
2019-02-26 10:34:56 +01:00
<PageActions>
<FilterInput
value={urls.getQueryStringFromLocation(location)}
filter={filter => {
history.push("/users/?q=" + filter);
}}
2019-02-26 10:34:56 +01:00
/>
<div className={classNames(classes.button, "input-button control")}>
<Button
label={t("users.createButton")}
link="/users/add"
color="primary"
/>
</div>
2019-02-26 10:34:56 +01:00
</PageActions>
);
2019-01-23 11:14:30 +01:00
}
return null;
2019-01-23 11:14:30 +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
)(injectSheet(styles)(translate("users")(Users)));