Files
SCM-Manager/scm-ui/src/groups/containers/Groups.js

168 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-07-31 10:54:31 +02:00
//@flow
import React from "react";
2018-07-31 10:54:31 +02:00
import { connect } from "react-redux";
2018-07-31 13:04:09 +02:00
import { translate } from "react-i18next";
import type { Group } from "@scm-manager/ui-types";
import type { PagedCollection } from "@scm-manager/ui-types";
2018-07-31 13:04:09 +02:00
import type { History } from "history";
import {
Page,
PageActions,
Button,
Paginator
} from "@scm-manager/ui-components";
2018-07-31 13:04:09 +02:00
import { GroupTable } from "./../components/table";
2018-07-31 18:44:01 +02:00
import CreateGroupButton from "../components/buttons/CreateGroupButton";
2018-07-31 13:04:09 +02:00
import {
fetchGroupsByPage,
fetchGroupsByLink,
getGroupsFromState,
isFetchGroupsPending,
getFetchGroupsFailure,
isPermittedToCreateGroups,
selectListAsCollection
} from "../modules/groups";
2018-10-09 16:17:25 +02:00
import { getGroupsLink } from "../../modules/indexResource";
2018-07-31 13:04:09 +02:00
type Props = {
groups: Group[],
loading: boolean,
error: Error,
canAddGroups: boolean,
list: PagedCollection,
page: number,
2018-10-09 16:17:25 +02:00
groupLink: string,
2018-07-31 13:04:09 +02:00
// context objects
t: string => string,
history: History,
// dispatch functions
2018-10-09 16:17:25 +02:00
fetchGroupsByPage: (link: string, page: number) => void,
2018-07-31 13:04:09 +02:00
fetchGroupsByLink: (link: string) => void
};
class Groups extends React.Component<Props> {
2018-07-31 13:04:09 +02:00
componentDidMount() {
2018-10-09 16:17:25 +02:00
this.props.fetchGroupsByPage(this.props.groupLink, this.props.page);
2018-07-31 13:04:09 +02:00
}
onPageChange = (link: string) => {
this.props.fetchGroupsByLink(link);
};
/**
* reflect page transitions in the uri
*/
componentDidUpdate = (prevProps: Props) => {
const { page, list } = this.props;
2018-08-02 10:46:28 +02:00
if (list.page >= 0) {
2018-07-31 13:04:09 +02:00
// backend starts paging by 0
const statePage: number = list.page + 1;
if (page !== statePage) {
this.props.history.push(`/groups/${statePage}`);
}
}
};
render() {
2018-07-31 13:04:09 +02:00
const { groups, loading, error, t } = this.props;
return (
<Page
title={t("groups.title")}
subtitle={t("groups.subtitle")}
loading={loading || !groups}
error={error}
>
2018-07-31 18:44:01 +02:00
<GroupTable groups={groups} />
2018-07-31 13:04:09 +02:00
{this.renderPaginator()}
{this.renderCreateButton()}
2019-02-26 10:34:56 +01:00
{this.renderPageActionCreateButton()}
2018-07-31 13:04:09 +02:00
</Page>
);
}
renderPaginator() {
const { list } = this.props;
if (list) {
return <Paginator collection={list} onPageChange={this.onPageChange} />;
}
return null;
}
renderCreateButton() {
2018-07-31 18:44:01 +02:00
if (this.props.canAddGroups) {
return (
2019-02-26 10:34:56 +01:00
<CreateGroupButton />
);
} else {
return;
}
}
renderPageActionCreateButton() {
if (this.props.canAddGroups) {
return (
<PageActions>
<Button
label={this.props.t("create-group-button.label")}
link="/groups/add"
color="primary"
/>
</PageActions>
);
2018-07-31 13:04:09 +02:00
} else {
return;
2018-07-31 18:44:01 +02:00
}
}
2018-07-31 10:54:31 +02:00
}
2018-07-31 13:04:09 +02:00
const getPageFromProps = props => {
let page = props.match.params.page;
if (page) {
page = parseInt(page, 10);
} else {
page = 1;
}
return page;
};
const mapStateToProps = (state, ownProps) => {
const groups = getGroupsFromState(state);
const loading = isFetchGroupsPending(state);
const error = getFetchGroupsFailure(state);
const page = getPageFromProps(ownProps);
const canAddGroups = isPermittedToCreateGroups(state);
const list = selectListAsCollection(state);
2018-10-09 16:17:25 +02:00
const groupLink = getGroupsLink(state);
2018-07-31 13:04:09 +02:00
return {
groups,
loading,
error,
canAddGroups,
list,
2018-10-09 16:17:25 +02:00
page,
groupLink
2018-07-31 13:04:09 +02:00
};
2018-07-31 10:54:31 +02:00
};
2018-07-31 18:44:01 +02:00
const mapDispatchToProps = dispatch => {
2018-07-31 13:04:09 +02:00
return {
2018-10-09 16:17:25 +02:00
fetchGroupsByPage: (link: string, page: number) => {
dispatch(fetchGroupsByPage(link, page));
2018-07-31 13:04:09 +02:00
},
fetchGroupsByLink: (link: string) => {
dispatch(fetchGroupsByLink(link));
}
};
};
2018-07-31 10:54:31 +02:00
export default connect(
mapStateToProps,
mapDispatchToProps
2018-07-31 13:04:09 +02:00
)(translate("groups")(Groups));