fix fetch content multiple times for same file

This commit is contained in:
Eduard Heimbuch
2020-01-15 08:01:12 +01:00
parent 0888006a74
commit 3f518c501a
2 changed files with 37 additions and 32 deletions

View File

@@ -17,7 +17,7 @@ type Props = WithTranslation &
class ChangesetsRoot extends React.Component<Props> { class ChangesetsRoot extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { branches, baseUrl } = this.props; const { branches, baseUrl } = this.props;
if (branches?.length > 0 && this.isSelectedBranchIsNotABranch()) { if (branches?.length > 0 && this.isSelectedBranchNotABranch()) {
const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0]; const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0];
this.props.history.push(`${baseUrl}/branch/${encodeURIComponent(defaultBranch.name)}/changesets/`); this.props.history.push(`${baseUrl}/branch/${encodeURIComponent(defaultBranch.name)}/changesets/`);
} }
@@ -30,7 +30,7 @@ class ChangesetsRoot extends React.Component<Props> {
return url; return url;
}; };
isSelectedBranchIsNotABranch = () => { isSelectedBranchNotABranch = () => {
const { branches, selectedBranch } = this.props; const { branches, selectedBranch } = this.props;
return branches?.filter(b => b.name === selectedBranch).length === 0; return branches?.filter(b => b.name === selectedBranch).length === 0;
}; };
@@ -64,7 +64,7 @@ class ChangesetsRoot extends React.Component<Props> {
<> <>
<CodeActionBar <CodeActionBar
branches={branches} branches={branches}
selectedBranch={!this.isSelectedBranchIsNotABranch() ? selectedBranch : undefined} selectedBranch={!this.isSelectedBranchNotABranch() ? selectedBranch : undefined}
onSelectBranch={this.onSelectBranch} onSelectBranch={this.onSelectBranch}
switchViewLink={this.evaluateSwitchViewLink()} switchViewLink={this.evaluateSwitchViewLink()}
/> />

View File

@@ -12,6 +12,7 @@ type State = {
content: string; content: string;
error?: Error; error?: Error;
loaded: boolean; loaded: boolean;
currentFileRevision: string;
}; };
class SourcecodeViewer extends React.Component<Props, State> { class SourcecodeViewer extends React.Component<Props, State> {
@@ -20,30 +21,44 @@ class SourcecodeViewer extends React.Component<Props, State> {
this.state = { this.state = {
content: "", content: "",
loaded: false loaded: false,
currentFileRevision: ""
}; };
} }
componentDidMount() { componentDidMount() {
const { file } = this.props;
const { currentFileRevision } = this.state;
if (file.revision !== currentFileRevision) {
this.fetchContent();
}
}
componentDidUpdate() {
const { file } = this.props;
const { currentFileRevision } = this.state;
if (file.revision !== currentFileRevision) {
this.fetchContent();
}
}
fetchContent = () => {
const { file } = this.props; const { file } = this.props;
getContent(file._links.self.href) getContent(file._links.self.href)
.then(result => { .then(content => {
if (result.error) {
this.setState({ this.setState({
...this.state, content,
error: result.error, loaded: true,
loaded: true currentFileRevision: file.revision
}); });
} else {
this.setState({
...this.state,
content: result,
loaded: true
});
}
}) })
.catch(err => {}); .catch(error => {
} this.setState({
error,
loaded: true
});
});
};
render() { render() {
const { content, error, loaded } = this.state; const { content, error, loaded } = this.state;
@@ -70,17 +85,7 @@ export function getLanguage(language: string) {
} }
export function getContent(url: string) { export function getContent(url: string) {
return apiClient return apiClient.get(url).then(response => response.text());
.get(url)
.then(response => response.text())
.then(response => {
return response;
})
.catch(err => {
return {
error: err
};
});
} }
export default withTranslation("repos")(SourcecodeViewer); export default withTranslation("repos")(SourcecodeViewer);