From 2b0eb49c0054f37c6048ffaeb0e244901aa5f9aa Mon Sep 17 00:00:00 2001
From: Baptiste Toulemonde <toulemonde@cines.fr>
Date: Fri, 23 Jul 2021 10:55:09 +0200
Subject: [PATCH] mapping fix request post

---
 .../datasets/services/dataset-crud.service.ts | 19 +++---
 src/app/mapping/mapping.component.ts          | 61 ++++++++-----------
 2 files changed, 36 insertions(+), 44 deletions(-)

diff --git a/src/app/datasets/services/dataset-crud.service.ts b/src/app/datasets/services/dataset-crud.service.ts
index 8bf3cae90..73ba27ffa 100644
--- a/src/app/datasets/services/dataset-crud.service.ts
+++ b/src/app/datasets/services/dataset-crud.service.ts
@@ -19,17 +19,18 @@ export class DatasetCrudService {
   constructor(private http: HttpClient, private appConfig: AppConfiguration, private parserService: ParseXmlService, private sessionStorage: TokenStorageService) { }
 
   
-  createDataset(data: string): Observable<any> {
+  async createDataset(data: string): Promise<any> {
     if (this.fds2Token) {
-      const httpOptions = {
-        headers: new HttpHeaders({
-          'Accept': 'text/turtle',
-          'Content-Type': 'text/turtle',
-          'Authorization': 'Bearer ' + this.fds2Token
-        })
-      };
+      const httpOptions = new Headers();
+      httpOptions.append( 'Accept',  'text/turtle');
+      httpOptions.append('Content-Type',  'text/turtle');
+      httpOptions.append('Authorization', 'Bearer '+ this.fds2Token );
+
+
+    const myInit = { method: 'POST', body: data , headers: httpOptions };
+    const myRequest = new Request(`${FDP_URL}/dataset`, myInit);
 
-      return this.http.post(FDP_URL + "/dataset", data, httpOptions);
+    return fetch(myRequest, myInit)
     }
   }
 
diff --git a/src/app/mapping/mapping.component.ts b/src/app/mapping/mapping.component.ts
index c1dc291ed..16d95dfaa 100644
--- a/src/app/mapping/mapping.component.ts
+++ b/src/app/mapping/mapping.component.ts
@@ -1,10 +1,11 @@
 
-import { ScrollStrategyOptions } from '@angular/cdk/overlay';
+
+import { HttpResponse } from '@angular/common/http';
 import { Component, Input, OnInit, ViewChild } from '@angular/core';
 import { MatDialog } from '@angular/material/dialog';
 import { Router } from '@angular/router';
-import { NbDialogService } from '@nebular/theme';
-import { Observable, of } from 'rxjs';
+
+import { Observable, of, Subscription } from 'rxjs';
 import { map } from 'rxjs/operators';
 import { environment } from 'src/environments/environment.prod';
 import { DatasetCrudService } from '../datasets/services/dataset-crud.service';
@@ -35,7 +36,7 @@ export class MappingComponent implements OnInit {
   loadingCr = false;
   FDP_URL = environment.fdpUrl;
 
-  ids: number [];
+  ids: number[];
   @ViewChild('autoInput') input;
   @Input() catalogId: any;
 
@@ -85,13 +86,15 @@ export class MappingComponent implements OnInit {
     return mappedMetadata;
   }
 
-  publishDataset() {
+   publishDataset() {
     let data: string = '';
     let properties: string = '';
     let postedDatastes = [];
     let notPostedDatasets = [];
     this.loading = true;
+    const requestPromises: Promise<any>[] = [];
     for (let i = 0; i < this.mappedMetadatas.length; i++) {
+
       let title = "";
       properties = "";
       this.mappedMetadatas[i].forEach((value: string, key: string) => {
@@ -130,38 +133,26 @@ export class MappingComponent implements OnInit {
                       dct:title "' + title + '" ;\n\
                       dct:isPartOf <https://f2ds.eosc-pillar.eu/catalog/' + this.catalogId + '>. } '*/
 
-      this.dataSetService.createDataset(data).subscribe(
-        (error: Error) => {
-          notPostedDatasets.push(this.ids[i]);
-          if (i === this.mappedMetadatas.length - 1) {
-            this.loading = false;
-            this.dialog.open(FeedbackDialogComponent, {
-              data: {
-                postedMetadatas: postedDatastes,
-                notPostedMetadatas: notPostedDatasets
-              },
-              autoFocus: true, 
-              maxHeight: '70%',
-            }).afterClosed().subscribe(() => this.route.navigateByUrl('/dashboard'));
-          }
-        },
-        () => {
-          postedDatastes.push(this.ids[i]);
-          if (i === this.mappedMetadatas.length - 1) {
-            this.loading = false;
-            this.dialog.open(FeedbackDialogComponent, {
-              data: {
-                postedMetadatas: postedDatastes,
-                notPostedMetadatas: notPostedDatasets
-              },
-              autoFocus: true, 
-              maxHeight: '70%',
-            }).afterClosed().subscribe(() => this.route.navigateByUrl('/dashboard'));
-
-          }
 
-        })
+      let requestPromise = this.dataSetService.createDataset(data).then( (resp: HttpResponse<any>) => {
+        if (resp.status.toString().startsWith('2') && resp) {
+          postedDatastes.push(this.ids[i]);
+        } else {
+          notPostedDatasets.push(this.ids[i]);
+        }
+      })
+      requestPromises.push(requestPromise);
     }
+    Promise.all(requestPromises).finally( () => {
+      this.loading = false;
+      this.dialog.open(FeedbackDialogComponent, {
+        data: {
+          postedMetadatas: postedDatastes,
+          notPostedMetadatas: notPostedDatasets
+        }
+      }).afterClosed().subscribe(() => this.route.navigateByUrl('/dashboard'));
+
+    })
   }
 
   /* function to get recursively all the keys and values from the JSON object */
-- 
GitLab