Files
SCM-Manager/scm-ui/src/repos/branches/containers/BranchesOverview.js

141 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-03-28 11:51:00 +01:00
// @flow
import React from "react";
import {
fetchBranches,
getBranches,
getFetchBranchesFailure,
isFetchBranchesPending
} from "../../modules/branches";
import { connect } from "react-redux";
import type { Branch, Repository } from "@scm-manager/ui-types";
import { compose } from "redux";
import { translate } from "react-i18next";
import { withRouter } from "react-router-dom";
import {
2019-03-28 14:47:57 +01:00
CreateButton,
ErrorNotification,
Loading,
Subtitle
} from "@scm-manager/ui-components";
import BranchTable from "../components/BranchTable";
2019-03-28 11:51:00 +01:00
type Props = {
repository: Repository,
baseUrl: string,
2019-03-28 11:51:00 +01:00
loading: boolean,
error: Error,
branches: Branch[],
// dispatch props
2019-03-28 14:47:57 +01:00
showCreateButton: boolean,
2019-03-28 11:51:00 +01:00
fetchBranches: Repository => void,
// Context props
history: any,
match: any,
t: string => string
};
// master, default should always be the first one,
// followed by develop the rest should be ordered by its name
export function orderBranches(branches: Branch[]) {
branches.sort((a, b) => {
if (a.defaultBranch && !b.defaultBranch) {
return -20;
} else if (!a.defaultBranch && b.defaultBranch) {
return 20;
} else if (a.name === "master" && b.name !== "master") {
return -10;
} else if (a.name !== "master" && b.name === "master") {
return 10;
} else if (a.name === "default" && b.name !== "default") {
return -10;
} else if (a.name !== "default" && b.name === "default") {
return 10;
} else if (a.name === "develop" && b.name !== "develop") {
return -5;
} else if (a.name !== "develop" && b.name === "develop") {
return 5;
} else if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
return 0;
});
}
2019-03-28 11:51:00 +01:00
class BranchesOverview extends React.Component<Props> {
componentDidMount() {
const { fetchBranches, repository } = this.props;
2019-03-28 11:51:00 +01:00
fetchBranches(repository);
}
render() {
const { baseUrl, loading, error, branches, t } = this.props;
2019-03-28 11:51:00 +01:00
if (error) {
return <ErrorNotification error={error} />;
}
if (!branches || loading) {
2019-03-28 11:51:00 +01:00
return <Loading />;
}
orderBranches(branches);
return (
<>
<Subtitle subtitle={t("branches.overview.title")} />
<BranchTable baseUrl={baseUrl} branches={branches} />
2019-03-28 14:47:57 +01:00
{this.renderCreateButton()}
</>
);
2019-03-28 11:51:00 +01:00
}
2019-03-28 14:47:57 +01:00
renderCreateButton() {
const { showCreateButton, t } = this.props;
if (showCreateButton || true) {
// TODO
2019-03-28 14:47:57 +01:00
return (
<CreateButton
label={t("branches.overview.createButton")}
link="./create"
/>
2019-03-28 14:47:57 +01:00
);
}
return null;
}
2019-03-28 11:51:00 +01:00
}
const mapStateToProps = (state, ownProps) => {
const { repository } = ownProps;
const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository);
return {
repository,
loading,
error,
branches
};
};
const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
}
2019-03-28 11:51:00 +01:00
};
};
export default compose(
translate("repos"),
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(BranchesOverview);