//@flow import React from "react"; import { connect } from "react-redux"; import { translate } from "react-i18next"; import type { History } from "history"; import type { Group, PagedCollection } from "@scm-manager/ui-types"; import { fetchGroupsByPage, getGroupsFromState, isFetchGroupsPending, getFetchGroupsFailure, isPermittedToCreateGroups, selectListAsCollection } from "../modules/groups"; import { Page, OverviewPageActions, Notification, LinkPaginator, urls } from "@scm-manager/ui-components"; import { GroupTable } from "./../components/table"; import CreateGroupButton from "../components/buttons/CreateGroupButton"; import { getGroupsLink } from "../../modules/indexResource"; type Props = { groups: Group[], loading: boolean, error: Error, canAddGroups: boolean, list: PagedCollection, page: number, groupLink: string, // context objects t: string => string, history: History, location: any, // dispatch functions fetchGroupsByPage: (link: string, page: number, filter?: string) => void }; class Groups extends React.Component { componentDidMount() { const { fetchGroupsByPage, groupLink, page, location } = this.props; fetchGroupsByPage( groupLink, page, urls.getQueryStringFromLocation(location) ); } componentDidUpdate = (prevProps: Props) => { const { loading, list, page, groupLink, location, fetchGroupsByPage } = this.props; if (list && page && !loading) { const statePage: number = list.page + 1; if (page !== statePage || prevProps.location.search !== location.search) { fetchGroupsByPage( groupLink, page, urls.getQueryStringFromLocation(location) ); } } }; render() { const { groups, loading, error, canAddGroups, t } = this.props; return ( {this.renderGroupTable()} {this.renderCreateButton()} ); } renderGroupTable() { const { groups, list, page, location, t } = this.props; if (groups && groups.length > 0) { return ( <> ); } return {t("groups.noGroups")}; } renderCreateButton() { if (this.props.canAddGroups) { return ; } return null; } } const mapStateToProps = (state, ownProps) => { const { match } = ownProps; const groups = getGroupsFromState(state); const loading = isFetchGroupsPending(state); const error = getFetchGroupsFailure(state); const page = urls.getPageFromMatch(match); const canAddGroups = isPermittedToCreateGroups(state); const list = selectListAsCollection(state); const groupLink = getGroupsLink(state); return { groups, loading, error, canAddGroups, list, page, groupLink }; }; const mapDispatchToProps = dispatch => { return { fetchGroupsByPage: (link: string, page: number, filter?: string) => { dispatch(fetchGroupsByPage(link, page, filter)); } }; }; export default connect( mapStateToProps, mapDispatchToProps )(translate("groups")(Groups));