From c151cb39e69c801362e352d376839c51a7a614c7 Mon Sep 17 00:00:00 2001 From: p-a-s-c-a-l Date: Wed, 13 Mar 2019 11:48:32 +0100 Subject: [PATCH] #3 add report image to an existing list instead of replacing the whole list with a new one --- app/data/glStepTemplate.json | 4 ---- .../drupalContextProviderDirectiveController.js | 8 +++++--- app/scripts/services/drupalService.js | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/data/glStepTemplate.json b/app/data/glStepTemplate.json index f7a96ed..c00f1e9 100644 --- a/app/data/glStepTemplate.json +++ b/app/data/glStepTemplate.json @@ -5,10 +5,6 @@ "relationships": { "field_report_images": { "data": [ - { - "id": null, - "type": "node--report_image" - } ] } } diff --git a/app/scripts/controllers/drupalContextProviderDirectiveController.js b/app/scripts/controllers/drupalContextProviderDirectiveController.js index 13ca34a..11567fe 100644 --- a/app/scripts/controllers/drupalContextProviderDirectiveController.js +++ b/app/scripts/controllers/drupalContextProviderDirectiveController.js @@ -519,12 +519,14 @@ angular.module( showIndicatorFileLoadingError(error.data.message.toString()); }); - // full glStepResource is not needed if we use PATCH method! -> data/glStepTemplate.json - /*drupalRestApi.initGlStepResource(eventData.stepUuid).then(function (glStepResource) { + // full glStepResource is needed, even if we use PATCH method! -> data/glStepTemplate.json + // PATCH replaces the field_report_images.data[] array completely, so we have to obtain the original array and add + // our report images on top of it :-/ + drupalRestApi.initGlStepResource(eventData.stepUuid).then(function (glStepResource) { console.log('glStepResource ' + eventData.stepUuid + ' loaded: ' + glStepResource.data.attributes.title); }, function (error) { console.log('could not load glStepResource:' + error); - });*/ + }); // FIXME: get scenario and view ids from Data Package emikatRestApi.getImpactScenario(2846, 2812).then(function (impactScenario) { diff --git a/app/scripts/services/drupalService.js b/app/scripts/services/drupalService.js index 5024e61..bc7816a 100644 --- a/app/scripts/services/drupalService.js +++ b/app/scripts/services/drupalService.js @@ -416,15 +416,26 @@ angular.module( var reportImageResource = createReportImageResource($this.drupalRestApi.token); reportImageResource.store(reportImageTemplate).$promise.then(function storeReportImageSuccess(reportImageResponse) { if (reportImageResponse && reportImageResponse.data && reportImageResponse.data.id) { + if ($this.drupalRestApi.glStepInstance && $this.drupalRestApi.glStepInstance.data && + $this.drupalRestApi.glStepInstance.data.relationships && $this.drupalRestApi.glStepInstance.data.relationships.field_report_images && $this.drupalRestApi.glStepInstance.data.relationships.field_report_images.data && + $this.drupalRestApi.glStepInstance.data.relationships.field_report_images.data.length > 0) { + console.log('adding resource image to ' + $this.drupalRestApi.glStepInstance.data.relationships.field_report_images.data.length + ' existing relationships'); + glStepTemplate.data.relationships.field_report_images.data = $this.drupalRestApi.glStepInstance.data.relationships.field_report_images.data; + } + + var reportImageRelationship = { + 'id': reportImageResponse.data.id, + 'type': 'node--report_image' + }; glStepTemplate.data.id = $this.drupalRestApi.eventData.stepUuid; - glStepTemplate.data.relationships.field_report_images.data[0].id = reportImageResponse.data.id; + glStepTemplate.data.relationships.field_report_images.data.push(reportImageRelationship); console.log('assigning report image ' + reportImageResponse.data.id + ' to GL Step ' + $this.drupalRestApi.eventData.stepUuid); $http( { method: 'PATCH', - url: $this.drupalRestApi.host + '/jsonapi/node/gl_step/'+$this.drupalRestApi.eventData.stepUuid, + url: $this.drupalRestApi.host + '/jsonapi/node/gl_step/' + $this.drupalRestApi.eventData.stepUuid, headers: { 'Accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json',