Files
SCM-Manager/scm-ui/src/repos/sources/containers/HistoryView.js

110 lines
2.2 KiB
JavaScript
Raw Normal View History

// @flow
import React from "react";
2018-11-26 16:39:26 +01:00
import type {
File,
Changeset,
Repository,
PagedCollection
} from "@scm-manager/ui-types";
2018-11-28 10:33:06 +01:00
import {
ErrorNotification,
Loading,
StatePaginator,
ChangesetList
2018-11-28 10:33:06 +01:00
} from "@scm-manager/ui-components";
2018-11-26 15:56:41 +01:00
import { getHistory } from "./history";
type Props = {
file: File,
2018-11-26 15:56:41 +01:00
repository: Repository
};
type State = {
loaded: boolean,
2018-11-26 15:56:41 +01:00
changesets: Changeset[],
2018-11-26 16:39:26 +01:00
page: number,
pageCollection?: PagedCollection,
error?: Error
};
class HistoryView extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
2018-11-26 15:56:41 +01:00
loaded: false,
2018-11-26 16:48:25 +01:00
page: 1,
2018-11-26 15:56:41 +01:00
changesets: []
};
}
componentDidMount() {
const { file } = this.props;
this.updateHistory(file._links.history.href);
}
updateHistory(link: string) {
getHistory(link)
.then(result => {
if (result.error) {
this.setState({
...this.state,
error: result.error,
loaded: true
});
} else {
this.setState({
...this.state,
2018-11-26 15:56:41 +01:00
loaded: true,
2018-11-26 16:39:26 +01:00
changesets: result.changesets,
2018-11-26 16:48:25 +01:00
pageCollection: result.pageCollection,
page: result.pageCollection.page
});
}
})
2018-11-26 15:56:41 +01:00
.catch(err => {});
}
updatePage(page: number) {
const { file } = this.props;
2018-11-28 08:59:18 +01:00
const internalPage = page - 1;
this.updateHistory(
file._links.history.href + "?page=" + internalPage.toString()
);
}
showHistory() {
2018-11-26 15:56:41 +01:00
const { repository } = this.props;
2018-11-26 16:39:26 +01:00
const { changesets, page, pageCollection } = this.state;
2018-11-28 08:59:18 +01:00
const currentPage = page + 1;
2018-11-26 16:39:26 +01:00
return (
<>
<ChangesetList repository={repository} changesets={changesets} />
<StatePaginator
page={currentPage}
collection={pageCollection}
updatePage={(newPage: number) => this.updatePage(newPage)}
/>
2018-11-26 16:39:26 +01:00
</>
);
}
render() {
2018-11-26 15:56:41 +01:00
const { file } = this.props;
const { loaded, error } = this.state;
if (!file || !loaded) {
return <Loading />;
}
if (error) {
return <ErrorNotification error={error} />;
}
const history = this.showHistory();
return <>{history}</>;
}
}
2018-11-26 16:39:26 +01:00
export default HistoryView;