2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { RouteComponentProps, withRouter } from "react-router-dom";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2020-09-04 21:44:48 +02:00
|
|
|
import { NamespaceCollection, RepositoryCollection } from "@scm-manager/ui-types";
|
2019-02-20 11:53:38 +01:00
|
|
|
import {
|
|
|
|
|
CreateButton,
|
2019-04-17 12:37:48 +02:00
|
|
|
LinkPaginator,
|
2020-01-08 15:57:13 +01:00
|
|
|
Notification,
|
|
|
|
|
OverviewPageActions,
|
|
|
|
|
Page,
|
|
|
|
|
PageActions,
|
2020-08-11 10:34:29 +02:00
|
|
|
urls
|
2019-10-20 18:02:52 +02:00
|
|
|
} from "@scm-manager/ui-components";
|
2020-09-04 21:44:48 +02:00
|
|
|
import { getNamespacesLink, getRepositoriesLink } from "../../modules/indexResource";
|
2019-10-23 15:47:08 +02:00
|
|
|
import {
|
2020-09-07 09:00:10 +02:00
|
|
|
fetchNamespaces,
|
2019-10-23 15:47:08 +02:00
|
|
|
fetchReposByPage,
|
|
|
|
|
getFetchReposFailure,
|
2020-09-07 09:00:10 +02:00
|
|
|
getNamespaceCollection,
|
2019-10-23 15:47:08 +02:00
|
|
|
getRepositoryCollection,
|
|
|
|
|
isAbleToCreateRepos,
|
2020-09-04 21:44:48 +02:00
|
|
|
isFetchNamespacesPending,
|
2020-09-07 09:00:10 +02:00
|
|
|
isFetchReposPending
|
2019-10-23 15:47:08 +02:00
|
|
|
} from "../modules/repos";
|
|
|
|
|
import RepositoryList from "../components/list";
|
2018-07-31 16:32:16 +02:00
|
|
|
|
2020-01-08 15:57:13 +01:00
|
|
|
type Props = WithTranslation &
|
|
|
|
|
RouteComponentProps & {
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
showCreateButton: boolean;
|
|
|
|
|
collection: RepositoryCollection;
|
2020-09-04 21:44:48 +02:00
|
|
|
namespaces: NamespaceCollection;
|
2020-01-08 15:57:13 +01:00
|
|
|
page: number;
|
2020-09-04 17:13:25 +02:00
|
|
|
namespace: string;
|
2020-01-08 15:57:13 +01:00
|
|
|
reposLink: string;
|
2020-09-04 21:44:48 +02:00
|
|
|
namespacesLink: string;
|
2018-07-31 16:32:16 +02:00
|
|
|
|
2020-01-08 15:57:13 +01:00
|
|
|
// dispatched functions
|
2020-09-04 17:13:25 +02:00
|
|
|
fetchReposByPage: (link: string, page: number, namespace?: string, filter?: string) => void;
|
2020-09-04 21:44:48 +02:00
|
|
|
fetchNamespaces: (link: string) => void;
|
2020-01-08 15:57:13 +01:00
|
|
|
};
|
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-09-07 09:00:10 +02:00
|
|
|
const { fetchNamespaces, namespacesLink } = this.props;
|
2020-09-04 21:44:48 +02:00
|
|
|
fetchNamespaces(namespacesLink);
|
2020-09-07 09:00:10 +02:00
|
|
|
this.fetchRepos();
|
2018-07-31 16:32:16 +02:00
|
|
|
}
|
2018-08-01 14:56:24 +02:00
|
|
|
|
2019-04-17 12:37:48 +02:00
|
|
|
componentDidUpdate = (prevProps: Props) => {
|
2020-09-07 09:00:10 +02:00
|
|
|
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;
|
2020-09-07 09:00:10 +02:00
|
|
|
if (page !== statePage || prevProps.location.search !== location.search || prevProps.namespace !== namespace) {
|
|
|
|
|
this.fetchRepos();
|
2018-08-01 14:56:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 12:37:48 +02:00
|
|
|
};
|
2018-08-01 14:56:24 +02:00
|
|
|
|
2020-09-07 09:00:10 +02:00
|
|
|
fetchRepos = () => {
|
2020-09-07 13:48:20 +02:00
|
|
|
const { page, location, fetchReposByPage } = this.props;
|
2020-09-07 09:00:10 +02:00
|
|
|
const link = this.getReposLink();
|
|
|
|
|
if (link) {
|
2020-09-07 13:48:20 +02:00
|
|
|
fetchReposByPage(link, page, urls.getQueryStringFromLocation(location));
|
2020-09-07 09:00:10 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-04 21:44:48 +02:00
|
|
|
getReposLink = () => {
|
|
|
|
|
const { namespace, namespaces, reposLink } = this.props;
|
|
|
|
|
if (namespace) {
|
2020-09-10 08:37:37 +02:00
|
|
|
return namespaces?._embedded.namespaces.find(n => n.namespace === namespace)?._links?.repositories?.href;
|
2020-09-04 21:44:48 +02:00
|
|
|
} else {
|
|
|
|
|
return reposLink;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-23 15:48:20 +01:00
|
|
|
getNamespaceFilterPlaceholder = () => {
|
|
|
|
|
return this.props.t("overview.allNamespaces");
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 14:16:24 +02:00
|
|
|
namespaceSelected = (newNamespace: string) => {
|
2020-11-23 15:48:20 +01:00
|
|
|
if (newNamespace === this.getNamespaceFilterPlaceholder()) {
|
2020-09-07 14:16:24 +02:00
|
|
|
this.props.history.push("/repos/");
|
|
|
|
|
} else {
|
|
|
|
|
this.props.history.push(`/repos/${newNamespace}/`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
render() {
|
2020-09-07 14:16:24 +02:00
|
|
|
const { error, loading, showCreateButton, namespace, namespaces, t } = this.props;
|
2020-11-23 15:48:20 +01:00
|
|
|
const namespaceFilterPlaceholder = this.getNamespaceFilterPlaceholder();
|
|
|
|
|
const namespacesToRender = namespaces
|
|
|
|
|
? [namespaceFilterPlaceholder, ...namespaces._embedded.namespaces.map(n => n.namespace).sort()]
|
|
|
|
|
: [];
|
2020-09-07 17:34:18 +02:00
|
|
|
|
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}>
|
2019-04-09 14:47:37 +02:00
|
|
|
{this.renderOverview()}
|
2019-04-24 09:46:22 +02:00
|
|
|
<PageActions>
|
2020-08-11 10:34:29 +02:00
|
|
|
<OverviewPageActions
|
|
|
|
|
showCreateButton={showCreateButton}
|
2020-09-07 17:34:18 +02:00
|
|
|
currentGroup={namespace}
|
|
|
|
|
groups={namespacesToRender}
|
|
|
|
|
groupSelected={this.namespaceSelected}
|
2020-09-10 10:37:35 +02:00
|
|
|
link="repos"
|
2020-08-11 10:34:29 +02:00
|
|
|
label={t("overview.createButton")}
|
|
|
|
|
testId="repository-overview"
|
2020-11-19 10:57:33 +01:00
|
|
|
searchPlaceholder={t("overview.searchRepository")}
|
2020-08-11 10:34:29 +02:00
|
|
|
/>
|
2019-04-24 09:46:22 +02:00
|
|
|
</PageActions>
|
2018-07-31 16:32:16 +02:00
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 14:47:37 +02:00
|
|
|
renderRepositoryList() {
|
2020-09-17 08:21:31 +02:00
|
|
|
const { collection, page, location, namespaces, t } = this.props;
|
2019-04-09 14:47:37 +02:00
|
|
|
|
|
|
|
|
if (collection._embedded && collection._embedded.repositories.length > 0) {
|
2018-07-31 16:32:16 +02:00
|
|
|
return (
|
2019-04-17 12:37:48 +02:00
|
|
|
<>
|
2020-09-17 08:21:31 +02:00
|
|
|
<RepositoryList repositories={collection._embedded.repositories} namespaces={namespaces} />
|
2019-10-21 10:57:56 +02:00
|
|
|
<LinkPaginator collection={collection} page={page} filter={urls.getQueryStringFromLocation(location)} />
|
2019-04-09 14:47:37 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-21 10:57:56 +02:00
|
|
|
return <Notification type="info">{t("overview.noRepositories")}</Notification>;
|
2019-04-09 14:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderOverview() {
|
|
|
|
|
const { collection } = this.props;
|
|
|
|
|
if (collection) {
|
|
|
|
|
return (
|
2019-04-17 14:27:17 +02:00
|
|
|
<>
|
2019-04-09 14:47:37 +02:00
|
|
|
{this.renderRepositoryList()}
|
2018-08-03 08:52:02 +02:00
|
|
|
{this.renderCreateButton()}
|
2019-04-17 12:37:48 +02:00
|
|
|
</>
|
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;
|
|
|
|
|
}
|
2019-04-17 12:37:48 +02:00
|
|
|
}
|
2018-08-01 14:56:24 +02:00
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
2019-04-17 12:37:48 +02:00
|
|
|
const { match } = ownProps;
|
2018-07-31 16:32:16 +02:00
|
|
|
const collection = getRepositoryCollection(state);
|
2020-09-04 21:44:48 +02:00
|
|
|
const namespaces = getNamespaceCollection(state);
|
|
|
|
|
const loading = isFetchReposPending(state) || isFetchNamespacesPending(state);
|
2018-08-01 10:00:53 +02:00
|
|
|
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);
|
2020-09-04 21:44:48 +02:00
|
|
|
const namespacesLink = getNamespacesLink(state);
|
2018-07-31 16:32:16 +02:00
|
|
|
return {
|
2018-08-01 10:00:53 +02:00
|
|
|
collection,
|
2020-09-04 21:44:48 +02:00
|
|
|
namespaces,
|
2018-08-01 10:00:53 +02:00
|
|
|
loading,
|
2018-08-03 08:52:02 +02:00
|
|
|
error,
|
2019-04-17 12:37:48 +02:00
|
|
|
page,
|
2020-09-04 17:13:25 +02:00
|
|
|
namespace,
|
2019-04-17 12:37:48 +02:00
|
|
|
showCreateButton,
|
2020-09-04 21:44:48 +02:00
|
|
|
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 {
|
2020-09-04 21:44:48 +02:00
|
|
|
fetchReposByPage: (link: string, page: number, filter?: string) => {
|
|
|
|
|
dispatch(fetchReposByPage(link, page, filter));
|
|
|
|
|
},
|
|
|
|
|
fetchNamespaces: (link: string) => {
|
|
|
|
|
dispatch(fetchNamespaces(link));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
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)));
|