blob: 3a80e734ea3967860f37770e4f28b29be366fdf9 [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]}
svishnev04783c42018-04-17 11:32:22 +030060 btnType="primary"
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020061 onClick={() => {
62 onConfirmed();
63 onClose();
64 }}>
65 {confirmationButtonText}
66 </Button>
67 )}
68 <Button
69 {...myPropsForNoConfirmed}
70 data-test-id="sdc-modal-cancel-button"
svishnev04783c42018-04-17 11:32:22 +030071 btnType="secondary"
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 color={typeClass[type]}
73 onClick={
74 onDeclined
75 ? () => {
76 onDeclined();
77 onClose();
78 }
79 : () => onClose()
80 }>
81 {cancelButtonText}
82 </Button>
83 </div>
84 </Modal.Footer>
85 );
az2497644017c2017-08-10 17:49:40 +030086};
AviZi280f8012017-06-09 02:39:56 +030087
88ModalFooter.defaultProps = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020089 type: 'default',
90 confirmationButtonText: i18n('OK'),
91 cancelButtonText: i18n('Cancel')
AviZi280f8012017-06-09 02:39:56 +030092};
93
svishnev1eb66b72018-01-11 14:39:45 +020094ModalFooter.PropTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020095 type: PropTypes.string,
96 confirmationButtonText: PropTypes.string,
97 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +020098};
99
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100export const mapStateToProps = ({ modal }) => {
101 const show = !!modal;
102 return {
103 show,
104 ...modal
105 };
AviZi280f8012017-06-09 02:39:56 +0300106};
107
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200108export const mapActionToProps = dispatch => {
109 return {
110 onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE })
111 };
AviZi280f8012017-06-09 02:39:56 +0300112};
113
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200114export class GlobalModalView extends React.Component {
115 static propTypes = {
116 show: PropTypes.bool,
117 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
118 title: PropTypes.string,
119 modalComponentProps: PropTypes.object,
120 modalComponentName: PropTypes.string,
121 onConfirmed: PropTypes.func,
122 onDeclined: PropTypes.func,
123 confirmationButtonText: PropTypes.string,
124 cancelButtonText: PropTypes.string
125 };
AviZi280f8012017-06-09 02:39:56 +0300126
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200127 static defaultProps = {
128 show: false,
129 type: 'default',
130 title: ''
131 };
AviZi280f8012017-06-09 02:39:56 +0300132
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200133 render() {
134 let {
135 title,
136 type,
137 show,
138 modalComponentName,
139 modalComponentProps,
140 modalClassName,
141 msg,
142 onConfirmed,
143 onDeclined,
144 confirmationButtonText,
145 cancelButtonText,
146 onClose
147 } = this.props;
148 const ComponentToRender = modalContentComponents[modalComponentName];
149 return (
150 <Modal
151 show={show}
152 bsSize={modalComponentProps && modalComponentProps.size}
153 className={`onborading-modal ${modalClassName || ''} ${
154 type2HeaderColor[type]
155 }`}>
156 <Modal.Header>
157 <Modal.Title>{title}</Modal.Title>
158 </Modal.Header>
159 <Modal.Body>
160 {ComponentToRender ? (
161 <ComponentToRender {...modalComponentProps} />
162 ) : msg && typeof msg === 'string' ? (
163 <div>
164 {' '}
165 {msg.split('\n').map((txt, i) => (
166 <span key={i}>
167 {' '}
168 {txt} <br />{' '}
169 </span>
170 ))}{' '}
171 </div>
172 ) : (
173 msg
174 )}
175 </Modal.Body>
176 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && (
177 <ModalFooter
178 type={type}
179 onConfirmed={onConfirmed}
180 onDeclined={onDeclined}
181 onClose={onClose}
182 confirmationButtonText={confirmationButtonText}
183 cancelButtonText={cancelButtonText}
184 />
185 )}
186 </Modal>
187 );
188 }
AviZi280f8012017-06-09 02:39:56 +0300189
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200190 componentDidUpdate() {
191 if (this.props.timeout) {
192 setTimeout(this.props.onClose, this.props.timeout);
193 }
194 }
195}
AviZi280f8012017-06-09 02:39:56 +0300196
svishnev1eb66b72018-01-11 14:39:45 +0200197GlobalModalView.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200198 show: PropTypes.bool,
199 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
200 title: PropTypes.string,
201 modalComponentProps: PropTypes.object,
202 modalComponentName: PropTypes.string,
203 onConfirmed: PropTypes.func,
204 onDeclined: PropTypes.func,
205 confirmationButtonText: PropTypes.string,
206 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +0200207};
208
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200209export default connect(mapStateToProps, mapActionToProps, null, {
210 withRef: true
211})(GlobalModalView);