adding save automatic with import

Issue-ID: CCSDK-2842
Signed-off-by: ShaabanEltanany <shaaban.eltanany.ext@orange.com>
Change-Id: I8b87cf0472b2ddd3e05558fe46b8265200eb8f28
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.html b/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.html
index 274435c..d578582 100644
--- a/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.html
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.html
@@ -45,9 +45,14 @@
                         (click)="resetTheUploadedFiles()">Cancel
                 </button>
                 <button type="button" class="btn btn-sm btn-primary" [disabled]="uploadedFiles?.length<=0"
-                        data-dismiss="modal" (click)="openFilesInCreationPackage();saveFileToStore()">
+                        data-dismiss="modal" (click)="importAndSave()">
+                    Import&Save
+                </button>
+                <button type="button" class="btn btn-sm btn-primary" [disabled]="uploadedFiles?.length<=0"
+                        data-dismiss="modal" (click)="importPackageAndViewIt()">
                     Import
                 </button>
+
             </div>
         </div>
     </div>
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.ts
index dae58a4..7496338 100644
--- a/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.ts
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.ts
@@ -3,6 +3,10 @@
 import {PackageCreationExtractionService} from '../../package-creation/package-creation-extraction.service';
 import {Router} from '@angular/router';
 import {PackageCreationStore} from '../../package-creation/package-creation.store';
+import * as JSZip from 'jszip';
+import {PackageCreationService} from '../../package-creation/package-creation.service';
+import {ToastrService} from 'ngx-toastr';
+import {PackagesStore} from '../../packages.store';
 
 @Component({
     selector: 'app-import-package',
@@ -14,10 +18,14 @@
     public uploadedFiles: FileSystemFileEntry[] = [];
     private fileNames: Set<string> = new Set();
     fileToDelete: any = {};
+    zipFile: JSZip = new JSZip();
     public files: NgxFileDropEntry[] = [];
 
     constructor(private packageCreationExtractionService: PackageCreationExtractionService,
                 private packageCreationStore: PackageCreationStore,
+                private packageCreationService: PackageCreationService,
+                private toastService: ToastrService,
+                private packagesStore: PackagesStore,
                 private router: Router) {
 
     }
@@ -72,7 +80,12 @@
     public fileLeave(event) {
         console.log(event);
     }
-// TODO mix two function in ond bigger one
+
+    importPackageAndViewIt() {
+        this.openFilesInCreationPackage();
+        this.saveFileToStore();
+    }
+
     saveFileToStore() {
         console.log(this.uploadedFiles.length);
         const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
@@ -91,4 +104,23 @@
             console.log(err);
         }
     }
+
+    importAndSave() {
+        const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
+        this.zipFile = new JSZip();
+        this.zipFile.loadAsync(file).then(zip => {
+            this.zipFile = zip;
+            console.log(this.zipFile);
+            this.resetTheUploadedFiles();
+            this.zipFile.generateAsync({type: 'blob'}).then(blob => {
+                this.packageCreationService.savePackage(blob).subscribe(
+                    bluePrintDetailModels => {
+                        this.toastService.info('package is imported and saved successfully ');
+                        this.router.navigate(['/packages']);
+                        this.packagesStore.getAll();
+                    }, error =>
+                        this.toastService.error('there is an error happened ' + error));
+            });
+        });
+    }
 }