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'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import {connect} from 'react-redux'; |
| 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'; |
| 24 | import {modalContentComponents} from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 25 | import {actionTypes, typeEnum} from './GlobalModalConstants.js'; |
| 26 | |
| 27 | |
| 28 | const typeClass = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 29 | 'default': 'primary', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 30 | error: 'negative', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 31 | warning: 'warning', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 32 | success: 'positive' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 33 | }; |
| 34 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 35 | const type2HeaderColor = { |
| 36 | 'default': 'primary', |
| 37 | error: 'danger', |
| 38 | warning: 'warning', |
| 39 | success: 'success' |
| 40 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 41 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 42 | |
| 43 | const ModalFooter = ({type, onConfirmed, onDeclined, onClose, confirmationButtonText, cancelButtonText}) => { |
| 44 | let myPropsForNoConfirmed = {}; |
| 45 | if (onConfirmed) { |
| 46 | myPropsForNoConfirmed.btnType = 'outline'; |
| 47 | } |
| 48 | return ( |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | <Modal.Footer> |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 50 | <div className='sdc-modal-footer'> |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 51 | {onConfirmed && <Button data-test-id='sdc-modal-confirm-button' color={typeClass[type]} onClick={() => { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 52 | onConfirmed(); |
| 53 | onClose(); |
| 54 | }}>{confirmationButtonText}</Button>} |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 55 | <Button {...myPropsForNoConfirmed} data-test-id='sdc-modal-cancel-button' btnType='outline' color={typeClass[type]} onClick={onDeclined ? () => { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 56 | onDeclined(); |
| 57 | onClose();} : () => onClose()}> |
| 58 | {cancelButtonText} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 59 | </Button> |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 60 | </div> |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 61 | </Modal.Footer> |
| 62 | ); |
| 63 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 64 | |
| 65 | ModalFooter.defaultProps = { |
| 66 | type: 'default', |
| 67 | confirmationButtonText: i18n('OK'), |
| 68 | cancelButtonText: i18n('Cancel') |
| 69 | }; |
| 70 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame^] | 71 | ModalFooter.PropTypes = { |
| 72 | type: PropTypes.string, |
| 73 | confirmationButtonText: PropTypes.string, |
| 74 | cancelButtonText: PropTypes.string |
| 75 | }; |
| 76 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 77 | export const mapStateToProps = ({modal}) => { |
| 78 | const show = !!modal; |
| 79 | return { |
| 80 | show, |
| 81 | ...modal |
| 82 | }; |
| 83 | }; |
| 84 | |
| 85 | export const mapActionToProps = (dispatch) => { |
| 86 | return { |
| 87 | onClose: () => dispatch({type: actionTypes.GLOBAL_MODAL_CLOSE}) |
| 88 | }; |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | export class GlobalModalView extends React.Component { |
| 93 | |
| 94 | static propTypes = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 95 | show: PropTypes.bool, |
| 96 | type: PropTypes.oneOf(['default', 'error', 'warning', 'success']), |
| 97 | title: PropTypes.string, |
| 98 | modalComponentProps: PropTypes.object, |
| 99 | modalComponentName: PropTypes.string, |
| 100 | onConfirmed: PropTypes.func, |
| 101 | onDeclined: PropTypes.func, |
| 102 | confirmationButtonText: PropTypes.string, |
| 103 | cancelButtonText: PropTypes.string |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static defaultProps = { |
| 107 | show: false, |
| 108 | type: 'default', |
| 109 | title: '' |
| 110 | }; |
| 111 | |
| 112 | render() { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 113 | let {title, type, show, modalComponentName, modalComponentProps, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 114 | modalClassName, msg, onConfirmed, onDeclined, confirmationButtonText, cancelButtonText, onClose} = this.props; |
| 115 | const ComponentToRender = modalContentComponents[modalComponentName]; |
| 116 | return ( |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 117 | <Modal show={show} bsSize={modalComponentProps && modalComponentProps.size} className={`onborading-modal ${modalClassName || ''} ${type2HeaderColor[type]}`}> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 118 | <Modal.Header> |
| 119 | <Modal.Title>{title}</Modal.Title> |
| 120 | </Modal.Header> |
| 121 | <Modal.Body> |
miriame | 6511b4b | 2017-10-22 15:14:44 +0300 | [diff] [blame] | 122 | {ComponentToRender ? |
| 123 | <ComponentToRender {...modalComponentProps}/> : |
miriame | dee9585 | 2017-10-26 14:31:47 +0300 | [diff] [blame] | 124 | msg && typeof msg === 'string' ? |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 125 | <div> {msg.split('\n').map((txt, i) => <span key={i}> {txt} <br/> </span>)} </div> : |
miriame | dee9585 | 2017-10-26 14:31:47 +0300 | [diff] [blame] | 126 | msg |
miriame | 6511b4b | 2017-10-22 15:14:44 +0300 | [diff] [blame] | 127 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 128 | </Modal.Body> |
| 129 | {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && |
| 130 | <ModalFooter |
| 131 | type={type} |
| 132 | onConfirmed={onConfirmed} |
| 133 | onDeclined={onDeclined} |
| 134 | onClose={onClose} |
| 135 | confirmationButtonText={confirmationButtonText} |
| 136 | cancelButtonText={cancelButtonText}/>} |
| 137 | </Modal> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | componentDidUpdate() { |
| 142 | if (this.props.timeout) { |
| 143 | setTimeout(this.props.onClose, this.props.timeout); |
| 144 | } |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 145 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 146 | }; |
| 147 | |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame^] | 148 | GlobalModalView.propTypes = { |
| 149 | show: PropTypes.bool, |
| 150 | type: PropTypes.oneOf(['default', 'error', 'warning', 'success']), |
| 151 | title: PropTypes.string, |
| 152 | modalComponentProps: PropTypes.object, |
| 153 | modalComponentName: PropTypes.string, |
| 154 | onConfirmed: PropTypes.func, |
| 155 | onDeclined: PropTypes.func, |
| 156 | confirmationButtonText: PropTypes.string, |
| 157 | cancelButtonText: PropTypes.string |
| 158 | }; |
| 159 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 160 | export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(GlobalModalView); |