2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { translate } from 'react-i18next';
|
|
|
|
|
import { History } from 'history';
|
|
|
|
|
import { Group, PagedCollection } from '@scm-manager/ui-types';
|
2018-07-31 13:04:09 +02:00
|
|
|
import {
|
|
|
|
|
fetchGroupsByPage,
|
|
|
|
|
getGroupsFromState,
|
|
|
|
|
isFetchGroupsPending,
|
|
|
|
|
getFetchGroupsFailure,
|
|
|
|
|
isPermittedToCreateGroups,
|
2019-10-19 16:38:07 +02:00
|
|
|
selectListAsCollection,
|
|
|
|
|
} from '../modules/groups';
|
2019-04-17 12:50:06 +02:00
|
|
|
import {
|
|
|
|
|
Page,
|
2019-04-24 09:46:22 +02:00
|
|
|
PageActions,
|
2019-04-19 13:22:17 +02:00
|
|
|
OverviewPageActions,
|
2019-04-17 14:27:17 +02:00
|
|
|
Notification,
|
2019-04-17 12:50:06 +02:00
|
|
|
LinkPaginator,
|
2019-04-24 09:46:22 +02:00
|
|
|
urls,
|
2019-10-19 16:38:07 +02:00
|
|
|
CreateButton,
|
|
|
|
|
} from '@scm-manager/ui-components';
|
|
|
|
|
import { GroupTable } from './../components/table';
|
|
|
|
|
import { getGroupsLink } from '../../modules/indexResource';
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
groups: Group[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
canAddGroups: boolean;
|
|
|
|
|
list: PagedCollection;
|
|
|
|
|
page: number;
|
|
|
|
|
groupLink: string;
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
// context objects
|
2019-10-19 16:38:07 +02:00
|
|
|
t: (p: string) => string;
|
|
|
|
|
history: History;
|
|
|
|
|
location: any;
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchGroupsByPage: (link: string, page: number, filter?: string) => void;
|
2018-07-31 13:04:09 +02:00
|
|
|
};
|
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-04-19 13:22:17 +02:00
|
|
|
fetchGroupsByPage(
|
|
|
|
|
groupLink,
|
|
|
|
|
page,
|
2019-10-19 16:38:07 +02:00
|
|
|
urls.getQueryStringFromLocation(location),
|
2019-04-19 13:22:17 +02:00
|
|
|
);
|
2019-04-17 12:13:04 +02:00
|
|
|
}
|
2018-07-31 13:04:09 +02:00
|
|
|
|
|
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
2019-04-17 16:35:17 +02:00
|
|
|
const {
|
2019-04-19 13:22:17 +02:00
|
|
|
loading,
|
2019-04-17 16:35:17 +02:00
|
|
|
list,
|
|
|
|
|
page,
|
2019-04-19 13:22:17 +02:00
|
|
|
groupLink,
|
2019-04-17 16:35:17 +02:00
|
|
|
location,
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchGroupsByPage,
|
2019-04-17 16:35:17 +02:00
|
|
|
} = this.props;
|
2019-04-17 15:46:49 +02:00
|
|
|
if (list && page && !loading) {
|
|
|
|
|
const statePage: number = list.page + 1;
|
2019-04-17 16:35:17 +02:00
|
|
|
if (page !== statePage || prevProps.location.search !== location.search) {
|
2019-04-19 13:22:17 +02:00
|
|
|
fetchGroupsByPage(
|
|
|
|
|
groupLink,
|
|
|
|
|
page,
|
2019-10-19 16:38:07 +02:00
|
|
|
urls.getQueryStringFromLocation(location),
|
2019-04-19 13:22:17 +02:00
|
|
|
);
|
2018-07-31 13:04:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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 (
|
|
|
|
|
<Page
|
2019-10-19 16:38:07 +02:00
|
|
|
title={t('groups.title')}
|
|
|
|
|
subtitle={t('groups.subtitle')}
|
2018-07-31 13:04:09 +02:00
|
|
|
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>
|
|
|
|
|
<OverviewPageActions
|
|
|
|
|
showCreateButton={canAddGroups}
|
2019-04-24 09:59:29 +02:00
|
|
|
link="groups"
|
2019-10-19 16:38:07 +02:00
|
|
|
label={t('create-group-button.label')}
|
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-04-19 13:22:17 +02:00
|
|
|
<LinkPaginator
|
|
|
|
|
collection={list}
|
|
|
|
|
page={page}
|
|
|
|
|
filter={urls.getQueryStringFromLocation(location)}
|
|
|
|
|
/>
|
2019-04-09 14:36:29 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-19 16:38:07 +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) {
|
|
|
|
|
return (
|
|
|
|
|
<CreateButton
|
2019-10-19 16:38:07 +02:00
|
|
|
label={t('create-group-button.label')}
|
2019-04-24 09:59:29 +02:00
|
|
|
link="/groups/create"
|
2019-04-24 09:46:22 +02:00
|
|
|
/>
|
|
|
|
|
);
|
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
|
|
|
|
|
|
|
|
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-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);
|
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,
|
2019-10-19 16:38:07 +02:00
|
|
|
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 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-19 16:38:07 +02:00
|
|
|
},
|
2018-07-31 13:04:09 +02:00
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
2019-04-17 15:46:49 +02:00
|
|
|
export default connect(
|
2019-04-17 16:35:17 +02:00
|
|
|
mapStateToProps,
|
2019-10-19 16:38:07 +02:00
|
|
|
mapDispatchToProps,
|
|
|
|
|
)(translate('groups')(Groups));
|