6aa390fcf6e4d5b4deef0f5906c1e341ca1eb44c
[aai/sparky-fe.git] / src / editAttributes / EditAttributes.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 import React, {Component} from 'react';
22 import {connect} from 'react-redux';
23 import i18n from 'utils/i18n/i18n';
24
25 import InlineMessage from 'generic-components/InlineMessage/InlineMessage.jsx';
26 import {
27   clearFeebackMessage,
28   requestEditEntityAttributes
29 } from './EditAttributeActions.js';
30 import {
31   SET_ATTRIBUTE_TITLE,
32   ATTRIBUTE_MODIFICATION
33 } from './EditAttributeConstants.js';
34 import ChangeAttributeForm from 'editAttributes/changeAttributeForm/ChangeAttributeForm.jsx';
35 import {NO_VALUE_SELECTED} from 'editAttributes/changeAttributeForm/ChangeAttributeFormConstants.js';
36
37 let mapStateToProps = ({setAttributes}) => {
38   let {
39         feedbackMsgText = '',
40         feedbackMsgSeverity = ''
41       } = setAttributes;
42
43   return {
44     feedbackMsgText,
45     feedbackMsgSeverity
46   };
47 };
48
49 let mapActionToProps = (dispatch) => {
50   return {
51     handleSubmit: (values) => {
52       let uri = values.uri;
53       let attrMap = new Map();
54       attrMap.set('provStatus', 'prov-status');
55       attrMap.set('inMaint', 'in-maint');
56       attrMap.set('isClosedLoopDisabled', 'is-closed-loop-disabled');
57   
58       let attributes = {};
59       let valueString = JSON.stringify(values);
60       JSON.parse(valueString, (key, value) => {
61         if(value !== NO_VALUE_SELECTED) {
62           let formattedKey = attrMap.get(key);
63           if(formattedKey !== undefined) {
64             attributes = {
65               ...attributes, [formattedKey]: value
66             };
67           }
68           return value;
69         }
70       });
71       
72       dispatch(requestEditEntityAttributes(uri, attributes));
73     },
74     clearFeedbackMessage: () => {
75       dispatch(clearFeebackMessage());
76     }
77   };
78 };
79
80 class SetAttribute extends Component {
81   render() {
82     let {
83                         feedbackMsgText,
84                         feedbackMsgSeverity,
85                         handleSubmit,
86                         clearFeedbackMessage} = this.props;
87     return (
88       <div>
89         <div className='header'>
90           <div className='application-title'>{i18n(SET_ATTRIBUTE_TITLE)}</div>
91         </div>
92         <div className='secondary-header'>
93           <span
94             className='secondary-title'>{i18n(ATTRIBUTE_MODIFICATION)}</span>
95           <InlineMessage level={feedbackMsgSeverity}
96                          messageTxt={feedbackMsgText}/>
97         </div>
98
99         <ChangeAttributeForm
100           onSubmit={(values) => {
101             handleSubmit(values);
102           }}
103           buttonSelected={() => {
104             clearFeedbackMessage();
105           }}/>
106       </div>
107     );
108   }
109 }
110
111 export default connect(mapStateToProps, mapActionToProps)(SetAttribute);