blob: 0c924304ccba44718944055f5edc38ddb1cddfbc [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,
37 cancelButtonText
38}) => {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030039 let actionButtonClick;
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 if (onConfirmed) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030041 actionButtonClick = () => {
42 onConfirmed();
43 onClose();
44 };
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 }
46 return (
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030047 <ModalFooter
48 actionButtonText={onConfirmed ? confirmationButtonText : undefined}
49 actionButtonClick={actionButtonClick}
50 closeButtonText={cancelButtonText}
51 onClose={
52 onDeclined
53 ? () => {
54 onDeclined();
55 onClose();
56 }
57 : () => onClose()
58 }
59 withButtons
60 />
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020061 );
az2497644017c2017-08-10 17:49:40 +030062};
AviZi280f8012017-06-09 02:39:56 +030063
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030064GlobalModalFooter.defaultProps = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 confirmationButtonText: i18n('OK'),
66 cancelButtonText: i18n('Cancel')
AviZi280f8012017-06-09 02:39:56 +030067};
68
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030069GlobalModalFooter.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 confirmationButtonText: PropTypes.string,
71 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +020072};
73
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020074export const mapStateToProps = ({ modal }) => {
75 const show = !!modal;
76 return {
77 show,
78 ...modal
79 };
AviZi280f8012017-06-09 02:39:56 +030080};
81
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082export const mapActionToProps = dispatch => {
83 return {
84 onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE })
85 };
AviZi280f8012017-06-09 02:39:56 +030086};
87
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088export class GlobalModalView extends React.Component {
89 static propTypes = {
90 show: PropTypes.bool,
91 type: PropTypes.oneOf(['default', 'error', 'warning', 'success']),
92 title: PropTypes.string,
93 modalComponentProps: PropTypes.object,
94 modalComponentName: PropTypes.string,
95 onConfirmed: PropTypes.func,
96 onDeclined: PropTypes.func,
97 confirmationButtonText: PropTypes.string,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030098 cancelButtonText: PropTypes.string,
99 bodyClassName: PropTypes.string
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 };
AviZi280f8012017-06-09 02:39:56 +0300101
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200102 static defaultProps = {
103 show: false,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300104 type: 'custom',
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200105 title: ''
106 };
AviZi280f8012017-06-09 02:39:56 +0300107
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200108 render() {
109 let {
110 title,
111 type,
112 show,
113 modalComponentName,
114 modalComponentProps,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200115 msg,
116 onConfirmed,
117 onDeclined,
118 confirmationButtonText,
119 cancelButtonText,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300120 onClose,
121 bodyClassName
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200122 } = this.props;
123 const ComponentToRender = modalContentComponents[modalComponentName];
124 return (
125 <Modal
126 show={show}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300127 type={type}
128 size={modalComponentProps && modalComponentProps.size}>
129 <ModalHeader type={type} onClose={onClose}>
130 <ModalTitle>{title}</ModalTitle>
131 </ModalHeader>
132 <ModalBody className={bodyClassName}>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200133 {ComponentToRender ? (
134 <ComponentToRender {...modalComponentProps} />
135 ) : msg && typeof msg === 'string' ? (
136 <div>
137 {' '}
138 {msg.split('\n').map((txt, i) => (
139 <span key={i}>
140 {' '}
141 {txt} <br />{' '}
142 </span>
143 ))}{' '}
144 </div>
145 ) : (
146 msg
147 )}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300148 </ModalBody>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && (
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300150 <GlobalModalFooter
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200151 onConfirmed={onConfirmed}
152 onDeclined={onDeclined}
153 onClose={onClose}
154 confirmationButtonText={confirmationButtonText}
155 cancelButtonText={cancelButtonText}
156 />
157 )}
158 </Modal>
159 );
160 }
AviZi280f8012017-06-09 02:39:56 +0300161
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200162 componentDidUpdate() {
163 if (this.props.timeout) {
164 setTimeout(this.props.onClose, this.props.timeout);
165 }
166 }
167}
AviZi280f8012017-06-09 02:39:56 +0300168
svishnev1eb66b72018-01-11 14:39:45 +0200169GlobalModalView.propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200170 show: PropTypes.bool,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300171 type: PropTypes.oneOf(['custom', 'error', 'alert', 'info']),
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200172 title: PropTypes.string,
173 modalComponentProps: PropTypes.object,
174 modalComponentName: PropTypes.string,
175 onConfirmed: PropTypes.func,
176 onDeclined: PropTypes.func,
177 confirmationButtonText: PropTypes.string,
178 cancelButtonText: PropTypes.string
svishnev1eb66b72018-01-11 14:39:45 +0200179};
180
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200181export default connect(mapStateToProps, mapActionToProps, null, {
182 withRef: true
183})(GlobalModalView);