From 745778fb3af34cab54f7ad7c7db228de38a930ec Mon Sep 17 00:00:00 2001
From: Charly <maeder@cines.fr>
Date: Tue, 20 Jul 2021 11:28:49 +0200
Subject: [PATCH] Add popup and redirection when catalog is created

---
 src/app/app.module.ts                         |  4 ++-
 src/app/repository/repository.component.ts    | 25 ++++++++-----
 .../services/publish-repository.service.ts    | 36 +++++++++----------
 3 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 71dfe3724..5e5c8cf1a 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -38,6 +38,7 @@ import { AppRoutingModule } from './app-routing.module';
 import { NbLayoutModule, NbThemeModule, NbTooltipModule, NbSpinnerModule, NbSelectModule, NbTabsetModule, NbAutocompleteModule, NbListModule, NbAccordionModule } from '@nebular/theme';
 
 import { DashboardComponent } from './dashboard/dashboard.component';
+import { NbToastrModule } from '@nebular/theme';
 
 
 
@@ -85,7 +86,8 @@ import { DashboardComponent } from './dashboard/dashboard.component';
     NbTooltipModule,
     NbAutocompleteModule,
     NbListModule,
-    NbAccordionModule
+    NbAccordionModule,
+    NbToastrModule.forRoot()
   ],
 
   providers: [
diff --git a/src/app/repository/repository.component.ts b/src/app/repository/repository.component.ts
index 425df4800..14f3ab337 100644
--- a/src/app/repository/repository.component.ts
+++ b/src/app/repository/repository.component.ts
@@ -1,13 +1,12 @@
 import { Component, OnInit } from '@angular/core';
 import { Validators } from '@angular/forms';
-import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { FileSaverService } from 'ngx-filesaver';
 import { FormControl } from '@angular/forms';
 import { FormGroup } from '@angular/forms';
-import { AppConfiguration } from '../AppConfiguration';
 import { environment } from 'src/environments/environment.prod';
-import { AuthService } from '../authentication/services/auth.service';
 import { PublishRepositoryService } from './services/publish-repository.service';
+import { NbGlobalPhysicalPosition, NbToastrService } from '@nebular/theme';
+import { Router } from '@angular/router';
 
 
 @Component({
@@ -37,14 +36,14 @@ export class RepositoryComponent implements OnInit {
 
 
   constructor(
-    private appConfig: AppConfiguration,
-    private authService: AuthService,
     private _FileSaverService: FileSaverService,
-    private publishService: PublishRepositoryService
+    private publishService: PublishRepositoryService,
+    private toastrService: NbToastrService,
+    private router: Router
   ) { }
 
   ngOnInit() {
-   
+
     this.publishService.getPersistentUrl().subscribe({
       next: (response) => this.persistentUrl = response.persistentUrl
     });
@@ -127,7 +126,15 @@ repository:\n\
     dct:licence <' + this.Form.value.repolicence + '>;\n\
     dct:language lang:' + this.Form.value.repolanguage + ' .\n';
 
-    console.log(data);
-    return this.publishService.publishRepository(data);
+    this.publishService.publishRepository(data).then(() => {
+      this.toastrService.show('Created', 'Catalog created !',
+        { position: NbGlobalPhysicalPosition.TOP_RIGHT, status: 'success' });
+      this.router.navigateByUrl('/dashboard/publishapi');
+    }).catch(() => {
+      this.toastrService.show('Error', 'Catalog created !',
+        { position: NbGlobalPhysicalPosition.TOP_RIGHT, status: 'warning' });
+    });
+
+
   }
 }
diff --git a/src/app/repository/services/publish-repository.service.ts b/src/app/repository/services/publish-repository.service.ts
index 934455262..5677cf78b 100644
--- a/src/app/repository/services/publish-repository.service.ts
+++ b/src/app/repository/services/publish-repository.service.ts
@@ -28,31 +28,27 @@ export class PublishRepositoryService {
     return environment.smartharvesterUrl + '/harvester/api'
   }
 
-  publishRepository(data: string) {
+  async publishRepository(data: string) {
 
-      if (this.fds2Token) {
+    if (this.fds2Token) {
 
-        const myHeaders = new Headers();
-        myHeaders.append('Accept', 'text/turtle');
-        myHeaders.append('Content-Type', 'text/turtle');
-        myHeaders.append('Authorization', 'Bearer ' + this.fds2Token);
-        const myInit = { method: 'POST', body: data, headers: myHeaders };
+      const myHeaders = new Headers();
+      myHeaders.append('Accept', 'text/turtle');
+      myHeaders.append('Content-Type', 'text/turtle');
+      myHeaders.append('Authorization', 'Bearer ' + this.fds2Token);
+      const myInit = { method: 'POST', body: data, headers: myHeaders };
 
-        const myRequest = new Request(FDP_URL + "/catalog", myInit);
+      const myRequest = new Request(FDP_URL + "/catalog", myInit);
 
-        fetch(myRequest, myInit)
-          .then(response => {
-            response.text()
-              .then(data => {
-                const catId = data.substring(38, 74);
-                console.log('catalog creaetd with fdp / id = ' + catId);
-                this.addUserCatalog(catId).then((response) => console.log(response));
-              });
-          });
+      const response = await fetch(myRequest, myInit);
+      const responseText = await response.text();
 
-        return "The repository has not been published"
-      }
-    
+      const catId = responseText.substring(38, 74);
+      console.log('catalog creaetd with fdp / id = ' + catId);
+      return this.addUserCatalog(catId);
+    }
+
+    return null;
   }
 
   getPersistentUrl(): Observable<PersistentUrlResponse> {
-- 
GitLab