blob: 118740498d6dd5968cb8c929e1294913547ebfed [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
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03007 *
svishnev1eb66b72018-01-11 14:39:45 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03009 *
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
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030021import {
22 Modal,
23 ModalHeader,
24 ModalTitle,
25 ModalBody,
26 ModalFooter
27} from 'sdc-ui/lib/react';
AviZi280f8012017-06-09 02:39:56 +030028import i18n from 'nfvo-utils/i18n/i18n.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029import { modalContentComponents } from 'sdc-app/common/modal/ModalContentMapper.js';
30import { actionTypes, typeEnum } from './GlobalModalConstants.js';
AviZi280f8012017-06-09 02:39:56 +030031
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030032const GlobalModalFooter = ({
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033 onConfirmed,
34 onDeclined,
35 onClose,
36 confirmationButtonText,
Yarin Dekel042f4cd2019-02-11 16:36:19 +020037 cancelButtonText,
38 confirmDataTestId,
39 cancelDataTestId
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040}) => {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030041 let actionButtonClick;
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020042 if (onConfirmed) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030043 actionButtonClick = () => {
44 onConfirmed();
45 onClose();
46 };
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047 }
48 return (
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030049 <ModalFooter
50 actionButtonText={onConfirmed ? confirmationButtonText : undefined}
51 actionButtonClick={actionButtonClick}
52 closeButtonText={cancelButtonText}
53 onClose={
54 onDeclined
55 ? () => {
56 onDeclined();
57 onClose();
58 }
59 : () => onClose()
60 }
61 withButtons
Yarin Dekel042f4cd2019-02-11 16:36:19 +020062 confirmDataTestId={confirmDataTestId}
63 cancelDataTestId={cancelDataTestId}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030064 />
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 );
az2497644017c2017-08-10 17:49:40 +030066};
AviZi280f8012017-06-09 02:39:56 +030067
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030068GlobalModalFooter.defaultProps = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 confirmationButtonText: i18n('OK'),
Yarin Dekel042f4cd2019-02-11 16:36:19 +020070 cancelButtonText: i18n('Cancel'),
71 cancelDataTestId: 'sdc-modal-cancel-button',
72 confirmDataTestId: 'sdc-modal-confirm-button'
AviZi280f8012017-06-09 02:39:56 +030073};
74
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030075GlobalModalFooter.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020076 confirmationButtonText: PropTypes.string,
Yarin Dekel042f4cd2019-02-11 16:36:19 +020077 cancelButtonText: PropTypes.string,
78 confirmDataTestId: PropTypes.string,
79 cancelDataTestId: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +020080};
81
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082export const mapStateToProps = ({ modal }) => {
83 const show = !!modal;
84 return {
85 show,
86 ...modal
87 };
AviZi280f8012017-06-09 02:39:56 +030088};
89
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020090export const mapActionToProps = dispatch => {
91 return {
92 onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE })
93 };
AviZi280f8012017-06-09 02:39:56 +030094};
95
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020096export class GlobalModalView extends React.Component {
97 static propTypes = {
98 show: PropTypes.bool,
99 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
100 title: PropTypes.string,
101 modalComponentProps: PropTypes.object,
102 modalComponentName: PropTypes.string,
103 onConfirmed: PropTypes.func,
104 onDeclined: PropTypes.func,
105 confirmationButtonText: PropTypes.string,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300106 cancelButtonText: PropTypes.string,
Yarin Dekel042f4cd2019-02-11 16:36:19 +0200107 bodyClassName: PropTypes.string,
108 cancelDataTestId: PropTypes.string,
109 confirmDataTestId: PropTypes.string
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200110 };
AviZi280f8012017-06-09 02:39:56 +0300111
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200112 static defaultProps = {
113 show: false,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300114 type: 'custom',
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200115 title: ''
116 };
AviZi280f8012017-06-09 02:39:56 +0300117
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200118 render() {
119 let {
120 title,
121 type,
122 show,
123 modalComponentName,
124 modalComponentProps,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 msg,
126 onConfirmed,
127 onDeclined,
128 confirmationButtonText,
129 cancelButtonText,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300130 onClose,
Yarin Dekel042f4cd2019-02-11 16:36:19 +0200131 bodyClassName,
132 confirmDataTestId,
133 cancelDataTestId
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200134 } = this.props;
135 const ComponentToRender = modalContentComponents[modalComponentName];
136 return (
137 <Modal
138 show={show}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300139 type={type}
140 size={modalComponentProps && modalComponentProps.size}>
141 <ModalHeader type={type} onClose={onClose}>
142 <ModalTitle>{title}</ModalTitle>
143 </ModalHeader>
144 <ModalBody className={bodyClassName}>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200145 {ComponentToRender ? (
146 <ComponentToRender {...modalComponentProps} />
147 ) : msg && typeof msg === 'string' ? (
148 <div>
149 {' '}
150 {msg.split('\n').map((txt, i) => (
151 <span key={i}>
152 {' '}
153 {txt} <br />{' '}
154 </span>
155 ))}{' '}
156 </div>
157 ) : (
158 msg
159 )}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300160 </ModalBody>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200161 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && (
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300162 <GlobalModalFooter
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 onConfirmed={onConfirmed}
164 onDeclined={onDeclined}
165 onClose={onClose}
166 confirmationButtonText={confirmationButtonText}
167 cancelButtonText={cancelButtonText}
Yarin Dekel042f4cd2019-02-11 16:36:19 +0200168 confirmDataTestId={confirmDataTestId}
169 cancelDataTestId={cancelDataTestId}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200170 />
171 )}
172 </Modal>
173 );
174 }
AviZi280f8012017-06-09 02:39:56 +0300175
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200176 componentDidUpdate() {
177 if (this.props.timeout) {
178 setTimeout(this.props.onClose, this.props.timeout);
179 }
180 }
181}
AviZi280f8012017-06-09 02:39:56 +0300182
svishnev1eb66b72018-01-11 14:39:45 +0200183GlobalModalView.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200184 show: PropTypes.bool,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300185 type: PropTypes.oneOf(['custom', 'error', 'alert', 'info']),
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200186 title: PropTypes.string,
187 modalComponentProps: PropTypes.object,
188 modalComponentName: PropTypes.string,
189 onConfirmed: PropTypes.func,
190 onDeclined: PropTypes.func,
191 confirmationButtonText: PropTypes.string,
192 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +0200193};
194
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200195export default connect(mapStateToProps, mapActionToProps, null, {
196 withRef: true
197})(GlobalModalView);