aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/packages-dashboard/import-package/import-package.component.ts
blob: 7496338d6afaccab1bbabd00186c785aaf90bbab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {Component, OnInit} from '@angular/core';
import {FileSystemFileEntry, NgxFileDropEntry} from 'ngx-file-drop';
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',
    templateUrl: './import-package.component.html',
    styleUrls: ['./import-package.component.css']
})
export class ImportPackageComponent implements OnInit {

    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) {

    }

    ngOnInit() {

    }

    removeInitFile(index) {
        this.uploadedFiles.splice(index, 1);
    }

    public dropped(files: NgxFileDropEntry[]) {
        this.files = files;
        for (const droppedFile of files) {
            // Is it a file? & Not added before
            if (droppedFile.fileEntry.isFile) {
                const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
                this.uploadedFiles.push(fileEntry);
                console.log(fileEntry.name);
                this.fileNames.add(fileEntry.name);

            }
        }
    }

    initDelete(file) {
        console.log(file);
        this.fileToDelete = file;
    }

    removeFile() {
        const filename = this.fileToDelete.key;
        for (let i = 0; i < this.uploadedFiles.length; i++) {
            console.log(this.uploadedFiles[i]);
            if (this.uploadedFiles[i].name === filename) {
                this.uploadedFiles.splice(i, 1);
                break;
            }
        }
    }

    resetTheUploadedFiles() {
        this.uploadedFiles = [];
    }


    public fileOver(event) {
        console.log(event);
    }

    public fileLeave(event) {
        console.log(event);
    }

    importPackageAndViewIt() {
        this.openFilesInCreationPackage();
        this.saveFileToStore();
    }

    saveFileToStore() {
        console.log(this.uploadedFiles.length);
        const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
        this.packageCreationStore.clear();
        this.packageCreationExtractionService.extractBlobToStore(file);
    }

    openFilesInCreationPackage() {
        this.router.navigate(['/packages/createPackage/']);
    }

    async getFile(fileEntry) {
        try {
            return await new Promise((resolve, reject) => fileEntry.file(resolve, reject));
        } catch (err) {
            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));
            });
        });
    }
}