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, |
| 37 | cancelButtonText |
| 38 | }) => { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 39 | let actionButtonClick; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | if (onConfirmed) { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 41 | actionButtonClick = () => { |
| 42 | onConfirmed(); |
| 43 | onClose(); |
| 44 | }; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 45 | } |
| 46 | return ( |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 47 | <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 Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 61 | ); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 62 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 64 | GlobalModalFooter.defaultProps = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | confirmationButtonText: i18n('OK'), |
| 66 | cancelButtonText: i18n('Cancel') |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 67 | }; |
| 68 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 69 | GlobalModalFooter.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | confirmationButtonText: PropTypes.string, |
| 71 | cancelButtonText: PropTypes.string |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 74 | export const mapStateToProps = ({ modal }) => { |
| 75 | const show = !!modal; |
| 76 | return { |
| 77 | show, |
| 78 | ...modal |
| 79 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 80 | }; |
| 81 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 82 | export const mapActionToProps = dispatch => { |
| 83 | return { |
| 84 | onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE }) |
| 85 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 86 | }; |
| 87 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 88 | export 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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 98 | cancelButtonText: PropTypes.string, |
| 99 | bodyClassName: PropTypes.string |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 100 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 101 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 102 | static defaultProps = { |
| 103 | show: false, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 104 | type: 'custom', |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 105 | title: '' |
| 106 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 107 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 108 | render() { |
| 109 | let { |
| 110 | title, |
| 111 | type, |
| 112 | show, |
| 113 | modalComponentName, |
| 114 | modalComponentProps, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 115 | msg, |
| 116 | onConfirmed, |
| 117 | onDeclined, |
| 118 | confirmationButtonText, |
| 119 | cancelButtonText, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 120 | onClose, |
| 121 | bodyClassName |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 122 | } = this.props; |
| 123 | const ComponentToRender = modalContentComponents[modalComponentName]; |
| 124 | return ( |
| 125 | <Modal |
| 126 | show={show} |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 127 | 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 Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 133 | {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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 148 | </ModalBody> |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 149 | {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && ( |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 150 | <GlobalModalFooter |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 151 | onConfirmed={onConfirmed} |
| 152 | onDeclined={onDeclined} |
| 153 | onClose={onClose} |
| 154 | confirmationButtonText={confirmationButtonText} |
| 155 | cancelButtonText={cancelButtonText} |
| 156 | /> |
| 157 | )} |
| 158 | </Modal> |
| 159 | ); |
| 160 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 161 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 162 | componentDidUpdate() { |
| 163 | if (this.props.timeout) { |
| 164 | setTimeout(this.props.onClose, this.props.timeout); |
| 165 | } |
| 166 | } |
| 167 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 168 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 169 | GlobalModalView.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | show: PropTypes.bool, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 171 | type: PropTypes.oneOf(['custom', 'error', 'alert', 'info']), |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 172 | 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 |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 179 | }; |
| 180 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 181 | export default connect(mapStateToProps, mapActionToProps, null, { |
| 182 | withRef: true |
| 183 | })(GlobalModalView); |