changed the header and license
[aai/sparky-fe.git] / src / generic-components / notifications / NotificationModal.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 /**
22         * NotificationModal options:
23         *
24         * show: whether to show notification or not,
25         * type: the type of the notification. valid values are: 'default', 'error',
26         * 'warning', 'success' msg: the notification content. could be a string or
27         * node (React component) title: the notification title timeout: timeout for
28         * the notification to fade out. if timeout == 0 then the notification is
29         * rendered until the user closes it
30         *
31         */
32 import React, {Component, PropTypes} from 'react';
33 import {connect} from 'react-redux';
34 import Button from 'react-bootstrap/lib/Button.js';
35
36 import i18n from 'utils/i18n/i18n.js';
37 import Modal from 'generic-components/modal/Modal.jsx';
38 import NotificationConstants from './NotificationConstants.js';
39
40 let typeClass = {
41                 'default': 'primary',
42                 error: 'danger',
43                 warning: 'warning',
44                 success: 'success'
45 };
46
47 const mapActionsToProps = (dispatch) => {
48                 return {
49                                 onCloseClick: () => dispatch({type: NotificationConstants.NOTIFY_CLOSE})
50                 };
51 };
52
53 const mapStateToProps = ({notification}) => {
54                 
55                 let show = notification !== null && notification.title !== 'Conflict';
56                 let mapResult = {show};
57                 if (show) {
58                                 mapResult = {show, ...notification};
59                 }
60                 
61                 return mapResult;
62 };
63
64 class NotificationModal extends Component {
65                 
66                 static propTypes = {
67                                 show: PropTypes.bool,
68                                 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
69                                 msg: PropTypes.node,
70                                 title: PropTypes.string,
71                                 timeout: PropTypes.number
72                 };
73                 
74                 static defaultProps = {
75                                 show: false,
76                                 type: 'default',
77                                 title: '',
78                                 msg: '',
79                                 timeout: 0
80                 };
81                 
82                 state = {type: undefined};
83                 
84                 componentWillReceiveProps(nextProps) {
85                                 if (this.props.show !== nextProps.show && nextProps.show === false) {
86                                                 this.setState({type: this.props.type});
87                                 }
88                                 else {
89                                                 this.setState({type: undefined});
90                                 }
91                 }
92                 
93                 componentDidUpdate() {
94                                 if (this.props.timeout) {
95                                                 setTimeout(this.props.onCloseClick, this.props.timeout);
96                                 }
97                 }
98                 
99                 render() {
100                                 let {title, type, msg, show} = this.props;
101                                 if (!show) {
102                                                 type = this.state.type;
103                                 }
104                                 return (
105                                                 <Modal show={this.props.show}
106                                                        className={`notification-modal ${typeClass[type]}`}>
107                                                                 <Modal.Header>
108                                                                                 <Modal.Title>{title}</Modal.Title>
109                                                                 </Modal.Header>
110                                                                 <Modal.Body>{msg}</Modal.Body>
111                                                                 <Modal.Footer>
112                                                                                 <Button bsStyle={typeClass[type]}
113                                                                                         onClick={this.props.onCloseClick}>{i18n('OK')}</Button>
114                                                                 </Modal.Footer>
115                                                 </Modal>
116                                 );
117                 }
118 }
119
120 export default connect(mapStateToProps, mapActionsToProps)(NotificationModal);