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 |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
| 21 | import Modal from 'nfvo-components/modal/Modal.jsx'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 22 | import Button from 'sdc-ui/lib/react/Button.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 24 | import { modalContentComponents } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 25 | import { actionTypes, typeEnum } from './GlobalModalConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 26 | |
| 27 | const typeClass = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 28 | default: 'primary', |
| 29 | error: 'negative', |
| 30 | warning: 'warning', |
| 31 | success: 'positive' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 32 | }; |
| 33 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 34 | const type2HeaderColor = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | default: 'primary', |
| 36 | error: 'danger', |
| 37 | warning: 'warning', |
| 38 | success: 'success' |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 39 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 40 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 41 | const 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]} |
svishnev | 04783c4 | 2018-04-17 11:32:22 +0300 | [diff] [blame] | 60 | btnType="primary" |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 61 | onClick={() => { |
| 62 | onConfirmed(); |
| 63 | onClose(); |
| 64 | }}> |
| 65 | {confirmationButtonText} |
| 66 | </Button> |
| 67 | )} |
| 68 | <Button |
| 69 | {...myPropsForNoConfirmed} |
| 70 | data-test-id="sdc-modal-cancel-button" |
svishnev | 04783c4 | 2018-04-17 11:32:22 +0300 | [diff] [blame] | 71 | btnType="secondary" |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 72 | 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 | ); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 86 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 87 | |
| 88 | ModalFooter.defaultProps = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 89 | type: 'default', |
| 90 | confirmationButtonText: i18n('OK'), |
| 91 | cancelButtonText: i18n('Cancel') |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 92 | }; |
| 93 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 94 | ModalFooter.PropTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 95 | type: PropTypes.string, |
| 96 | confirmationButtonText: PropTypes.string, |
| 97 | cancelButtonText: PropTypes.string |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 100 | export const mapStateToProps = ({ modal }) => { |
| 101 | const show = !!modal; |
| 102 | return { |
| 103 | show, |
| 104 | ...modal |
| 105 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 106 | }; |
| 107 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 108 | export const mapActionToProps = dispatch => { |
| 109 | return { |
| 110 | onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE }) |
| 111 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 112 | }; |
| 113 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 114 | export 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 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 126 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 127 | static defaultProps = { |
| 128 | show: false, |
| 129 | type: 'default', |
| 130 | title: '' |
| 131 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 133 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 189 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 190 | componentDidUpdate() { |
| 191 | if (this.props.timeout) { |
| 192 | setTimeout(this.props.onClose, this.props.timeout); |
| 193 | } |
| 194 | } |
| 195 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 196 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 197 | GlobalModalView.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 198 | 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 |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 209 | export default connect(mapStateToProps, mapActionToProps, null, { |
| 210 | withRef: true |
| 211 | })(GlobalModalView); |