// @flow import React from "react"; import { connect } from "react-redux"; import { translate } from "react-i18next"; import type { History } from "history"; import type { User, PagedCollection } from "@scm-manager/ui-types"; import { fetchUsersByPage, getUsersFromState, selectListAsCollection, isPermittedToCreateUsers, isFetchUsersPending, getFetchUsersFailure } from "../modules/users"; import { Page, OverviewPageActions, Notification, LinkPaginator, urls } from "@scm-manager/ui-components"; import { UserTable } from "./../components/table"; import CreateUserButton from "../components/buttons/CreateUserButton"; import { getUsersLink } from "../../modules/indexResource"; type Props = { users: User[], loading: boolean, error: Error, canAddUsers: boolean, list: PagedCollection, page: number, usersLink: string, // context objects t: string => string, history: History, location: any, // dispatch functions fetchUsersByPage: (link: string, page: number, filter?: string) => void }; class Users extends React.Component { componentDidMount() { const { fetchUsersByPage, usersLink, page, location } = this.props; fetchUsersByPage( usersLink, page, urls.getQueryStringFromLocation(location) ); } componentDidUpdate = (prevProps: Props) => { const { loading, list, page, usersLink, location, fetchUsersByPage } = this.props; if (list && page && !loading) { const statePage: number = list.page + 1; if (page !== statePage || prevProps.location.search !== location.search) { fetchUsersByPage( usersLink, page, urls.getQueryStringFromLocation(location) ); } } }; render() { const { users, loading, error, canAddUsers, t } = this.props; return ( {this.renderUserTable()} {this.renderCreateButton()} ); } renderUserTable() { const { users, list, page, location, t } = this.props; if (users && users.length > 0) { return ( <> ); } return {t("users.noUsers")}; } renderCreateButton() { if (this.props.canAddUsers) { return ; } return null; } } const mapStateToProps = (state, ownProps) => { const { match } = ownProps; const users = getUsersFromState(state); const loading = isFetchUsersPending(state); const error = getFetchUsersFailure(state); const page = urls.getPageFromMatch(match); const canAddUsers = isPermittedToCreateUsers(state); const list = selectListAsCollection(state); const usersLink = getUsersLink(state); return { users, loading, error, canAddUsers, list, page, usersLink }; }; const mapDispatchToProps = dispatch => { return { fetchUsersByPage: (link: string, page: number, filter?: string) => { dispatch(fetchUsersByPage(link, page, filter)); } }; }; export default connect( mapStateToProps, mapDispatchToProps )(translate("users")(Users));