aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/store/states/artifacts.state.ts
blob: 64efbe96a96b5b802f559d4751092d232200379f (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * Created by ob0695 on 7/17/2018.
 */
import { Action, Selector, State, StateContext } from '@ngxs/store';
import * as _ from 'lodash';
import { tap } from 'rxjs/operators';
import { ArtifactModel } from '../../../models/artifacts';
import { ArtifactGroupType } from '../../../utils/constants';
import { TopologyTemplateService } from '../../services/component-services/topology-template.service';
import { ComponentGenericResponse } from '../../services/responses/component-generic-response';
import { ServiceGenericResponse } from '../../services/responses/service-generic-response';
import { CreateOrUpdateArtifactAction, DeleteArtifactAction, GetArtifactsByTypeAction } from '../actions/artifacts.action';

export interface ArtifactsStateModel {
    artifacts: ArtifactModel[];
    deploymentArtifacts: ArtifactModel[];
    toscaArtifacts: ArtifactModel[];
    serviceApiArtifacts: ArtifactModel[];
}

@State<ArtifactsStateModel>({
    name: 'artifacts',
    defaults: {
        artifacts: [],
        deploymentArtifacts: [],
        toscaArtifacts: [],
        serviceApiArtifacts: []
    }
})

export class ArtifactsState {

    constructor(protected topologyTemplateService: TopologyTemplateService) {
    }

    @Selector()
    static getEnvArtifact(state: ArtifactsStateModel, heatEnvArtifact: ArtifactModel) {
        return (heatEnvArtifact: ArtifactModel) => {
            _.find(state.deploymentArtifacts, (artifact)=> {
                return artifact.generatedFromId === heatEnvArtifact.uniqueId
            })
        };
    }

    @Selector()
    static getArtifactsByType(state: ArtifactsStateModel, type: string) {
        return (type: string) => {
            switch (type) {
                case ArtifactGroupType.TOSCA:
                    return state.toscaArtifacts;
                case ArtifactGroupType.INFORMATION:
                    return state.artifacts;
                case ArtifactGroupType.DEPLOYMENT:
                    return state.deploymentArtifacts;
                case ArtifactGroupType.SERVICE_API:
                    return state.serviceApiArtifacts;
            }
        };
    }

    private updateArtifactState = (artifactsState: ArtifactModel[], artifactToUpdate: ArtifactModel, updatedArtifact: ArtifactModel) => {
        if (!artifactToUpdate.uniqueId) { // Create Artifact
            return [...artifactsState, updatedArtifact]
        } else { // Update Artifact
            let artifactToUpdateIndex = _.findIndex(artifactsState, (artifact) => {
                return artifact.uniqueId === artifactToUpdate.uniqueId
            })
            let artifacts = Array.from(artifactsState);
            artifacts[artifactToUpdateIndex] = updatedArtifact;
            return [...artifacts];
        }
    }

    @Action(GetArtifactsByTypeAction)
    getArtifactsByType({getState, patchState}: StateContext<ArtifactsStateModel>, action: GetArtifactsByTypeAction) {
        const state = getState();
        return this.topologyTemplateService.getArtifactsByType(action.payload.componentType, action.payload.componentId, action.payload.artifactType)
            .pipe(tap((resp: ComponentGenericResponse) => {
                switch (action.payload.artifactType) {
                    case ArtifactGroupType.INFORMATION:
                        patchState({
                            artifacts: <ArtifactModel[]>_.values(resp.artifacts)
                        });

                    case ArtifactGroupType.DEPLOYMENT:
                        patchState({
                            deploymentArtifacts: <ArtifactModel[]>_.values(resp.deploymentArtifacts)
                        });

                    case ArtifactGroupType.TOSCA:
                        patchState({
                            toscaArtifacts: <ArtifactModel[]>_.values(resp.toscaArtifacts)
                        });

                    case ArtifactGroupType.SERVICE_API:
                        patchState({
                            serviceApiArtifacts: <ArtifactModel[]>_.values((<ServiceGenericResponse>resp).serviceApiArtifacts)
                        });
                }
            }));
    }

    @Action(CreateOrUpdateArtifactAction)
    createOrUpdateArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: CreateOrUpdateArtifactAction) {
        const state = getState();
        return this.topologyTemplateService.addOrUpdateArtifact(action.payload.componentType, action.payload.componentId, action.payload.artifact)
            .pipe(tap((resp: ArtifactModel) => {

                switch (resp.artifactGroupType) {
                    case ArtifactGroupType.DEPLOYMENT:
                        patchState({
                            deploymentArtifacts: this.updateArtifactState(state.deploymentArtifacts, action.payload.artifact, resp)
                        });

                    case ArtifactGroupType.INFORMATION:
                        patchState({
                            artifacts: this.updateArtifactState(state.artifacts, action.payload.artifact, resp)
                        });
                }
            }));
    }

    @Action(DeleteArtifactAction)
    deleteArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: DeleteArtifactAction) {
        const state = getState();
        return this.topologyTemplateService.deleteArtifact(action.payload.componentId, action.payload.componentType, action.payload.artifact.uniqueId, action.payload.artifact.artifactLabel)
            .pipe(tap((resp: ArtifactModel) => {
                switch (resp.artifactGroupType) {
                    case ArtifactGroupType.DEPLOYMENT:
                        patchState({
                            deploymentArtifacts: state.deploymentArtifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
                        });
                    case ArtifactGroupType.INFORMATION:
                        patchState({
                            artifacts: state.artifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
                        });
                }
            }));
    }
}