From 74de556b371a746e490090fd4a946b7f885cac6d Mon Sep 17 00:00:00 2001 From: hudson-newey Date: Fri, 6 Sep 2024 12:28:17 +1000 Subject: [PATCH] Fix new site form not submitting with project_ids Following a change to the baw-server which required an array of project_ids to be sent while site creation, the new site form started throwing errors which stopped users from creating new sites. This commit now sends project_ids when creating a new site, fixing the bug which stopped users from creating new sites. Fixes: #2143 --- .../sites/pages/new/new.component.spec.ts | 21 +++++++++++++++++++ .../sites/pages/new/new.component.ts | 14 ++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/app/components/sites/pages/new/new.component.spec.ts b/src/app/components/sites/pages/new/new.component.spec.ts index fdd97c7bd..31a6a2538 100644 --- a/src/app/components/sites/pages/new/new.component.spec.ts +++ b/src/app/components/sites/pages/new/new.component.spec.ts @@ -132,6 +132,27 @@ describe("SiteNewComponent", () => { ); }); + it("should contain the region and project id in the api calls site model", () => { + setup(defaultProject, defaultRegion); + api.create.and.callFake(() => new Subject()); + const site = new Site( + generateSite(withRegion ? { regionId: defaultRegion.id } : {}) + ); + + spec.component.submit({ ...site }); + + const expectedSiteModel = new Site({ + ...site, + projectIds: [defaultProject.id], + regionId: withRegion ? defaultRegion.id : undefined, + }); + + expect(api.create).toHaveBeenCalledWith( + expectedSiteModel, + defaultProject + ); + }); + it("should redirect to site", () => { setup(defaultProject, defaultRegion); const site = new Site( diff --git a/src/app/components/sites/pages/new/new.component.ts b/src/app/components/sites/pages/new/new.component.ts index dec1e0403..4d66254b5 100644 --- a/src/app/components/sites/pages/new/new.component.ts +++ b/src/app/components/sites/pages/new/new.component.ts @@ -17,6 +17,7 @@ import { Region } from "@models/Region"; import { Site } from "@models/Site"; import { List } from "immutable"; import { ToastrService } from "ngx-toastr"; +import { isInstantiated } from "@helpers/isInstantiated/isInstantiated"; import pointSchema from "../../point.base.json"; import siteSchema from "../../site.base.json"; @@ -40,7 +41,18 @@ class SiteNewComponent extends FormTemplate implements OnInit { successMsg: (model) => defaultSuccessMsg("created", model.name), redirectUser: (model) => this.router.navigateByUrl(model.getViewUrl(this.project)), - getModel: () => (this.region ? { regionId: this.region.id } : {}), + getModel: () => { + const projectIds = new Set([this.project.id]); + + if (this.region) { + return { + regionId: this.region.id, + projectIds, + }; + } + + return { projectIds }; + }, }); }