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]} |
| 60 | onClick={() => { |
| 61 | onConfirmed(); |
| 62 | onClose(); |
| 63 | }}> |
| 64 | {confirmationButtonText} |
| 65 | </Button> |
| 66 | )} |
| 67 | <Button |
| 68 | {...myPropsForNoConfirmed} |
| 69 | data-test-id="sdc-modal-cancel-button" |
| 70 | btnType="outline" |
| 71 | color={typeClass[type]} |
| 72 | onClick={ |
| 73 | onDeclined |
| 74 | ? () => { |
| 75 | onDeclined(); |
| 76 | onClose(); |
| 77 | } |
| 78 | : () => onClose() |
| 79 | }> |
| 80 | {cancelButtonText} |
| 81 | </Button> |
| 82 | </div> |
| 83 | </Modal.Footer> |
| 84 | ); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 85 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 86 | |
| 87 | ModalFooter.defaultProps = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 88 | type: 'default', |
| 89 | confirmationButtonText: i18n('OK'), |
| 90 | cancelButtonText: i18n('Cancel') |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 91 | }; |
| 92 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 93 | ModalFooter.PropTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 94 | type: PropTypes.string, |
| 95 | confirmationButtonText: PropTypes.string, |
| 96 | cancelButtonText: PropTypes.string |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 99 | export const mapStateToProps = ({ modal }) => { |
| 100 | const show = !!modal; |
| 101 | return { |
| 102 | show, |
| 103 | ...modal |
| 104 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 105 | }; |
| 106 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 107 | export const mapActionToProps = dispatch => { |
| 108 | return { |
| 109 | onClose: () => dispatch({ type: actionTypes.GLOBAL_MODAL_CLOSE }) |
| 110 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 111 | }; |
| 112 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 113 | export class GlobalModalView extends React.Component { |
| 114 | static propTypes = { |
| 115 | show: PropTypes.bool, |
| 116 | type: PropTypes.oneOf(['default', 'error', 'warning', 'success']), |
| 117 | title: PropTypes.string, |
| 118 | modalComponentProps: PropTypes.object, |
| 119 | modalComponentName: PropTypes.string, |
| 120 | onConfirmed: PropTypes.func, |
| 121 | onDeclined: PropTypes.func, |
| 122 | confirmationButtonText: PropTypes.string, |
| 123 | cancelButtonText: PropTypes.string |
| 124 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 125 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 126 | static defaultProps = { |
| 127 | show: false, |
| 128 | type: 'default', |
| 129 | title: '' |
| 130 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 131 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 132 | render() { |
| 133 | let { |
| 134 | title, |
| 135 | type, |
| 136 | show, |
| 137 | modalComponentName, |
| 138 | modalComponentProps, |
| 139 | modalClassName, |
| 140 | msg, |
| 141 | onConfirmed, |
| 142 | onDeclined, |
| 143 | confirmationButtonText, |
| 144 | cancelButtonText, |
| 145 | onClose |
| 146 | } = this.props; |
| 147 | const ComponentToRender = modalContentComponents[modalComponentName]; |
| 148 | return ( |
| 149 | <Modal |
| 150 | show={show} |
| 151 | bsSize={modalComponentProps && modalComponentProps.size} |
| 152 | className={`onborading-modal ${modalClassName || ''} ${ |
| 153 | type2HeaderColor[type] |
| 154 | }`}> |
| 155 | <Modal.Header> |
| 156 | <Modal.Title>{title}</Modal.Title> |
| 157 | </Modal.Header> |
| 158 | <Modal.Body> |
| 159 | {ComponentToRender ? ( |
| 160 | <ComponentToRender {...modalComponentProps} /> |
| 161 | ) : msg && typeof msg === 'string' ? ( |
| 162 | <div> |
| 163 | {' '} |
| 164 | {msg.split('\n').map((txt, i) => ( |
| 165 | <span key={i}> |
| 166 | {' '} |
| 167 | {txt} <br />{' '} |
| 168 | </span> |
| 169 | ))}{' '} |
| 170 | </div> |
| 171 | ) : ( |
| 172 | msg |
| 173 | )} |
| 174 | </Modal.Body> |
| 175 | {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && ( |
| 176 | <ModalFooter |
| 177 | type={type} |
| 178 | onConfirmed={onConfirmed} |
| 179 | onDeclined={onDeclined} |
| 180 | onClose={onClose} |
| 181 | confirmationButtonText={confirmationButtonText} |
| 182 | cancelButtonText={cancelButtonText} |
| 183 | /> |
| 184 | )} |
| 185 | </Modal> |
| 186 | ); |
| 187 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 188 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 189 | componentDidUpdate() { |
| 190 | if (this.props.timeout) { |
| 191 | setTimeout(this.props.onClose, this.props.timeout); |
| 192 | } |
| 193 | } |
| 194 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 195 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 196 | GlobalModalView.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 197 | show: PropTypes.bool, |
| 198 | type: PropTypes.oneOf(['default', 'error', 'warning', 'success']), |
| 199 | title: PropTypes.string, |
| 200 | modalComponentProps: PropTypes.object, |
| 201 | modalComponentName: PropTypes.string, |
| 202 | onConfirmed: PropTypes.func, |
| 203 | onDeclined: PropTypes.func, |
| 204 | confirmationButtonText: PropTypes.string, |
| 205 | cancelButtonText: PropTypes.string |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 208 | export default connect(mapStateToProps, mapActionToProps, null, { |
| 209 | withRef: true |
| 210 | })(GlobalModalView); |