blob: 3f19bd76a3ac748652a6e5acecdccc3384926d6b [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020019import { connect } from 'react-redux';
AviZi280f8012017-06-09 02:39:56 +030020
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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024import { modalContentComponents } from 'sdc-app/common/modal/ModalContentMapper.js';
25import { actionTypes, typeEnum } from './GlobalModalConstants.js';
AviZi280f8012017-06-09 02:39:56 +030026
27const typeClass = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020028 default: 'primary',
29 error: 'negative',
30 warning: 'warning',
31 success: 'positive'
AviZi280f8012017-06-09 02:39:56 +030032};
33
az2497644017c2017-08-10 17:49:40 +030034const type2HeaderColor = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 default: 'primary',
36 error: 'danger',
37 warning: 'warning',
38 success: 'success'
az2497644017c2017-08-10 17:49:40 +030039};
AviZi280f8012017-06-09 02:39:56 +030040
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041const ModalFooter = ({
42 type,
43 onConfirmed,
44 onDeclined,
45 onClose,
46 confirmationButtonText,
47 cancelButtonText
48}) => {
49 let myPropsForNoConfirmed = {};
50 if (onConfirmed) {
51 myPropsForNoConfirmed.btnType = 'outline';
52 }
53 return (
54 <Modal.Footer>
55 <div className="sdc-modal-footer">
56 {onConfirmed && (
57 <Button
58 data-test-id="sdc-modal-confirm-button"
59 color={typeClass[type]}
60 onClick={() => {
61 onConfirmed();
62 onClose();
63 }}>
64 {confirmationButtonText}
65 </Button>
66 )}
67 <Button
68 {...myPropsForNoConfirmed}
69 data-test-id="sdc-modal-cancel-button"
70 btnType="outline"
71 color={typeClass[type]}
72 onClick={
73 onDeclined
74 ? () => {
75 onDeclined();
76 onClose();
77 }
78 : () => onClose()
79 }>
80 {cancelButtonText}
81 </Button>
82 </div>
83 </Modal.Footer>
84 );
az2497644017c2017-08-10 17:49:40 +030085};
AviZi280f8012017-06-09 02:39:56 +030086
87ModalFooter.defaultProps = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 type: 'default',
89 confirmationButtonText: i18n('OK'),
90 cancelButtonText: i18n('Cancel')
AviZi280f8012017-06-09 02:39:56 +030091};
92
svishnev1eb66b72018-01-11 14:39:45 +020093ModalFooter.PropTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020094 type: PropTypes.string,
95 confirmationButtonText: PropTypes.string,
96 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +020097};
98
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020099export const mapStateToProps = ({ modal }) => {
100 const show = !!modal;
101 return {
102 show,
103 ...modal
104 };
AviZi280f8012017-06-09 02:39:56 +0300105};
106
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107export const mapActionToProps = dispatch => {
108 return {
109 onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE })
110 };
AviZi280f8012017-06-09 02:39:56 +0300111};
112
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200113export class GlobalModalView extends React.Component {
114 static propTypes = {
115 show: PropTypes.bool,
116 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
117 title: PropTypes.string,
118 modalComponentProps: PropTypes.object,
119 modalComponentName: PropTypes.string,
120 onConfirmed: PropTypes.func,
121 onDeclined: PropTypes.func,
122 confirmationButtonText: PropTypes.string,
123 cancelButtonText: PropTypes.string
124 };
AviZi280f8012017-06-09 02:39:56 +0300125
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200126 static defaultProps = {
127 show: false,
128 type: 'default',
129 title: ''
130 };
AviZi280f8012017-06-09 02:39:56 +0300131
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200132 render() {
133 let {
134 title,
135 type,
136 show,
137 modalComponentName,
138 modalComponentProps,
139 modalClassName,
140 msg,
141 onConfirmed,
142 onDeclined,
143 confirmationButtonText,
144 cancelButtonText,
145 onClose
146 } = this.props;
147 const ComponentToRender = modalContentComponents[modalComponentName];
148 return (
149 <Modal
150 show={show}
151 bsSize={modalComponentProps && modalComponentProps.size}
152 className={`onborading-modal ${modalClassName || ''} ${
153 type2HeaderColor[type]
154 }`}>
155 <Modal.Header>
156 <Modal.Title>{title}</Modal.Title>
157 </Modal.Header>
158 <Modal.Body>
159 {ComponentToRender ? (
160 <ComponentToRender {...modalComponentProps} />
161 ) : msg && typeof msg === 'string' ? (
162 <div>
163 {' '}
164 {msg.split('\n').map((txt, i) => (
165 <span key={i}>
166 {' '}
167 {txt} <br />{' '}
168 </span>
169 ))}{' '}
170 </div>
171 ) : (
172 msg
173 )}
174 </Modal.Body>
175 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && (
176 <ModalFooter
177 type={type}
178 onConfirmed={onConfirmed}
179 onDeclined={onDeclined}
180 onClose={onClose}
181 confirmationButtonText={confirmationButtonText}
182 cancelButtonText={cancelButtonText}
183 />
184 )}
185 </Modal>
186 );
187 }
AviZi280f8012017-06-09 02:39:56 +0300188
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200189 componentDidUpdate() {
190 if (this.props.timeout) {
191 setTimeout(this.props.onClose, this.props.timeout);
192 }
193 }
194}
AviZi280f8012017-06-09 02:39:56 +0300195
svishnev1eb66b72018-01-11 14:39:45 +0200196GlobalModalView.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200197 show: PropTypes.bool,
198 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
199 title: PropTypes.string,
200 modalComponentProps: PropTypes.object,
201 modalComponentName: PropTypes.string,
202 onConfirmed: PropTypes.func,
203 onDeclined: PropTypes.func,
204 confirmationButtonText: PropTypes.string,
205 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +0200206};
207
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200208export default connect(mapStateToProps, mapActionToProps, null, {
209 withRef: true
210})(GlobalModalView);