Files
SCM-Manager/scm-ui/ui-webapp/src/repos/containers/Overview.tsx

163 lines
5.1 KiB
TypeScript
Raw Normal View History

/*
* 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.
*/
import React from "react";
import { connect } from "react-redux";
import { RouteComponentProps, withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
2020-08-04 11:41:00 +02:00
import { Me, RepositoryCollection } from "@scm-manager/ui-types";
import {
CreateButton,
LinkPaginator,
Notification,
OverviewPageActions,
Page,
PageActions,
urls,
Loading
} from "@scm-manager/ui-components";
import { getRepositoriesLink } from "../../modules/indexResource";
import {
fetchReposByPage,
getFetchReposFailure,
getRepositoryCollection,
isAbleToCreateRepos,
isFetchReposPending
} from "../modules/repos";
import RepositoryList from "../components/list";
2020-08-04 11:41:00 +02:00
import { fetchMe, isFetchMePending } from "../../modules/auth";
2018-07-31 16:32:16 +02:00
type Props = WithTranslation &
RouteComponentProps & {
2020-08-04 11:41:00 +02:00
me: Me;
loading: boolean;
error: Error;
showCreateButton: boolean;
collection: RepositoryCollection;
page: number;
reposLink: string;
2018-07-31 16:32:16 +02:00
// dispatched functions
fetchReposByPage: (link: string, page: number, filter?: string) => void;
};
2018-08-03 08:52:02 +02:00
2019-04-17 15:46:49 +02:00
class Overview extends React.Component<Props> {
2018-07-31 16:32:16 +02:00
componentDidMount() {
2020-08-04 11:41:00 +02:00
const { me, fetchReposByPage, reposLink, page, location } = this.props;
if (me) {
fetchReposByPage(reposLink, page, urls.getQueryStringFromLocation(location));
}
2018-07-31 16:32:16 +02:00
}
componentDidUpdate = (prevProps: Props) => {
2020-08-04 11:41:00 +02:00
const { me, loading, collection, page, reposLink, location, fetchReposByPage } = this.props;
if (collection && page && !loading && me) {
2019-04-17 15:46:49 +02:00
const statePage: number = collection.page + 1;
if (page !== statePage || prevProps.location.search !== location.search) {
2019-10-21 10:57:56 +02:00
fetchReposByPage(reposLink, page, urls.getQueryStringFromLocation(location));
}
}
};
2018-07-31 16:32:16 +02:00
render() {
const { error, loading, showCreateButton, t } = this.props;
if (loading) {
return <Loading />;
}
2018-07-31 16:32:16 +02:00
return (
2019-10-21 10:57:56 +02:00
<Page title={t("overview.title")} subtitle={t("overview.subtitle")} loading={loading} error={error}>
{this.renderOverview()}
<PageActions>
2019-10-21 10:57:56 +02:00
<OverviewPageActions showCreateButton={showCreateButton} link="repos" label={t("overview.createButton")} />
</PageActions>
2018-07-31 16:32:16 +02:00
</Page>
);
}
renderRepositoryList() {
const { collection, page, location, t } = this.props;
if (collection._embedded && collection._embedded.repositories.length > 0) {
2018-07-31 16:32:16 +02:00
return (
<>
<RepositoryList repositories={collection._embedded.repositories} />
2019-10-21 10:57:56 +02:00
<LinkPaginator collection={collection} page={page} filter={urls.getQueryStringFromLocation(location)} />
</>
);
}
2019-10-21 10:57:56 +02:00
return <Notification type="info">{t("overview.noRepositories")}</Notification>;
}
renderOverview() {
const { collection } = this.props;
if (collection) {
return (
2019-04-17 14:27:17 +02:00
<>
{this.renderRepositoryList()}
2018-08-03 08:52:02 +02:00
{this.renderCreateButton()}
</>
2018-07-31 16:32:16 +02:00
);
}
return null;
}
2018-08-03 08:52:02 +02:00
renderCreateButton() {
const { showCreateButton, t } = this.props;
if (showCreateButton) {
2019-10-21 10:57:56 +02:00
return <CreateButton label={t("overview.createButton")} link="/repos/create" />;
2019-02-26 10:34:56 +01:00
}
return null;
}
}
2020-01-08 13:40:07 +01:00
const mapStateToProps = (state: any, ownProps: Props) => {
const { match } = ownProps;
2020-08-04 11:41:00 +02:00
const me = fetchMe(state);
2018-07-31 16:32:16 +02:00
const collection = getRepositoryCollection(state);
2020-08-04 11:41:00 +02:00
const loading = isFetchReposPending(state) || isFetchMePending(state);
const error = getFetchReposFailure(state);
const page = urls.getPageFromMatch(match);
2018-08-03 08:52:02 +02:00
const showCreateButton = isAbleToCreateRepos(state);
2018-10-09 16:46:39 +02:00
const reposLink = getRepositoriesLink(state);
2018-07-31 16:32:16 +02:00
return {
2020-08-04 11:41:00 +02:00
me,
collection,
loading,
2018-08-03 08:52:02 +02:00
error,
page,
showCreateButton,
reposLink
2018-07-31 16:32:16 +02:00
};
};
2020-01-08 13:40:07 +01:00
const mapDispatchToProps = (dispatch: any) => {
2018-07-31 16:32:16 +02:00
return {
2019-04-17 16:42:27 +02:00
fetchReposByPage: (link: string, page: number, filter?: string) => {
2019-04-10 17:25:36 +02:00
dispatch(fetchReposByPage(link, page, filter));
}
2018-07-31 16:32:16 +02:00
};
};
2020-01-08 13:40:07 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("repos")(withRouter(Overview)));