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";
|
|
|
|
|
import type { Group } from "../types/Group.js";
|
|
|
|
|
import type { PagedCollection } from "../../types/Collection";
|
|
|
|
|
import type { History } from "history";
|
|
|
|
|
import { Page } from "../../components/layout";
|
|
|
|
|
import { GroupTable } from "./../components/table";
|
|
|
|
|
import Paginator from "../../components/Paginator";
|
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";
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
groups: Group[],
|
|
|
|
|
loading: boolean,
|
|
|
|
|
error: Error,
|
|
|
|
|
canAddGroups: boolean,
|
|
|
|
|
list: PagedCollection,
|
|
|
|
|
page: number,
|
|
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
t: string => string,
|
|
|
|
|
history: History,
|
|
|
|
|
|
|
|
|
|
// dispatch functions
|
|
|
|
|
fetchGroupsByPage: (page: number) => void,
|
|
|
|
|
fetchGroupsByLink: (link: string) => void
|
|
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
|
|
|
|
|
class Groups extends React.Component<Props> {
|
2018-07-31 13:04:09 +02:00
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.fetchGroupsByPage(this.props.page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onPageChange = (link: string) => {
|
|
|
|
|
this.props.fetchGroupsByLink(link);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* reflect page transitions in the uri
|
|
|
|
|
*/
|
|
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
|
|
|
|
const { page, list } = this.props;
|
|
|
|
|
if (list.page) {
|
|
|
|
|
// backend starts paging by 0
|
|
|
|
|
const statePage: number = list.page + 1;
|
|
|
|
|
if (page !== statePage) {
|
|
|
|
|
this.props.history.push(`/groups/${statePage}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 10:16:18 +02:00
|
|
|
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()}
|
|
|
|
|
</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) {
|
2018-07-31 13:04:09 +02:00
|
|
|
return <CreateGroupButton />;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
2018-07-31 18:44:01 +02:00
|
|
|
}
|
2018-07-31 10:16:18 +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);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
groups,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
canAddGroups,
|
|
|
|
|
list,
|
|
|
|
|
page
|
|
|
|
|
};
|
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: (page: number) => {
|
|
|
|
|
dispatch(fetchGroupsByPage(page));
|
|
|
|
|
},
|
|
|
|
|
fetchGroupsByLink: (link: string) => {
|
|
|
|
|
dispatch(fetchGroupsByLink(link));
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-07-31 10:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
2018-07-31 10:54:31 +02:00
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-07-31 13:04:09 +02:00
|
|
|
)(translate("groups")(Groups));
|