diff --git a/addon/controllers/project.js b/addon/controllers/project.js index c9f1bed8..b96251d0 100644 --- a/addon/controllers/project.js +++ b/addon/controllers/project.js @@ -1,7 +1,6 @@ import Controller from "@ember/controller"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; -import { task, lastValue } from "ember-concurrency-decorators"; export default class ProjectController extends Controller { @service router; @@ -18,22 +17,15 @@ export default class ProjectController extends Controller { return Number(this.router.currentRoute.params.project_id); } - @lastValue("fetchProjects") projects = []; + get projects() { + return this.constructionProject.projects; + } - @task - *fetchProjects() { - const links = yield this.store.query("gwr-link", { - local_id: this.model.id, - }); - // We make a request for each project here but the probability - // that there are a lot of linked projects is rather small so this - // should be okay. Would be a future pain point if this requirement - // would change. - const projects = yield Promise.all( - links.map(({ eproid }) => - this.constructionProject.getFromCacheOrApi(eproid) - ) - ); + @action + async onLoad() { + // We then use `constructionProject.projects` in the template to reference this. + // This is so we can update the table if we add a new project in the subroute /new + const projects = await this.constructionProject.all.perform(this.model); // Load the first project in the list if none is selected so we always display a project. if ( @@ -42,8 +34,6 @@ export default class ProjectController extends Controller { ) { this.transitionToRoute("project.form", projects[0].EPROID); } - - return projects; } @action diff --git a/addon/controllers/project/form.js b/addon/controllers/project/form.js index e7c1e22d..47891194 100644 --- a/addon/controllers/project/form.js +++ b/addon/controllers/project/form.js @@ -65,6 +65,8 @@ export default class ProjectFormController extends Controller { localId: this.model.instanceId, }); await link.save(); + // Reload the overview table to display the new project + await this.constructionProject.all.perform(this.model.instanceId); this.transitionToRoute("project.form", project.EPROID); } else { await this.constructionProject.update(this.project); diff --git a/addon/routes/project.js b/addon/routes/project.js index 694697ae..e362befe 100644 --- a/addon/routes/project.js +++ b/addon/routes/project.js @@ -2,6 +2,6 @@ import Route from "@ember/routing/route"; export default class ProjectRoute extends Route { async model() { - return this.modelFor("application"); + return this.modelFor("application").id; } } diff --git a/addon/routes/project/new.js b/addon/routes/project/new.js index 9f9a8cce..95930485 100644 --- a/addon/routes/project/new.js +++ b/addon/routes/project/new.js @@ -13,6 +13,7 @@ export default class ProjectNewRoute extends Route { project.constructionLocalisation.municipalityId = this.config.municipalityId; project.constructionLocalisation.municipalityName = this.config.municipalityName; project.constructionLocalisation.cantonAbbreviation = this.config.cantonAbbreviation; + project.constructionSurveyDept = this.config.constructionSurveyDept; return { project, instanceId: this.modelFor("application").id }; } } diff --git a/addon/services/construction-project.js b/addon/services/construction-project.js index 809218de..87b40c7b 100644 --- a/addon/services/construction-project.js +++ b/addon/services/construction-project.js @@ -1,4 +1,5 @@ import { inject as service } from "@ember/service"; +import { task, lastValue } from "ember-concurrency-decorators"; import ConstructionProject from "ember-ebau-gwr/models/construction-project"; import ConstructionProjectsList from "ember-ebau-gwr/models/construction-projects-list"; import SearchResult from "ember-ebau-gwr/models/search-result"; @@ -7,6 +8,8 @@ import XMLApiService from "./xml-api"; export default class BuildingProjectService extends XMLApiService { @service config; + @service store; + cache = {}; createAndCacheProject(xml) { @@ -130,4 +133,20 @@ export default class BuildingProjectService extends XMLApiService { return new SearchResult(await response.text()).constructionProjectsList; } + + @lastValue("all") projects = []; + @task + *all(localId) { + const links = yield this.store.query("gwr-link", { + local_id: localId, + }); + // We make a request for each project here but the probability + // that there are a lot of linked projects is rather small so this + // should be okay. Would be a future pain point if this requirement + // would change. + const projects = yield Promise.all( + links.map(({ eproid }) => this.getFromCacheOrApi(eproid)) + ); + return projects; + } } diff --git a/addon/templates/project.hbs b/addon/templates/project.hbs index c05c3cb3..6d5792fd 100644 --- a/addon/templates/project.hbs +++ b/addon/templates/project.hbs @@ -1,5 +1,5 @@ -