Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / store / states / artifacts.state.ts
1 /**
2  * Created by ob0695 on 7/17/2018.
3  */
4 import { Action, Selector, State, StateContext } from '@ngxs/store';
5 import * as _ from 'lodash';
6 import { tap } from 'rxjs/operators';
7 import { ArtifactModel } from '../../../models/artifacts';
8 import { ArtifactGroupType } from '../../../utils/constants';
9 import { TopologyTemplateService } from '../../services/component-services/topology-template.service';
10 import { ComponentGenericResponse } from '../../services/responses/component-generic-response';
11 import { ServiceGenericResponse } from '../../services/responses/service-generic-response';
12 import { CreateOrUpdateArtifactAction, DeleteArtifactAction, GetArtifactsByTypeAction } from '../actions/artifacts.action';
13
14 export interface ArtifactsStateModel {
15     artifacts: ArtifactModel[];
16     deploymentArtifacts: ArtifactModel[];
17     toscaArtifacts: ArtifactModel[];
18     serviceApiArtifacts: ArtifactModel[];
19 }
20
21 @State<ArtifactsStateModel>({
22     name: 'artifacts',
23     defaults: {
24         artifacts: [],
25         deploymentArtifacts: [],
26         toscaArtifacts: [],
27         serviceApiArtifacts: []
28     }
29 })
30
31 export class ArtifactsState {
32
33     constructor(protected topologyTemplateService: TopologyTemplateService) {
34     }
35
36     @Selector()
37     static getEnvArtifact(state: ArtifactsStateModel, heatEnvArtifact: ArtifactModel) {
38         return (heatEnvArtifact: ArtifactModel) => {
39             _.find(state.deploymentArtifacts, (artifact)=> {
40                 return artifact.generatedFromId === heatEnvArtifact.uniqueId
41             })
42         };
43     }
44
45     @Selector()
46     static getArtifactsByType(state: ArtifactsStateModel, type: string) {
47         return (type: string) => {
48             switch (type) {
49                 case ArtifactGroupType.TOSCA:
50                     return state.toscaArtifacts;
51                 case ArtifactGroupType.INFORMATION:
52                     return state.artifacts;
53                 case ArtifactGroupType.DEPLOYMENT:
54                     return state.deploymentArtifacts;
55                 case ArtifactGroupType.SERVICE_API:
56                     return state.serviceApiArtifacts;
57             }
58         };
59     }
60
61     private updateArtifactState = (artifactsState: ArtifactModel[], artifactToUpdate: ArtifactModel, updatedArtifact: ArtifactModel) => {
62         if (!artifactToUpdate.uniqueId) { // Create Artifact
63             return [...artifactsState, updatedArtifact]
64         } else { // Update Artifact
65             let artifactToUpdateIndex = _.findIndex(artifactsState, (artifact) => {
66                 return artifact.uniqueId === artifactToUpdate.uniqueId
67             })
68             let artifacts = Array.from(artifactsState);
69             artifacts[artifactToUpdateIndex] = updatedArtifact;
70             return [...artifacts];
71         }
72     }
73
74     @Action(GetArtifactsByTypeAction)
75     getArtifactsByType({getState, patchState}: StateContext<ArtifactsStateModel>, action: GetArtifactsByTypeAction) {
76         const state = getState();
77         return this.topologyTemplateService.getArtifactsByType(action.payload.componentType, action.payload.componentId, action.payload.artifactType)
78             .pipe(tap((resp: ComponentGenericResponse) => {
79                 switch (action.payload.artifactType) {
80                     case ArtifactGroupType.INFORMATION:
81                         patchState({
82                             artifacts: <ArtifactModel[]>_.values(resp.artifacts)
83                         });
84
85                     case ArtifactGroupType.DEPLOYMENT:
86                         patchState({
87                             deploymentArtifacts: <ArtifactModel[]>_.values(resp.deploymentArtifacts)
88                         });
89
90                     case ArtifactGroupType.TOSCA:
91                         patchState({
92                             toscaArtifacts: <ArtifactModel[]>_.values(resp.toscaArtifacts)
93                         });
94
95                     case ArtifactGroupType.SERVICE_API:
96                         patchState({
97                             serviceApiArtifacts: <ArtifactModel[]>_.values((<ServiceGenericResponse>resp).serviceApiArtifacts)
98                         });
99                 }
100             }));
101     }
102
103     @Action(CreateOrUpdateArtifactAction)
104     createOrUpdateArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: CreateOrUpdateArtifactAction) {
105         const state = getState();
106         return this.topologyTemplateService.addOrUpdateArtifact(action.payload.componentType, action.payload.componentId, action.payload.artifact)
107             .pipe(tap((resp: ArtifactModel) => {
108
109                 switch (resp.artifactGroupType) {
110                     case ArtifactGroupType.DEPLOYMENT:
111                         patchState({
112                             deploymentArtifacts: this.updateArtifactState(state.deploymentArtifacts, action.payload.artifact, resp)
113                         });
114
115                     case ArtifactGroupType.INFORMATION:
116                         patchState({
117                             artifacts: this.updateArtifactState(state.artifacts, action.payload.artifact, resp)
118                         });
119                 }
120             }));
121     }
122
123     @Action(DeleteArtifactAction)
124     deleteArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: DeleteArtifactAction) {
125         const state = getState();
126         return this.topologyTemplateService.deleteArtifact(action.payload.componentId, action.payload.componentType, action.payload.artifact.uniqueId, action.payload.artifact.artifactLabel)
127             .pipe(tap((resp: ArtifactModel) => {
128                 switch (resp.artifactGroupType) {
129                     case ArtifactGroupType.DEPLOYMENT:
130                         patchState({
131                             deploymentArtifacts: state.deploymentArtifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
132                         });
133                     case ArtifactGroupType.INFORMATION:
134                         patchState({
135                             artifacts: state.artifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
136                         });
137                 }
138             }));
139     }
140 }