blob: a47c42a1fba4fab3d388ad73ad2432fc19815a5a [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';
talig8e9c0652017-12-20 14:30:43 +020018import PropTypes from 'prop-types';
AviZi280f8012017-06-09 02:39:56 +030019import {connect} from 'react-redux';
20
21import Modal from 'nfvo-components/modal/Modal.jsx';
Avi Zivb8e2faf2017-07-18 19:45:38 +030022import Button from 'sdc-ui/lib/react/Button.js';
AviZi280f8012017-06-09 02:39:56 +030023import i18n from 'nfvo-utils/i18n/i18n.js';
24import {modalContentComponents} from 'sdc-app/common/modal/ModalContentMapper.js';
25import {actionTypes, typeEnum} from './GlobalModalConstants.js';
26
27
28const typeClass = {
talig8e9c0652017-12-20 14:30:43 +020029 'default': 'primary',
Avi Zivb8e2faf2017-07-18 19:45:38 +030030 error: 'negative',
AviZi280f8012017-06-09 02:39:56 +030031 warning: 'warning',
Avi Zivb8e2faf2017-07-18 19:45:38 +030032 success: 'positive'
AviZi280f8012017-06-09 02:39:56 +030033};
34
az2497644017c2017-08-10 17:49:40 +030035const type2HeaderColor = {
36 'default': 'primary',
37 error: 'danger',
38 warning: 'warning',
39 success: 'success'
40};
AviZi280f8012017-06-09 02:39:56 +030041
az2497644017c2017-08-10 17:49:40 +030042
43const ModalFooter = ({type, onConfirmed, onDeclined, onClose, confirmationButtonText, cancelButtonText}) => {
44 let myPropsForNoConfirmed = {};
45 if (onConfirmed) {
46 myPropsForNoConfirmed.btnType = 'outline';
47 }
48 return (
AviZi280f8012017-06-09 02:39:56 +030049 <Modal.Footer>
Avi Zivb8e2faf2017-07-18 19:45:38 +030050 <div className='sdc-modal-footer'>
talig8e9c0652017-12-20 14:30:43 +020051 {onConfirmed && <Button data-test-id='sdc-modal-confirm-button' color={typeClass[type]} onClick={() => {
Avi Zivb8e2faf2017-07-18 19:45:38 +030052 onConfirmed();
53 onClose();
54 }}>{confirmationButtonText}</Button>}
talig8e9c0652017-12-20 14:30:43 +020055 <Button {...myPropsForNoConfirmed} data-test-id='sdc-modal-cancel-button' btnType='outline' color={typeClass[type]} onClick={onDeclined ? () => {
Avi Zivb8e2faf2017-07-18 19:45:38 +030056 onDeclined();
57 onClose();} : () => onClose()}>
58 {cancelButtonText}
AviZi280f8012017-06-09 02:39:56 +030059 </Button>
Avi Zivb8e2faf2017-07-18 19:45:38 +030060 </div>
az2497644017c2017-08-10 17:49:40 +030061 </Modal.Footer>
62 );
63};
AviZi280f8012017-06-09 02:39:56 +030064
65ModalFooter.defaultProps = {
66 type: 'default',
67 confirmationButtonText: i18n('OK'),
68 cancelButtonText: i18n('Cancel')
69};
70
71export const mapStateToProps = ({modal}) => {
72 const show = !!modal;
73 return {
74 show,
75 ...modal
76 };
77};
78
79export const mapActionToProps = (dispatch) => {
80 return {
81 onClose: () => dispatch({type: actionTypes.GLOBAL_MODAL_CLOSE})
82 };
83};
84
85
86export class GlobalModalView extends React.Component {
87
88 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020089 show: PropTypes.bool,
90 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
91 title: PropTypes.string,
92 modalComponentProps: PropTypes.object,
93 modalComponentName: PropTypes.string,
94 onConfirmed: PropTypes.func,
95 onDeclined: PropTypes.func,
96 confirmationButtonText: PropTypes.string,
97 cancelButtonText: PropTypes.string
AviZi280f8012017-06-09 02:39:56 +030098 };
99
100 static defaultProps = {
101 show: false,
102 type: 'default',
103 title: ''
104 };
105
106 render() {
Avi Zivb8e2faf2017-07-18 19:45:38 +0300107 let {title, type, show, modalComponentName, modalComponentProps,
AviZi280f8012017-06-09 02:39:56 +0300108 modalClassName, msg, onConfirmed, onDeclined, confirmationButtonText, cancelButtonText, onClose} = this.props;
109 const ComponentToRender = modalContentComponents[modalComponentName];
110 return (
az2497644017c2017-08-10 17:49:40 +0300111 <Modal show={show} bsSize={modalComponentProps && modalComponentProps.size} className={`onborading-modal ${modalClassName || ''} ${type2HeaderColor[type]}`}>
AviZi280f8012017-06-09 02:39:56 +0300112 <Modal.Header>
113 <Modal.Title>{title}</Modal.Title>
114 </Modal.Header>
115 <Modal.Body>
miriame6511b4b2017-10-22 15:14:44 +0300116 {ComponentToRender ?
117 <ComponentToRender {...modalComponentProps}/> :
miriamedee95852017-10-26 14:31:47 +0300118 msg && typeof msg === 'string' ?
talig8e9c0652017-12-20 14:30:43 +0200119 <div> {msg.split('\n').map((txt, i) => <span key={i}> {txt} <br/> </span>)} </div> :
miriamedee95852017-10-26 14:31:47 +0300120 msg
miriame6511b4b2017-10-22 15:14:44 +0300121 }
AviZi280f8012017-06-09 02:39:56 +0300122 </Modal.Body>
123 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) &&
124 <ModalFooter
125 type={type}
126 onConfirmed={onConfirmed}
127 onDeclined={onDeclined}
128 onClose={onClose}
129 confirmationButtonText={confirmationButtonText}
130 cancelButtonText={cancelButtonText}/>}
131 </Modal>
132 );
133 }
134
135 componentDidUpdate() {
136 if (this.props.timeout) {
137 setTimeout(this.props.onClose, this.props.timeout);
138 }
Avi Zivb8e2faf2017-07-18 19:45:38 +0300139 }
AviZi280f8012017-06-09 02:39:56 +0300140};
141
142export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(GlobalModalView);