Skip to content

Commit

Permalink
Merge pull request #35 from velrest/hot-fix
Browse files Browse the repository at this point in the history
reload relation table if new project is added
  • Loading branch information
velrest authored Mar 17, 2021
2 parents a7a22fb + 96ac1ff commit 64fc76a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 24 deletions.
26 changes: 8 additions & 18 deletions addon/controllers/project.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 (
Expand All @@ -42,8 +34,6 @@ export default class ProjectController extends Controller {
) {
this.transitionToRoute("project.form", projects[0].EPROID);
}

return projects;
}

@action
Expand Down
2 changes: 2 additions & 0 deletions addon/controllers/project/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion addon/routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions addon/routes/project/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
19 changes: 19 additions & 0 deletions addon/services/construction-project.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -7,6 +8,8 @@ import XMLApiService from "./xml-api";

export default class BuildingProjectService extends XMLApiService {
@service config;
@service store;

cache = {};

createAndCacheProject(xml) {
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions addon/templates/project.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div {{did-insert (perform this.fetchProjects)}}>
{{#if this.fetchProjects.isRunning}}
<div {{did-insert this.onLoad}}>
{{#if this.constructionProject.all.isRunning}}
<div class="uk-flex uk-flex-center uk-flex-middle uk-margin-large-top">
<UkSpinner @ratio="3" />
</div>
Expand Down
3 changes: 1 addition & 2 deletions addon/templates/project/form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
disabled
readonly
/>
<Field type="number" @attr="constructionSurveyDept" disabled readonly />

<Field
@inputType="select"
Expand Down Expand Up @@ -117,8 +118,6 @@
@attr="totalCostsOfProject"
/>

<Field type="number" @required={{true}} @attr="constructionSurveyDept" />

<Field
@inputType="select"
@attr="typeOfConstruction"
Expand Down
3 changes: 2 additions & 1 deletion tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default function () {
return schema.gwrLinks
.all()
.filter(
(link) => link.attrs.localId === Number(request.queryParams.local_id)
(link) =>
Number(link.attrs.localId) === Number(request.queryParams.local_id)
);
}
return schema.gwrLinks.all();
Expand Down

0 comments on commit 64fc76a

Please sign in to comment.