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

148 lines
4.3 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 { compose } from "redux";
2020-02-28 13:15:27 +01:00
import { withRouter, RouteComponentProps } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
2019-10-21 10:57:56 +02:00
import { Branch, Changeset, PagedCollection, Repository } from "@scm-manager/ui-types";
2019-02-01 10:50:05 +01:00
import {
ChangesetList,
2019-02-01 10:50:05 +01:00
ErrorNotification,
getPageFromMatch,
LinkPaginator,
2019-04-09 15:19:43 +02:00
Loading,
Notification
} from "@scm-manager/ui-components";
import {
fetchChangesets,
getChangesets,
getFetchChangesetsFailure,
isFetchChangesetsPending,
selectListAsCollection
} from "../modules/changesets";
2018-09-17 14:03:13 +02:00
2020-02-28 13:15:27 +01:00
type Props = RouteComponentProps &
WithTranslation & {
repository: Repository;
branch: Branch;
page: number;
2018-10-16 20:19:22 +02:00
2020-02-28 13:15:27 +01:00
// State props
changesets: Changeset[];
list: PagedCollection;
loading: boolean;
error: Error;
2018-10-16 20:19:22 +02:00
2020-02-28 13:15:27 +01:00
// Dispatch props
fetchChangesets: (p1: Repository, p2: Branch, p3: number) => void;
};
2018-10-17 10:38:46 +02:00
class Changesets extends React.Component<Props> {
2018-09-17 14:03:13 +02:00
componentDidMount() {
2018-10-17 10:38:46 +02:00
const { fetchChangesets, repository, branch, page } = this.props;
fetchChangesets(repository, branch, page);
}
2020-03-02 14:13:51 +01:00
shouldComponentUpdate(nextProps: Readonly<Props>): boolean {
return this.props.changesets !== nextProps.changesets ||
this.props.loading !== nextProps.loading ||
this.props.error !== nextProps.error;
2020-02-28 13:15:27 +01:00
}
render() {
2019-04-09 15:19:43 +02:00
const { changesets, loading, error, t } = this.props;
if (error) {
2018-10-17 11:55:47 +02:00
return <ErrorNotification error={error} />;
}
if (loading) {
2018-09-18 16:51:31 +02:00
return <Loading />;
2018-09-17 14:03:13 +02:00
}
2018-10-17 11:55:47 +02:00
if (!changesets || changesets.length === 0) {
2019-04-09 15:19:43 +02:00
return (
<div className="panel-block">
2019-10-21 10:57:56 +02:00
<Notification type="info">{t("changesets.noChangesets")}</Notification>
2019-04-09 15:19:43 +02:00
</div>
);
}
2018-09-18 16:51:31 +02:00
return (
<>
{this.renderList()}
2018-09-19 13:49:04 +02:00
{this.renderPaginator()}
</>
2018-09-18 16:51:31 +02:00
);
}
renderList = () => {
const { repository, changesets } = this.props;
2019-02-01 10:50:05 +01:00
return (
<div className="panel-block">
<ChangesetList repository={repository} changesets={changesets} />
</div>
);
};
renderPaginator = () => {
2018-10-17 10:57:01 +02:00
const { page, list } = this.props;
2018-09-19 13:49:04 +02:00
if (list) {
2019-02-01 10:50:05 +01:00
return (
<div className="panel-footer">
<LinkPaginator page={page} collection={list} />
</div>
);
2018-09-19 13:49:04 +02:00
}
return null;
2018-09-17 14:03:13 +02:00
};
}
const mapDispatchToProps = (dispatch: any) => {
2018-09-17 14:03:13 +02:00
return {
2018-10-17 10:38:46 +02:00
fetchChangesets: (repo: Repository, branch: Branch, page: number) => {
dispatch(fetchChangesets(repo, branch, page));
}
2018-09-18 16:51:31 +02:00
};
2018-09-17 14:03:13 +02:00
};
const mapStateToProps = (state: any, ownProps: Props) => {
2018-10-17 10:38:46 +02:00
const { repository, branch, match } = ownProps;
const changesets = getChangesets(state, repository, branch);
const loading = isFetchChangesetsPending(state, repository, branch);
const error = getFetchChangesetsFailure(state, repository, branch);
const list = selectListAsCollection(state, repository, branch);
2018-10-17 11:55:47 +02:00
const page = getPageFromMatch(match);
2018-10-17 10:38:46 +02:00
return {
changesets,
list,
page,
loading,
error
};
};
2018-10-17 10:38:46 +02:00
export default compose(withTranslation("repos"), withRouter, connect(mapStateToProps, mapDispatchToProps))(Changesets);