blob: 75b7983ac3e11e6aaaf26a6e4638cea2ea2b31e6 [file] [log] [blame]
svishnev1eb66b72018-01-11 14:39:45 +02001/*
2 * Copyright © 2016-2017 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
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
svishnev1eb66b72018-01-11 14:39:45 +02007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
AviZi280f8012017-06-09 02:39:56 +030010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnev1eb66b72018-01-11 14:39:45 +020012 * 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.
AviZi280f8012017-06-09 02:39:56 +030015 */
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
svishnev1eb66b72018-01-11 14:39:45 +020071ModalFooter.PropTypes = {
72 type: PropTypes.string,
73 confirmationButtonText: PropTypes.string,
74 cancelButtonText: PropTypes.string
75};
76
AviZi280f8012017-06-09 02:39:56 +030077export const mapStateToProps = ({modal}) => {
78 const show = !!modal;
79 return {
80 show,
81 ...modal
82 };
83};
84
85export const mapActionToProps = (dispatch) => {
86 return {
87 onClose: () => dispatch({type: actionTypes.GLOBAL_MODAL_CLOSE})
88 };
89};
90
91
92export class GlobalModalView extends React.Component {
93
94 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020095 show: PropTypes.bool,
96 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
97 title: PropTypes.string,
98 modalComponentProps: PropTypes.object,
99 modalComponentName: PropTypes.string,
100 onConfirmed: PropTypes.func,
101 onDeclined: PropTypes.func,
102 confirmationButtonText: PropTypes.string,
103 cancelButtonText: PropTypes.string
AviZi280f8012017-06-09 02:39:56 +0300104 };
105
106 static defaultProps = {
107 show: false,
108 type: 'default',
109 title: ''
110 };
111
112 render() {
Avi Zivb8e2faf2017-07-18 19:45:38 +0300113 let {title, type, show, modalComponentName, modalComponentProps,
AviZi280f8012017-06-09 02:39:56 +0300114 modalClassName, msg, onConfirmed, onDeclined, confirmationButtonText, cancelButtonText, onClose} = this.props;
115 const ComponentToRender = modalContentComponents[modalComponentName];
116 return (
az2497644017c2017-08-10 17:49:40 +0300117 <Modal show={show} bsSize={modalComponentProps && modalComponentProps.size} className={`onborading-modal ${modalClassName || ''} ${type2HeaderColor[type]}`}>
AviZi280f8012017-06-09 02:39:56 +0300118 <Modal.Header>
119 <Modal.Title>{title}</Modal.Title>
120 </Modal.Header>
121 <Modal.Body>
miriame6511b4b2017-10-22 15:14:44 +0300122 {ComponentToRender ?
123 <ComponentToRender {...modalComponentProps}/> :
miriamedee95852017-10-26 14:31:47 +0300124 msg && typeof msg === 'string' ?
talig8e9c0652017-12-20 14:30:43 +0200125 <div> {msg.split('\n').map((txt, i) => <span key={i}> {txt} <br/> </span>)} </div> :
miriamedee95852017-10-26 14:31:47 +0300126 msg
miriame6511b4b2017-10-22 15:14:44 +0300127 }
AviZi280f8012017-06-09 02:39:56 +0300128 </Modal.Body>
129 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) &&
130 <ModalFooter
131 type={type}
132 onConfirmed={onConfirmed}
133 onDeclined={onDeclined}
134 onClose={onClose}
135 confirmationButtonText={confirmationButtonText}
136 cancelButtonText={cancelButtonText}/>}
137 </Modal>
138 );
139 }
140
141 componentDidUpdate() {
142 if (this.props.timeout) {
143 setTimeout(this.props.onClose, this.props.timeout);
144 }
Avi Zivb8e2faf2017-07-18 19:45:38 +0300145 }
AviZi280f8012017-06-09 02:39:56 +0300146};
147
svishnev1eb66b72018-01-11 14:39:45 +0200148GlobalModalView.propTypes = {
149 show: PropTypes.bool,
150 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
151 title: PropTypes.string,
152 modalComponentProps: PropTypes.object,
153 modalComponentName: PropTypes.string,
154 onConfirmed: PropTypes.func,
155 onDeclined: PropTypes.func,
156 confirmationButtonText: PropTypes.string,
157 cancelButtonText: PropTypes.string
158};
159
AviZi280f8012017-06-09 02:39:56 +0300160export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(GlobalModalView);