2018-07-31 10:54:31 +02:00
|
|
|
//@flow
|
2018-07-31 10:16:18 +02:00
|
|
|
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";
|
2018-09-05 14:32:49 +02:00
|
|
|
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";
|
2019-04-17 12:13:04 +02:00
|
|
|
import queryString from "query-string";
|
2019-02-20 12:57:47 +01:00
|
|
|
import {
|
|
|
|
|
Page,
|
|
|
|
|
PageActions,
|
|
|
|
|
Button,
|
2019-04-17 12:13:04 +02:00
|
|
|
LinkPaginator,
|
|
|
|
|
getPageFromMatch
|
2019-02-20 12:57:47 +01:00
|
|
|
} 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 10:16:18 +02:00
|
|
|
|
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";
|
2019-04-17 12:13:04 +02:00
|
|
|
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,
|
2019-04-17 12:13:04 +02:00
|
|
|
location: any,
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2019-04-17 12:13:04 +02:00
|
|
|
fetchGroupsByPage: (link: string, page: number, filter?: any) => void,
|
2018-07-31 13:04:09 +02:00
|
|
|
fetchGroupsByLink: (link: string) => void
|
|
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
|
2019-04-17 12:13:04 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-04-17 12:13:04 +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) => {
|
2019-04-17 12:13:04 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 10:16:18 +02:00
|
|
|
render() {
|
2019-04-17 12:13:04 +02:00
|
|
|
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 => {
|
2019-04-17 12:13:04 +02:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 12:13:04 +02:00
|
|
|
renderPaginator = () => {
|
|
|
|
|
const { list, page } = this.props;
|
2018-07-31 13:04:09 +02:00
|
|
|
if (list) {
|
2019-04-17 12:13:04 +02:00
|
|
|
return (
|
|
|
|
|
<LinkPaginator
|
|
|
|
|
collection={list}
|
|
|
|
|
page={page}
|
|
|
|
|
filter={this.getQueryString()}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2018-07-31 13:04:09 +02:00
|
|
|
}
|
|
|
|
|
return null;
|
2019-04-17 12:13:04 +02:00
|
|
|
};
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
renderCreateButton() {
|
2018-07-31 18:44:01 +02:00
|
|
|
if (this.props.canAddGroups) {
|
2019-04-17 12:13:04 +02:00
|
|
|
return <CreateGroupButton />;
|
2019-02-26 10:34:56 +01:00
|
|
|
}
|
2019-04-17 12:13:04 +02:00
|
|
|
return null;
|
2019-02-26 10:34:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderPageActionCreateButton() {
|
2019-04-17 12:13:04 +02:00
|
|
|
const { canAddGroups, t } = this.props;
|
|
|
|
|
if (canAddGroups) {
|
2019-02-26 10:34:56 +01:00
|
|
|
return (
|
|
|
|
|
<PageActions>
|
|
|
|
|
<Button
|
2019-04-17 12:13:04 +02:00
|
|
|
label={t("create-group-button.label")}
|
2019-02-26 10:34:56 +01:00
|
|
|
link="/groups/add"
|
|
|
|
|
color="primary"
|
|
|
|
|
/>
|
|
|
|
|
</PageActions>
|
2019-02-25 14:16:45 +01:00
|
|
|
);
|
2018-07-31 18:44:01 +02:00
|
|
|
}
|
2019-04-17 12:13:04 +02:00
|
|
|
return null;
|
2018-07-31 10:16:18 +02:00
|
|
|
}
|
2018-07-31 10:54:31 +02:00
|
|
|
|
2019-04-17 12:13:04 +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) => {
|
2019-04-17 12:13:04 +02:00
|
|
|
const { match } = ownProps;
|
2018-07-31 13:04:09 +02:00
|
|
|
const groups = getGroupsFromState(state);
|
|
|
|
|
const loading = isFetchGroupsPending(state);
|
|
|
|
|
const error = getFetchGroupsFailure(state);
|
2019-04-17 12:13:04 +02:00
|
|
|
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 {
|
2019-04-17 12:13:04 +02:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
2019-04-17 12:13:04 +02:00
|
|
|
export default compose(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)
|
2018-07-31 13:04:09 +02:00
|
|
|
)(translate("groups")(Groups));
|