From 9b9831bd9920bb8a73f0c32a63482f33d2b028eb Mon Sep 17 00:00:00 2001
From: kaw67872 <kawtar.laariche@iais.fraunhofer.de>
Date: Wed, 26 Jun 2024 10:47:23 +0200
Subject: [PATCH] #24: add create tag functionality

---
 .../core/services/private-catalogs.service.ts |  2 +-
 .../publish-to-marketplace-page.component.ts  | 62 +++++++++++--------
 2 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/src/app/core/services/private-catalogs.service.ts b/src/app/core/services/private-catalogs.service.ts
index c3077b8..3c2d9c2 100644
--- a/src/app/core/services/private-catalogs.service.ts
+++ b/src/app/core/services/private-catalogs.service.ts
@@ -701,7 +701,7 @@ export class PrivateCatalogsService {
     );
   }
 
-  createTags(tag: Tag) {
+  createTags(tag: string) {
     const req = {
       request_body: {
         tag,
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 d01c6d3..c4671ce 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
@@ -516,12 +516,6 @@ export class PublishToMarketplacePageComponent implements OnInit {
       .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({
@@ -555,27 +549,45 @@ export class PublishToMarketplacePageComponent implements OnInit {
   }
 
   addTag(event: MatChipInputEvent): void {
-    const value = { tag: (event.value || '').trim() };
+    if (!(this.allTags().indexOf(event.value) > -1)) {
+      this.privateCatalogsService.createTags(event.value).subscribe({
+        next: (res) => {
+          const alert: Alert = {
+            message: 'Tag created successfully',
+            type: AlertType.Success,
+          };
+          this.alertService.notify(alert, 5000);
+          this.alertService.notify;
 
-    if (value) {
-      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: () => {},
-        });
+          this.privateCatalogsService.getAllTag().subscribe({
+            next: (res) => {
+              this.allTags.update((tags: string[]) => [...res]);
+              this.filteredTags.next(this.allTags());
+            },
+            error: () => {},
+          });
+        },
+        error: () => {},
+      });
     }
+    this.privateCatalogsService.addTag(event.value, this.solutionId).subscribe({
+      next: (res) => {
+        if (this.allTags().indexOf(event.value) > -1) {
+          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()),
+          );
+        }
+        this.updateTags();
+      },
+      error: () => {},
+    });
 
     // Clear the input value
     event.chipInput!.clear();
-- 
GitLab