AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import React from 'react'; |
| 18 | import {connect} from 'react-redux'; |
| 19 | |
| 20 | import Modal from 'nfvo-components/modal/Modal.jsx'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 21 | import Button from 'sdc-ui/lib/react/Button.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 22 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 23 | import {modalContentComponents} from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 24 | import {actionTypes, typeEnum} from './GlobalModalConstants.js'; |
| 25 | |
| 26 | |
| 27 | const typeClass = { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 28 | 'default': 'default', |
| 29 | error: 'negative', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 30 | warning: 'warning', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 = { |
| 35 | 'default': 'primary', |
| 36 | error: 'danger', |
| 37 | warning: 'warning', |
| 38 | success: 'success' |
| 39 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 40 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 41 | |
| 42 | const ModalFooter = ({type, onConfirmed, onDeclined, onClose, confirmationButtonText, cancelButtonText}) => { |
| 43 | let myPropsForNoConfirmed = {}; |
| 44 | if (onConfirmed) { |
| 45 | myPropsForNoConfirmed.btnType = 'outline'; |
| 46 | } |
| 47 | return ( |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 48 | <Modal.Footer> |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 49 | <div className='sdc-modal-footer'> |
| 50 | {onConfirmed && <Button color={typeClass[type]} onClick={() => { |
| 51 | onConfirmed(); |
| 52 | onClose(); |
| 53 | }}>{confirmationButtonText}</Button>} |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 54 | <Button {...myPropsForNoConfirmed} color={typeClass[type]} onClick={onDeclined ? () => { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 55 | onDeclined(); |
| 56 | onClose();} : () => onClose()}> |
| 57 | {cancelButtonText} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 58 | </Button> |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 59 | </div> |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 60 | </Modal.Footer> |
| 61 | ); |
| 62 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | |
| 64 | ModalFooter.defaultProps = { |
| 65 | type: 'default', |
| 66 | confirmationButtonText: i18n('OK'), |
| 67 | cancelButtonText: i18n('Cancel') |
| 68 | }; |
| 69 | |
| 70 | export const mapStateToProps = ({modal}) => { |
| 71 | const show = !!modal; |
| 72 | return { |
| 73 | show, |
| 74 | ...modal |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | export const mapActionToProps = (dispatch) => { |
| 79 | return { |
| 80 | onClose: () => dispatch({type: actionTypes.GLOBAL_MODAL_CLOSE}) |
| 81 | }; |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | export class GlobalModalView extends React.Component { |
| 86 | |
| 87 | static propTypes = { |
| 88 | show: React.PropTypes.bool, |
| 89 | type: React.PropTypes.oneOf(['default', 'error', 'warning', 'success']), |
| 90 | title: React.PropTypes.string, |
| 91 | modalComponentProps: React.PropTypes.object, |
| 92 | modalComponentName: React.PropTypes.string, |
| 93 | onConfirmed: React.PropTypes.func, |
| 94 | onDeclined: React.PropTypes.func, |
| 95 | confirmationButtonText: React.PropTypes.string, |
| 96 | cancelButtonText: React.PropTypes.string |
| 97 | }; |
| 98 | |
| 99 | static defaultProps = { |
| 100 | show: false, |
| 101 | type: 'default', |
| 102 | title: '' |
| 103 | }; |
| 104 | |
| 105 | render() { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 106 | let {title, type, show, modalComponentName, modalComponentProps, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 107 | modalClassName, msg, onConfirmed, onDeclined, confirmationButtonText, cancelButtonText, onClose} = this.props; |
| 108 | const ComponentToRender = modalContentComponents[modalComponentName]; |
| 109 | return ( |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 110 | <Modal show={show} bsSize={modalComponentProps && modalComponentProps.size} className={`onborading-modal ${modalClassName || ''} ${type2HeaderColor[type]}`}> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 111 | <Modal.Header> |
| 112 | <Modal.Title>{title}</Modal.Title> |
| 113 | </Modal.Header> |
| 114 | <Modal.Body> |
miriame | 6511b4b | 2017-10-22 15:14:44 +0300 | [diff] [blame] | 115 | {ComponentToRender ? |
| 116 | <ComponentToRender {...modalComponentProps}/> : |
miriame | dee9585 | 2017-10-26 14:31:47 +0300 | [diff] [blame] | 117 | msg && typeof msg === 'string' ? |
| 118 | <div> {msg.split('\n').map(txt => <span> {txt} <br/> </span>)} </div> : |
| 119 | msg |
miriame | 6511b4b | 2017-10-22 15:14:44 +0300 | [diff] [blame] | 120 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 121 | </Modal.Body> |
| 122 | {(onConfirmed || onDeclined || type !== typeEnum.DEFAULT) && |
| 123 | <ModalFooter |
| 124 | type={type} |
| 125 | onConfirmed={onConfirmed} |
| 126 | onDeclined={onDeclined} |
| 127 | onClose={onClose} |
| 128 | confirmationButtonText={confirmationButtonText} |
| 129 | cancelButtonText={cancelButtonText}/>} |
| 130 | </Modal> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | componentDidUpdate() { |
| 135 | if (this.props.timeout) { |
| 136 | setTimeout(this.props.onClose, this.props.timeout); |
| 137 | } |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 138 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(GlobalModalView); |