9ffc46c1ad5806524c176907ee57e0393f11837e
[sdc/sdc-workflow-designer.git] /
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 React from 'react';
18 import PropTypes from 'prop-types';
19 import { isEmpty } from 'lodash';
20
21 const VersionSelect = props => {
22     const {
23         currentWorkflowVersion,
24         viewableVersions,
25         dynamicDispatcher
26     } = props;
27
28     function onChangeHandler(ev) {
29         const versionIndex = Object.keys(viewableVersions).find(
30             key => viewableVersions[key].id === ev.target.value
31         );
32         const currentVersion = viewableVersions[versionIndex].id;
33         dynamicDispatcher('CHANGE_VERSION', currentVersion);
34         return currentVersion;
35     }
36
37     return (
38         <select
39             className="version-selector"
40             key={'selector'}
41             value={currentWorkflowVersion}
42             onChange={onChangeHandler}
43             data-test-id="vc-versions-select-box">
44             {!isEmpty(viewableVersions) &&
45                 viewableVersions.map(item => {
46                     return (
47                         <option
48                             key={'versionSelect' + item.id}
49                             value={item.id}
50                             data-test-id="vc-version-option">
51                             {item.displayed || item.name}
52                         </option>
53                     );
54                 })}
55         </select>
56     );
57 };
58
59 VersionSelect.propTypes = {
60     currentWorkflowVersion: PropTypes.string,
61     viewableVersions: PropTypes.arrayOf(Object),
62     dynamicDispatcher: PropTypes.func
63 };
64
65 export default VersionSelect;