blob: 825cc609a82ebaffa266507ff8d24a5c85bb27d3 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17import React from 'react';
18import {connect} from 'react-redux';
19
20import Modal from 'nfvo-components/modal/Modal.jsx';
Avi Zivb8e2faf2017-07-18 19:45:38 +030021import Button from 'sdc-ui/lib/react/Button.js';
AviZi280f8012017-06-09 02:39:56 +030022import i18n from 'nfvo-utils/i18n/i18n.js';
23import {modalContentComponents} from 'sdc-app/common/modal/ModalContentMapper.js';
24import {actionTypes, typeEnum} from './GlobalModalConstants.js';
25
26
27const typeClass = {
Avi Zivb8e2faf2017-07-18 19:45:38 +030028 'default': 'default',
29 error: 'negative',
AviZi280f8012017-06-09 02:39:56 +030030 warning: 'warning',
Avi Zivb8e2faf2017-07-18 19:45:38 +030031 success: 'positive'
AviZi280f8012017-06-09 02:39:56 +030032};
33
34
Avi Zivb8e2faf2017-07-18 19:45:38 +030035const ModalFooter = ({type, onConfirmed, onDeclined, onClose, confirmationButtonText, cancelButtonText}) =>
AviZi280f8012017-06-09 02:39:56 +030036 <Modal.Footer>
Avi Zivb8e2faf2017-07-18 19:45:38 +030037 <div className='sdc-modal-footer'>
38 {onConfirmed && <Button color={typeClass[type]} onClick={() => {
39 onConfirmed();
40 onClose();
41 }}>{confirmationButtonText}</Button>}
42 <Button btnType='outline' color={typeClass[type]} onClick={onDeclined ? () => {
43 onDeclined();
44 onClose();} : () => onClose()}>
45 {cancelButtonText}
AviZi280f8012017-06-09 02:39:56 +030046 </Button>
Avi Zivb8e2faf2017-07-18 19:45:38 +030047 </div>
AviZi280f8012017-06-09 02:39:56 +030048 </Modal.Footer>;
49
50ModalFooter.defaultProps = {
51 type: 'default',
52 confirmationButtonText: i18n('OK'),
53 cancelButtonText: i18n('Cancel')
54};
55
56export const mapStateToProps = ({modal}) => {
57 const show = !!modal;
58 return {
59 show,
60 ...modal
61 };
62};
63
64export const mapActionToProps = (dispatch) => {
65 return {
66 onClose: () => dispatch({type: actionTypes.GLOBAL_MODAL_CLOSE})
67 };
68};
69
70
71export class GlobalModalView extends React.Component {
72
73 static propTypes = {
74 show: React.PropTypes.bool,
75 type: React.PropTypes.oneOf(['default', 'error', 'warning', 'success']),
76 title: React.PropTypes.string,
77 modalComponentProps: React.PropTypes.object,
78 modalComponentName: React.PropTypes.string,
79 onConfirmed: React.PropTypes.func,
80 onDeclined: React.PropTypes.func,
81 confirmationButtonText: React.PropTypes.string,
82 cancelButtonText: React.PropTypes.string
83 };
84
85 static defaultProps = {
86 show: false,
87 type: 'default',
88 title: ''
89 };
90
91 render() {
Avi Zivb8e2faf2017-07-18 19:45:38 +030092 let {title, type, show, modalComponentName, modalComponentProps,
AviZi280f8012017-06-09 02:39:56 +030093 modalClassName, msg, onConfirmed, onDeclined, confirmationButtonText, cancelButtonText, onClose} = this.props;
94 const ComponentToRender = modalContentComponents[modalComponentName];
95 return (
96 <Modal show={show} bsSize={modalComponentProps && modalComponentProps.size} className={`onborading-modal ${modalClassName || ''} ${typeClass[type]}`}>
97 <Modal.Header>
98 <Modal.Title>{title}</Modal.Title>
99 </Modal.Header>
100 <Modal.Body>
Avi Zivb8e2faf2017-07-18 19:45:38 +0300101 {ComponentToRender ? <ComponentToRender {...modalComponentProps}/> : msg}
AviZi280f8012017-06-09 02:39:56 +0300102 </Modal.Body>
103 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) &&
104 <ModalFooter
105 type={type}
106 onConfirmed={onConfirmed}
107 onDeclined={onDeclined}
108 onClose={onClose}
109 confirmationButtonText={confirmationButtonText}
110 cancelButtonText={cancelButtonText}/>}
111 </Modal>
112 );
113 }
114
115 componentDidUpdate() {
116 if (this.props.timeout) {
117 setTimeout(this.props.onClose, this.props.timeout);
118 }
Avi Zivb8e2faf2017-07-18 19:45:38 +0300119 }
AviZi280f8012017-06-09 02:39:56 +0300120};
121
122export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(GlobalModalView);