Migrate sdc-sdc-workflow-designer docs
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / inputOutput / inputOutputSelectors.js
1 /*
2 * Copyright © 2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import { createSelector } from 'reselect';
18 import isEmpty from 'lodash.isempty';
19
20 import { INPUTS } from 'features/version/inputOutput/inputOutputConstants';
21
22 export const getInputOutput = state => state.currentVersion.inputOutput;
23 export const getInputs = createSelector(getInputOutput, data => data.inputs);
24 export const getOutputs = createSelector(getInputOutput, data => data.outputs);
25 export const getInputOutputForComposition = state => ({
26     inputs: getInputs(state).map(item => ({
27         ...item,
28         type: item.type.toLowerCase()
29     })),
30     outputs: getOutputs(state).map(item => ({
31         ...item,
32         type: item.type.toLowerCase()
33     }))
34 });
35 export const getCurrent = createSelector(
36     getInputOutput,
37     inputOutput => inputOutput.current
38 );
39
40 export const getIsShowInputs = createSelector(
41     getCurrent,
42     current => current === INPUTS
43 );
44
45 export const getSearch = createSelector(
46     getInputOutput,
47     inputOutput => inputOutput.search
48 );
49
50 export const getDataRows = createSelector(
51     [getInputOutput, getCurrent],
52     (inputOutput, current) => {
53         if (inputOutput.search) {
54             return inputOutput[current].filter(dataRow =>
55                 dataRow.name
56                     .toLowerCase()
57                     .includes(inputOutput.search.toLowerCase())
58             );
59         }
60
61         return inputOutput[current];
62     }
63 );
64
65 export const getTypes = createSelector(
66     getInputOutput,
67     inputOutput => inputOutput.types
68 );
69
70 export const getError = createSelector(
71     [getInputOutput, getCurrent],
72     (inputOutput, current) => inputOutput.error[current]
73 );
74
75 export const getErrorsInputOutput = createSelector(
76     getInputOutput,
77     ({ error }) => error
78 );
79
80 export const getInputErrors = createSelector(
81     getErrorsInputOutput,
82     ({ inputs }) =>
83         !isEmpty(inputs) &&
84         Boolean(
85             inputs.alreadyExists.length ||
86                 inputs.invalidCharacters.length ||
87                 inputs.emptyName.length
88         )
89 );
90
91 export const getOutputErrors = createSelector(
92     getErrorsInputOutput,
93     ({ outputs }) =>
94         !isEmpty(outputs) &&
95         Boolean(
96             outputs.alreadyExists.length ||
97                 outputs.invalidCharacters.length ||
98                 outputs.emptyName.length
99         )
100 );
101
102 export const getIOErrors = createSelector(
103     getInputErrors,
104     getOutputErrors,
105     (inputsErrors, outputsErrors) => inputsErrors || outputsErrors
106 );