Skip to content

Commit

Permalink
#3 add report image to an existing list instead of replacing the whol…
Browse files Browse the repository at this point in the history
…e list with a new one
  • Loading branch information
p-a-s-c-a-l committed Mar 13, 2019
1 parent 93b0b85 commit c151cb3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 0 additions & 4 deletions app/data/glStepTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"relationships": {
"field_report_images": {
"data": [
{
"id": null,
"type": "node--report_image"
}
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 13 additions & 2 deletions app/scripts/services/drupalService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c151cb3

Please sign in to comment.