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

195 lines
6.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";
import { NamespaceCollection, RepositoryCollection } from "@scm-manager/ui-types";
import {
CreateButton,
LinkPaginator,
Notification,
OverviewPageActions,
Page,
PageActions,
2020-08-11 10:34:29 +02:00
urls
} from "@scm-manager/ui-components";
import { getNamespacesLink, getRepositoriesLink } from "../../modules/indexResource";
import {
fetchNamespaces,
fetchReposByPage,
getFetchReposFailure,
getNamespaceCollection,
getRepositoryCollection,
isAbleToCreateRepos,
isFetchNamespacesPending,
isFetchReposPending
} from "../modules/repos";
import RepositoryList from "../components/list";
2018-07-31 16:32:16 +02:00
type Props = WithTranslation &
RouteComponentProps & {
loading: boolean;
error: Error;
showCreateButton: boolean;
collection: RepositoryCollection;
namespaces: NamespaceCollection;
page: number;
2020-09-04 17:13:25 +02:00
namespace: string;
reposLink: string;
namespacesLink: string;
2018-07-31 16:32:16 +02:00
// dispatched functions
2020-09-04 17:13:25 +02:00
fetchReposByPage: (link: string, page: number, namespace?: string, filter?: string) => void;
fetchNamespaces: (link: 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() {
const { fetchNamespaces, namespacesLink } = this.props;
fetchNamespaces(namespacesLink);
this.fetchRepos();
2018-07-31 16:32:16 +02:00
}
componentDidUpdate = (prevProps: Props) => {
const { loading, collection, namespace, namespaces, page, location } = this.props;
if (namespaces !== prevProps.namespaces && namespace) {
this.fetchRepos();
} else if (collection && (page || namespace) && !loading) {
2019-04-17 15:46:49 +02:00
const statePage: number = collection.page + 1;
if (page !== statePage || prevProps.location.search !== location.search || prevProps.namespace !== namespace) {
this.fetchRepos();
}
}
};
fetchRepos = () => {
2020-09-07 13:48:20 +02:00
const { page, location, fetchReposByPage } = this.props;
const link = this.getReposLink();
if (link) {
2020-09-07 13:48:20 +02:00
fetchReposByPage(link, page, urls.getQueryStringFromLocation(location));
}
};
getReposLink = () => {
const { namespace, namespaces, reposLink } = this.props;
if (namespace) {
return namespaces?.find(n => n.namespace === namespace)?._links?.repositories?.href;
} else {
return reposLink;
}
};
2018-07-31 16:32:16 +02:00
render() {
2020-09-04 17:13:25 +02:00
const { error, loading, showCreateButton, namespace, t } = this.props;
const link = namespace ? `repos/${namespace}` : "repos";
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>
2020-08-11 10:34:29 +02:00
<OverviewPageActions
showCreateButton={showCreateButton}
2020-09-04 17:13:25 +02:00
link={link}
2020-08-11 10:34:29 +02:00
label={t("overview.createButton")}
testId="repository-overview"
/>
</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;
2018-07-31 16:32:16 +02:00
const collection = getRepositoryCollection(state);
const namespaces = getNamespaceCollection(state);
const loading = isFetchReposPending(state) || isFetchNamespacesPending(state);
const error = getFetchReposFailure(state);
2020-09-04 17:13:25 +02:00
const { namespace, page } = urls.getNamespaceAndPageFromMatch(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);
const namespacesLink = getNamespacesLink(state);
2018-07-31 16:32:16 +02:00
return {
collection,
namespaces,
loading,
2018-08-03 08:52:02 +02:00
error,
page,
2020-09-04 17:13:25 +02:00
namespace,
showCreateButton,
reposLink,
namespacesLink
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 {
fetchReposByPage: (link: string, page: number, filter?: string) => {
dispatch(fetchReposByPage(link, page, filter));
},
fetchNamespaces: (link: string) => {
dispatch(fetchNamespaces(link));
}
2018-07-31 16:32:16 +02:00
};
};
2020-09-04 17:13:25 +02:00
2020-01-08 13:40:07 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("repos")(withRouter(Overview)));