svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
| 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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 7 | * |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 9 | * |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 12 | * 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. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 18 | import PropTypes from 'prop-types'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 19 | import { connect } from 'react-redux'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 20 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 21 | import { |
| 22 | Modal, |
| 23 | ModalHeader, |
| 24 | ModalTitle, |
| 25 | ModalBody, |
| 26 | ModalFooter |
| 27 | } from 'sdc-ui/lib/react'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 28 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | import { modalContentComponents } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 30 | import { actionTypes, typeEnum } from './GlobalModalConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 31 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 32 | const GlobalModalFooter = ({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | onConfirmed, |
| 34 | onDeclined, |
| 35 | onClose, |
| 36 | confirmationButtonText, |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 37 | cancelButtonText, |
| 38 | confirmDataTestId, |
| 39 | cancelDataTestId |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | }) => { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 41 | let actionButtonClick; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 42 | if (onConfirmed) { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 43 | actionButtonClick = () => { |
| 44 | onConfirmed(); |
| 45 | onClose(); |
| 46 | }; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 47 | } |
| 48 | return ( |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 49 | <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 Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 62 | confirmDataTestId={confirmDataTestId} |
| 63 | cancelDataTestId={cancelDataTestId} |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 64 | /> |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | ); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 66 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 67 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 68 | GlobalModalFooter.defaultProps = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 69 | confirmationButtonText: i18n('OK'), |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 70 | cancelButtonText: i18n('Cancel'), |
| 71 | cancelDataTestId: 'sdc-modal-cancel-button', |
| 72 | confirmDataTestId: 'sdc-modal-confirm-button' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 73 | }; |
| 74 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 75 | GlobalModalFooter.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 76 | confirmationButtonText: PropTypes.string, |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 77 | cancelButtonText: PropTypes.string, |
| 78 | confirmDataTestId: PropTypes.string, |
| 79 | cancelDataTestId: PropTypes.string |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 82 | export const mapStateToProps = ({ modal }) => { |
| 83 | const show = !!modal; |
| 84 | return { |
| 85 | show, |
| 86 | ...modal |
| 87 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 88 | }; |
| 89 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 90 | export const mapActionToProps = dispatch => { |
| 91 | return { |
| 92 | onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE }) |
| 93 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 94 | }; |
| 95 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 96 | export 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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 106 | cancelButtonText: PropTypes.string, |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 107 | bodyClassName: PropTypes.string, |
| 108 | cancelDataTestId: PropTypes.string, |
| 109 | confirmDataTestId: PropTypes.string |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 110 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 111 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 112 | static defaultProps = { |
| 113 | show: false, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 114 | type: 'custom', |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 115 | title: '' |
| 116 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 117 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 118 | render() { |
| 119 | let { |
| 120 | title, |
| 121 | type, |
| 122 | show, |
| 123 | modalComponentName, |
| 124 | modalComponentProps, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 125 | msg, |
| 126 | onConfirmed, |
| 127 | onDeclined, |
| 128 | confirmationButtonText, |
| 129 | cancelButtonText, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 130 | onClose, |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 131 | bodyClassName, |
| 132 | confirmDataTestId, |
| 133 | cancelDataTestId |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 134 | } = this.props; |
| 135 | const ComponentToRender = modalContentComponents[modalComponentName]; |
| 136 | return ( |
| 137 | <Modal |
| 138 | show={show} |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 139 | 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 Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 145 | {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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 160 | </ModalBody> |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 161 | {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && ( |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 162 | <GlobalModalFooter |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 163 | onConfirmed={onConfirmed} |
| 164 | onDeclined={onDeclined} |
| 165 | onClose={onClose} |
| 166 | confirmationButtonText={confirmationButtonText} |
| 167 | cancelButtonText={cancelButtonText} |
Yarin Dekel | 042f4cd | 2019-02-11 16:36:19 +0200 | [diff] [blame] | 168 | confirmDataTestId={confirmDataTestId} |
| 169 | cancelDataTestId={cancelDataTestId} |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | /> |
| 171 | )} |
| 172 | </Modal> |
| 173 | ); |
| 174 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 175 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 176 | componentDidUpdate() { |
| 177 | if (this.props.timeout) { |
| 178 | setTimeout(this.props.onClose, this.props.timeout); |
| 179 | } |
| 180 | } |
| 181 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 182 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 183 | GlobalModalView.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 184 | show: PropTypes.bool, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 185 | type: PropTypes.oneOf(['custom', 'error', 'alert', 'info']), |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 186 | 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 |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 195 | export default connect(mapStateToProps, mapActionToProps, null, { |
| 196 | withRef: true |
| 197 | })(GlobalModalView); |