2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2020-01-08 13:40:07 +01:00
|
|
|
import { RouteComponentProps } from "react-router-dom";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { Group, PagedCollection } from "@scm-manager/ui-types";
|
2019-04-17 12:50:06 +02:00
|
|
|
import {
|
2020-01-08 15:57:13 +01:00
|
|
|
CreateButton,
|
|
|
|
|
LinkPaginator,
|
|
|
|
|
Notification,
|
|
|
|
|
OverviewPageActions,
|
2019-04-17 12:50:06 +02:00
|
|
|
Page,
|
2019-04-24 09:46:22 +02:00
|
|
|
PageActions,
|
2020-08-11 10:34:29 +02:00
|
|
|
urls
|
2019-10-20 18:02:52 +02:00
|
|
|
} from "@scm-manager/ui-components";
|
2020-08-27 13:20:43 +02:00
|
|
|
import { mustGetGroupsLink } from "../../modules/indexResource";
|
2019-10-23 15:47:08 +02:00
|
|
|
import {
|
|
|
|
|
fetchGroupsByPage,
|
2020-01-08 15:57:13 +01:00
|
|
|
getFetchGroupsFailure,
|
2019-10-23 15:47:08 +02:00
|
|
|
getGroupsFromState,
|
|
|
|
|
isFetchGroupsPending,
|
|
|
|
|
isPermittedToCreateGroups,
|
|
|
|
|
selectListAsCollection
|
|
|
|
|
} from "../modules/groups";
|
|
|
|
|
import { GroupTable } from "./../components/table";
|
2018-07-31 13:04:09 +02:00
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
type Props = RouteComponentProps &
|
|
|
|
|
WithTranslation & {
|
|
|
|
|
groups: Group[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
canAddGroups: boolean;
|
|
|
|
|
list: PagedCollection;
|
|
|
|
|
page: number;
|
|
|
|
|
groupLink: string;
|
2018-07-31 13:04:09 +02:00
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
// dispatch functions
|
|
|
|
|
fetchGroupsByPage: (link: string, page: number, filter?: string) => void;
|
|
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
|
2019-04-17 15:46:49 +02:00
|
|
|
class Groups extends React.Component<Props> {
|
2019-04-17 12:13:04 +02:00
|
|
|
componentDidMount() {
|
2019-04-18 11:42:09 +02:00
|
|
|
const { fetchGroupsByPage, groupLink, page, location } = this.props;
|
2019-10-21 10:57:56 +02:00
|
|
|
fetchGroupsByPage(groupLink, page, urls.getQueryStringFromLocation(location));
|
2019-04-17 12:13:04 +02:00
|
|
|
}
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
2019-10-21 10:57:56 +02:00
|
|
|
const { loading, list, page, groupLink, location, fetchGroupsByPage } = this.props;
|
2019-04-17 15:46:49 +02:00
|
|
|
if (list && page && !loading) {
|
2019-11-20 11:15:36 +01:00
|
|
|
const statePage: number = this.resolveStatePage();
|
2019-04-17 16:35:17 +02:00
|
|
|
if (page !== statePage || prevProps.location.search !== location.search) {
|
2019-10-21 10:57:56 +02:00
|
|
|
fetchGroupsByPage(groupLink, page, urls.getQueryStringFromLocation(location));
|
2018-07-31 13:04:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 11:15:36 +01:00
|
|
|
resolveStatePage = () => {
|
|
|
|
|
const { list } = this.props;
|
|
|
|
|
if (list.page) {
|
|
|
|
|
return list.page + 1;
|
|
|
|
|
}
|
|
|
|
|
// set page to 1 if undefined, because if groups couldn't be fetched it would lead to an fetch-loop otherwise
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 10:16:18 +02:00
|
|
|
render() {
|
2019-04-19 13:22:17 +02:00
|
|
|
const { groups, loading, error, canAddGroups, t } = this.props;
|
2018-07-31 13:04:09 +02:00
|
|
|
return (
|
2019-10-21 10:57:56 +02:00
|
|
|
<Page title={t("groups.title")} subtitle={t("groups.subtitle")} loading={loading || !groups} error={error}>
|
2019-04-09 14:36:29 +02:00
|
|
|
{this.renderGroupTable()}
|
2018-07-31 13:04:09 +02:00
|
|
|
{this.renderCreateButton()}
|
2019-04-24 09:46:22 +02:00
|
|
|
<PageActions>
|
2020-11-23 15:48:20 +01:00
|
|
|
<OverviewPageActions
|
|
|
|
|
showCreateButton={canAddGroups}
|
|
|
|
|
link="groups"
|
|
|
|
|
label={t("create-group-button.label")}
|
|
|
|
|
searchPlaceholder={t("overview.searchGroup")}
|
|
|
|
|
/>
|
2019-04-24 09:46:22 +02:00
|
|
|
</PageActions>
|
2018-07-31 13:04:09 +02:00
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 14:36:29 +02:00
|
|
|
renderGroupTable() {
|
2019-04-19 13:22:17 +02:00
|
|
|
const { groups, list, page, location, t } = this.props;
|
2019-04-09 14:36:29 +02:00
|
|
|
if (groups && groups.length > 0) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<GroupTable groups={groups} />
|
2019-10-21 10:57:56 +02:00
|
|
|
<LinkPaginator collection={list} page={page} filter={urls.getQueryStringFromLocation(location)} />
|
2019-04-09 14:36:29 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-20 18:02:52 +02:00
|
|
|
return <Notification type="info">{t("groups.noGroups")}</Notification>;
|
2019-04-09 14:36:29 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 13:04:09 +02:00
|
|
|
renderCreateButton() {
|
2019-04-24 09:46:22 +02:00
|
|
|
const { canAddGroups, t } = this.props;
|
|
|
|
|
if (canAddGroups) {
|
2019-10-21 10:57:56 +02:00
|
|
|
return <CreateButton label={t("create-group-button.label")} link="/groups/create" />;
|
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
|
|
|
}
|
2019-04-17 12:13:04 +02:00
|
|
|
}
|
2018-07-31 13:04:09 +02:00
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
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-18 11:42:09 +02:00
|
|
|
const page = urls.getPageFromMatch(match);
|
2018-07-31 13:04:09 +02:00
|
|
|
const canAddGroups = isPermittedToCreateGroups(state);
|
|
|
|
|
const list = selectListAsCollection(state);
|
2020-08-27 13:20:43 +02:00
|
|
|
const groupLink = mustGetGroupsLink(state);
|
2018-10-09 16:17:25 +02:00
|
|
|
|
2018-07-31 13:04:09 +02:00
|
|
|
return {
|
|
|
|
|
groups,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
canAddGroups,
|
|
|
|
|
list,
|
2018-10-09 16:17:25 +02:00
|
|
|
page,
|
2019-10-20 18:02:52 +02:00
|
|
|
groupLink
|
2018-07-31 13:04:09 +02:00
|
|
|
};
|
2018-07-31 10:54:31 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2018-07-31 13:04:09 +02:00
|
|
|
return {
|
2019-04-17 16:42:27 +02:00
|
|
|
fetchGroupsByPage: (link: string, page: number, filter?: string) => {
|
2019-04-10 17:24:35 +02:00
|
|
|
dispatch(fetchGroupsByPage(link, page, filter));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
2018-07-31 13:04:09 +02:00
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("groups")(Groups));
|