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

183 lines
4.2 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 queryString from "query-string";
import {
Page,
PageActions,
Button,
LinkPaginator,
getPageFromMatch
} 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";
import { compose } from "redux";
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,
location: any,
2018-07-31 13:04:09 +02:00
// dispatch functions
fetchGroupsByPage: (link: string, page: number, filter?: any) => void,
2018-07-31 13:04:09 +02:00
fetchGroupsByLink: (link: string) => void
};
type State = {
page: number
};
class Groups extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
page: -1
};
2018-07-31 13:04:09 +02:00
}
componentDidMount() {
const { fetchGroupsByPage, groupLink, page } = this.props;
fetchGroupsByPage(groupLink, page, this.getQueryString());
this.setState({ page: page });
}
2018-07-31 13:04:09 +02:00
componentDidUpdate = (prevProps: Props) => {
const { list, page, location, fetchGroupsByPage, groupLink } = this.props;
if (list && page) {
if (
page !== this.state.page ||
prevProps.location.search !== location.search
) {
fetchGroupsByPage(groupLink, page, this.getQueryString());
this.setState({ page: page });
2018-07-31 13:04:09 +02:00
}
}
};
render() {
const { groups, loading, error, history, t } = this.props;
2018-07-31 13:04:09 +02:00
return (
<Page
title={t("groups.title")}
subtitle={t("groups.subtitle")}
loading={loading || !groups}
error={error}
2019-04-10 17:24:35 +02:00
filter={filter => {
history.push("/groups/?q=" + filter);
2019-04-10 17:24:35 +02:00
}}
2018-07-31 13:04:09 +02:00
>
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, page } = this.props;
2018-07-31 13:04:09 +02:00
if (list) {
return (
<LinkPaginator
collection={list}
page={page}
filter={this.getQueryString()}
/>
);
2018-07-31 13:04:09 +02:00
}
return null;
};
2018-07-31 13:04:09 +02:00
renderCreateButton() {
2018-07-31 18:44:01 +02:00
if (this.props.canAddGroups) {
return <CreateGroupButton />;
2019-02-26 10:34:56 +01:00
}
return null;
2019-02-26 10:34:56 +01:00
}
renderPageActionCreateButton() {
const { canAddGroups, t } = this.props;
if (canAddGroups) {
2019-02-26 10:34:56 +01:00
return (
<PageActions>
<Button
label={t("create-group-button.label")}
2019-02-26 10:34:56 +01:00
link="/groups/add"
color="primary"
/>
</PageActions>
);
2018-07-31 18:44:01 +02:00
}
return null;
}
2018-07-31 10:54:31 +02:00
getQueryString = () => {
const { location } = this.props;
return location.search ? queryString.parse(location.search).q : null;
};
}
2018-07-31 13:04:09 +02:00
const mapStateToProps = (state, ownProps) => {
const { match } = ownProps;
2018-07-31 13:04:09 +02:00
const groups = getGroupsFromState(state);
const loading = isFetchGroupsPending(state);
const error = getFetchGroupsFailure(state);
const page = getPageFromMatch(match);
2018-07-31 13:04:09 +02:00
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 {
fetchGroupsByPage: (link: string, page: number, filter?: any) => {
2019-04-10 17:24:35 +02:00
dispatch(fetchGroupsByPage(link, page, filter));
2018-07-31 13:04:09 +02:00
},
fetchGroupsByLink: (link: string) => {
dispatch(fetchGroupsByLink(link));
}
};
};
export default compose(
connect(
mapStateToProps,
mapDispatchToProps
)
2018-07-31 13:04:09 +02:00
)(translate("groups")(Groups));