Provide UI support to upload csar to update service
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / tosca-artifacts / tosca-artifact-page.component.ts
1 import { Component, Inject, OnInit, ViewChild } from "@angular/core";
2 import { WorkspaceService } from "../workspace.service";
3 import { ArtifactModel } from "../../../../models";
4 import { Select, Store } from "@ngxs/store";
5 import { WorkspaceState } from "../../../store/states/workspace.state";
6 import { ArtifactGroupType } from "../../../../utils";
7 import { GetArtifactsByTypeAction } from "../../../store/actions/artifacts.action";
8 import { Observable } from "rxjs/index";
9 import { ArtifactsState } from "../../../store/states/artifacts.state";
10 import { map } from "rxjs/operators";
11 import { ArtifactType, ComponentState, ComponentType } from "app/utils/constants"
12 import { TopologyTemplateService } from "app/ng2/services/component-services/topology-template.service";
13
14 @Component({
15     selector: 'tosca-artifact-page',
16
17     templateUrl: './tosca-artifact-page.component.html',
18     styleUrls: ['./tosca-artifact-page.component.less', '../../../../../assets/styles/table-style.less']
19 })
20 export class ToscaArtifactPageComponent implements OnInit {
21
22     @Select(WorkspaceState.isViewOnly) isViewOnly$: boolean;
23     @ViewChild('toscaArtifactsTable') table: any;
24     public toscaArtifacts$: Observable<ArtifactModel[]>;
25     public componentId: string;
26     public componentType:string;
27     public isLoading: boolean = false;
28
29     constructor(
30         private workspaceService: WorkspaceService,
31         private store: Store,
32         @Inject("Notification") private Notification: any,
33         private componentService: TopologyTemplateService) {
34     }
35
36
37     ngOnInit(): void {
38         this.componentId = this.workspaceService.metadata.uniqueId;
39         this.componentType = this.workspaceService.metadata.componentType;
40
41         this.store.dispatch(new GetArtifactsByTypeAction({componentType:this.componentType, componentId:this.componentId, artifactType:ArtifactGroupType.TOSCA}));
42         this.toscaArtifacts$ = this.store.select(ArtifactsState.getArtifactsByType).pipe(map(filterFn => filterFn(ArtifactGroupType.TOSCA)));
43     }
44
45     onActivate(event) {
46         if(event.type === 'click'){
47             this.table.rowDetail.toggleExpandRow(event.row);
48         }
49     }
50
51     getExtension(artifactType: string) {
52         switch (artifactType) {
53             case (ArtifactType.TOSCA.TOSCA_CSAR):
54                 return "csar";
55             case (ArtifactType.TOSCA.TOSCA_TEMPLATE):
56                 return "yaml,yml";
57         }
58     }
59
60     isService() {
61         return ComponentType.SERVICE === this.componentType;
62     }
63
64     isCheckedOut() {
65         return this.workspaceService.metadata.lifecycleState === ComponentState.NOT_CERTIFIED_CHECKOUT;
66     }
67
68     onFileUpload(file, artifactType) {
69         if (file && file.name) {
70             this.isLoading = true;
71             switch (artifactType) {
72                 case (ArtifactType.TOSCA.TOSCA_CSAR):
73                     this.componentService.putServiceToscaModel(this.componentId, this.componentType, file).subscribe((response)=> {
74                         this.Notification.success({
75                             message: "Service " + response.name + " has been updated",
76                             title: "Success"
77                         });
78                         this.isLoading = false;
79                     }, () => {
80                         this.isLoading = false;
81                     });
82                     break;
83                 case (ArtifactType.TOSCA.TOSCA_TEMPLATE):
84                     this.componentService.putServiceToscaTemplate(this.componentId, this.componentType, file).subscribe((response)=> {
85                         this.Notification.success({
86                             message: "Service " + response.name + " has been updated",
87                             title: "Success"
88                         });
89                         this.isLoading = false;
90                     }, () => {
91                         this.isLoading = false;
92                     });
93                     break;
94             }
95         }
96     }
97 }