From 4e381522ccd13063b42dd25138309162771ca5b0 Mon Sep 17 00:00:00 2001 From: lorenzo-cavazzi Date: Fri, 24 Apr 2020 17:56:32 +0200 Subject: [PATCH] refactor(environments): use commit data from project coordinator (#917) --- src/notebooks/Notebooks.container.js | 32 ++++++++++++++++++++-------- src/notebooks/Notebooks.state.js | 23 -------------------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/notebooks/Notebooks.container.js b/src/notebooks/Notebooks.container.js index 5c82da7459..70e94f3d0d 100644 --- a/src/notebooks/Notebooks.container.js +++ b/src/notebooks/Notebooks.container.js @@ -24,6 +24,7 @@ import { StartNotebookServer as StartNotebookServerPresent, NotebooksDisabled } import { Notebooks as NotebooksPresent } from "./Notebooks.present"; import { CheckNotebookIcon } from "./Notebooks.present"; import { StatusHelper } from "../model/Model"; +import { ProjectCoordinator } from "../project"; /** * Display the list of Notebook servers @@ -145,6 +146,9 @@ class StartNotebookServer extends Component { this.model = props.model.subModel("notebooks"); this.userModel = props.model.subModel("user"); this.coordinator = new NotebooksCoordinator(props.client, this.model, this.userModel); + // TODO: this should go away when moving all project content to projectCoordinator + this.projectModel = props.model.subModel("project"); + this.projectCoordinator = new ProjectCoordinator(props.client, props.model.subModel("project")); // temporarily reset data since notebooks model was not designed to be static this.coordinator.reset(); @@ -243,13 +247,19 @@ class StartNotebookServer extends Component { async refreshCommits() { if (this._isMounted) { - if (this.model.get("data.fetching")) - return; + if (!this.projectModel.get("commits.fetching")) + await this.projectCoordinator.fetchCommits(); + this.selectCommitWhenReady(); + } + } - await this.coordinator.fetchCommits(); - if (this._isMounted) + // TODO: ugly workaround until branches and commits will be unified in projectCoordinator + async selectCommitWhenReady() { + if (this._isMounted) { + if (this.projectModel.get("commits.fetching")) + setTimeout(() => { this.selectCommitWhenReady(); }, 100); + else this.selectCommit(); - } } @@ -258,8 +268,8 @@ class StartNotebookServer extends Component { // filter the list of commits according to the constraints const maximum = this.model.get("filters.displayedCommits"); const commits = maximum && parseInt(maximum) > 0 ? - this.model.get("data.commits").slice(0, maximum) : - this.model.get("data.commits"); + this.projectModel.get("commits.list").slice(0, maximum) : + this.projectModel.get("commits.list"); let commit = commits[0]; // find the proper commit or return @@ -339,7 +349,9 @@ class StartNotebookServer extends Component { const augmentedState = { ...state.notebooks, data: { - ...state.notebooks.data, + fetched: state.project.commits.fetched, + fetching: state.project.commits.fetching, + commits: state.project.commits.list, branches: ownProps.inherited.branches, autosaved: ownProps.inherited.autosaved }, @@ -388,6 +400,8 @@ class CheckNotebookStatus extends Component { this.model = props.model.subModel("notebooks"); this.userModel = props.model.subModel("user"); this.coordinator = new NotebooksCoordinator(props.client, this.model, this.userModel); + // TODO: this should go away when moving all project content to projectCoordinator + this.projectCoordinator = new ProjectCoordinator(props.client, props.model.subModel("project")); // temporarily reset data since notebooks model was not designed to be static this.coordinator.reset(); @@ -402,7 +416,7 @@ class CheckNotebookStatus extends Component { if (!scope.branch || !scope.commit) return; if (scope.commit === "latest") { - let commits = await this.coordinator.fetchCommits(); + let commits = await this.projectCoordinator.fetchCommits(); scope.commit = commits[0]; this.coordinator.setNotebookFilters(scope); } diff --git a/src/notebooks/Notebooks.state.js b/src/notebooks/Notebooks.state.js index 4cde720f49..f42ca18ebf 100644 --- a/src/notebooks/Notebooks.state.js +++ b/src/notebooks/Notebooks.state.js @@ -699,29 +699,6 @@ class NotebooksCoordinator { throw error; }); } - - - // * Fetch commits -- to be moved * // - async fetchCommits() { - this.model.set("data.fetching", true); - const filters = this.model.get("filters"); - const projectPathWithNamespace = `${encodeURIComponent(filters.namespace)}%2F${filters.project}`; - return this.client.getCommits(projectPathWithNamespace, filters.branch.name) - .then(resp => { - this.model.setObject({ - data: { - fetching: false, - fetched: new Date(), - commits: { $set: resp.data } - } - }); - return resp.data; - }) - .catch(error => { - this.model.set("data.fetching", false); - throw error; - }); - } } export { NotebooksHelper, ExpectedAnnotations, NotebooksCoordinator };