From 27a6b5a6c31f7fd97b2dc26a85f06b02a3b6c745 Mon Sep 17 00:00:00 2001
From: kaw67872 <kawtar.laariche@iais.fraunhofer.de>
Date: Wed, 26 Jun 2024 10:11:44 +0200
Subject: [PATCH] #24: add update image , add , delete tag functionalities

---
 .../core/services/private-catalogs.service.ts |  13 +-
 ...publish-to-marketplace-page.component.html |   2 +-
 .../publish-to-marketplace-page.component.ts  | 118 ++++++++++++++----
 3 files changed, 99 insertions(+), 34 deletions(-)

diff --git a/src/app/core/services/private-catalogs.service.ts b/src/app/core/services/private-catalogs.service.ts
index d8656f6..c3077b8 100644
--- a/src/app/core/services/private-catalogs.service.ts
+++ b/src/app/core/services/private-catalogs.service.ts
@@ -716,7 +716,7 @@ export class PrivateCatalogsService {
     );
   }
 
-  deleteTag(tag: Tag, solutionId: string) {
+  deleteTag(tag: string, solutionId: string) {
     const url =
       apiConfig.apiBackendURL +
       apiConfig.urlDeleteTag +
@@ -732,7 +732,7 @@ export class PrivateCatalogsService {
     );
   }
 
-  updateAddTag(tag: Tag, solutionId: string) {
+  addTag(tag: string, solutionId: string) {
     const url =
       apiConfig.apiBackendURL +
       apiConfig.urlAddTag +
@@ -774,10 +774,11 @@ export class PrivateCatalogsService {
       '/picture';
     const formData = new FormData();
     formData.append('file', image);
-
-    const req = new HttpRequest('POST', url, formData);
-
-    return this.http.request(req);
+    return this._httpSharedService.post(url, undefined, formData).pipe(
+      catchError((error) => {
+        throw error;
+      }),
+    );
   }
 
   updateSolutionFiles(
diff --git a/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.html b/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.html
index c5c8c0a..8fd346a 100644
--- a/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.html
+++ b/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.html
@@ -141,7 +141,7 @@
             <mat-label>Add tag</mat-label>
             <mat-chip-grid #chipGrid>
               @for (tag of tagsItems(); track tag) {
-                <mat-chip-row (removed)="remove(tag)">
+                <mat-chip-row (removed)="removeTag(tag)">
                   {{ tag.tag }}
                   <button matChipRemove [attr.aria-label]="'remove ' + tag">
                     <mat-icon>cancel</mat-icon>
diff --git a/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.ts b/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.ts
index 898e24d..d01c6d3 100644
--- a/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.ts
+++ b/src/app/shared/components/publish-to-marketplace-page/publish-to-marketplace-page.component.ts
@@ -411,6 +411,12 @@ export class PublishToMarketplacePageComponent implements OnInit {
       const extensionFile = this.getFilenameExtension(this.licenseProfile.name);
     }
   }
+  onClickUpdateImageSolution(file: File) {
+    return this.privateCatalogsService.setSolutionPicture(
+      this.solutionId,
+      file,
+    );
+  }
 
   onClickUploadImageFile() {
     const dialogRef: MatDialogRef<UploadLicenseProfileComponent> =
@@ -420,14 +426,26 @@ export class PublishToMarketplacePageComponent implements OnInit {
             title: 'Upload image model',
             isEditMode: false,
             isCheckExtension: false,
-            action: (file: File) => {},
+            action: (file: File) => this.onClickUpdateImageSolution(file),
             isLicenseProfile: false,
             isProcessEvent: false,
           },
         },
       });
 
-    dialogRef.afterClosed().subscribe((result) => {});
+    dialogRef.afterClosed().subscribe((result) => {
+      this.updateSolutionImage();
+    });
+  }
+  updateSolutionImage() {
+    this.publicSolutionsService
+      .getPictureOfSolution(this.solutionId)
+      .subscribe({
+        next: (res) => {
+          this.createImageFromBlob(res);
+        },
+        error: (error) => {},
+      });
   }
 
   onClickUpdateSolutionDocument(file: File) {
@@ -478,19 +496,42 @@ export class PublishToMarketplacePageComponent implements OnInit {
     this.addEditLicenseProfile = true;
   }
 
-  remove(tag: Tag) {
-    this.tagsItems.update((tags: Tag[]) => {
-      const index = tags.indexOf(tag);
-      if (index < 0) {
-        return tags;
-      }
-
-      tags.splice(index, 1);
-      this.announcer.announce(`Removed ${tag}`);
-      return [...tags];
+  removeTag(tag: Tag) {
+    this.privateCatalogsService.deleteTag(tag.tag, this.solutionId).subscribe({
+      next: (res) => {
+        const alert: Alert = {
+          message: 'Tag deleted successfully',
+          type: AlertType.Success,
+        };
+        this.alertService.notify(alert, 5000);
+        this.alertService.notify;
+        this.updateTags();
+      },
+      error: () => {},
     });
   }
 
+  updateTags() {
+    this.publicSolutionsService
+      .getSolutionDetails(this.solutionId, this.revisionId)
+      .subscribe({
+        next: (res) => {
+          const alert: Alert = {
+            message: 'Tag deleted successfully',
+            type: AlertType.Success,
+          };
+          this.alertService.notify(alert, 5000);
+          this.alertService.notify;
+          const tagsList = res?.solutionTagList;
+          this.tagsItems.update((tags: Tag[]) => [...tagsList]);
+          this.publishToMarketPlaceForm.patchValue({
+            tags: res?.solutionTagList,
+          });
+        },
+        error: () => {},
+      });
+  }
+
   removeDocument(document: DocumentModel) {
     this.privateCatalogsService
       .deleteSolutionsFiles(
@@ -516,12 +557,24 @@ export class PublishToMarketplacePageComponent implements OnInit {
   addTag(event: MatChipInputEvent): void {
     const value = { tag: (event.value || '').trim() };
 
-    // Add our keyword
     if (value) {
-      this.tagsItems.update((tags: Tag[]) => [...tags, value]);
-      this.filteredTags.next(
-        this.updateAllTagsSignal(event.value, this.allTags()),
-      );
+      this.privateCatalogsService
+        .addTag(event.value, this.solutionId)
+        .subscribe({
+          next: (res) => {
+            const alert: Alert = {
+              message: 'Tag added successfully',
+              type: AlertType.Success,
+            };
+            this.alertService.notify(alert, 5000);
+            this.alertService.notify;
+            this.updateTags();
+            this.filteredTags.next(
+              this.updateAllTagsSignal(event.value, this.allTags()),
+            );
+          },
+          error: () => {},
+        });
     }
 
     // Clear the input value
@@ -602,7 +655,7 @@ export class PublishToMarketplacePageComponent implements OnInit {
       data.solution.solutionTagList.length >= 1
     ) {
       const tagsList = data.solution?.solutionTagList;
-      this.tagsItems.update((tags: Tag[]) => [...tags, ...tagsList]);
+      this.tagsItems.update((tags: Tag[]) => [...tagsList]);
       this.publishToMarketPlaceForm.patchValue({
         tags: data.solution?.solutionTagList,
       });
@@ -665,14 +718,25 @@ export class PublishToMarketplacePageComponent implements OnInit {
   }
 
   selected(event: MatAutocompleteSelectedEvent): void {
-    this.tagsItems.update((tags: Tag[]) => [
-      ...tags,
-      { tag: event.option.value },
-    ]);
-    event.option.deselect();
-    this.tagCtrl.setValue(null);
-    this.filteredTags.next(
-      this.updateAllTagsSignal(event.option.value, this.allTags()),
-    );
+    this.privateCatalogsService
+      .addTag(event.option.value, this.solutionId)
+      .subscribe({
+        next: (res) => {
+          const alert: Alert = {
+            message: 'Tag added successfully',
+            type: AlertType.Success,
+          };
+          this.alertService.notify(alert, 5000);
+          this.alertService.notify;
+          this.updateTags();
+          event.option.deselect();
+          this.tagCtrl.setValue(null);
+          this.tagInput.nativeElement.value = '';
+          this.filteredTags.next(
+            this.updateAllTagsSignal(event.option.value, this.allTags()),
+          );
+        },
+        error: () => {},
+      });
   }
 }
-- 
GitLab