update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / widgets / skyquake_notification / skyquakeNotification.jsx
1 import React from 'react';
2 import Crouton from 'react-crouton';
3 import NETCONF_ERRORS from './netConfErrors.js';
4
5 class SkyquakeNotification extends React.Component {
6     constructor(props) {
7         super(props);
8         this.state = {};
9         this.state.displayNotification = props.visible;
10         this.state.notificationMessage = '';
11         this.state.notificationType = 'error';
12     }
13     componentWillReceiveProps(props) {
14         if(props.visible) {
15             this.processMessage(props.data);
16         } else {
17             this.setState({displayNotification: props.visible});
18         }
19     }
20     buildNetconfError(data) {
21         let error = data;
22         try {
23             let info = JSON.parse(data);
24             let rpcError = info.body || info.errorMessage.body || info.errorMessage.error;
25             if (rpcError && typeof rpcError === 'string') {
26                 const index = rpcError.indexOf('{');
27                 if (index >= 0) {
28                     rpcError = JSON.parse(rpcError.substr(index));
29                 } else {
30                     return rpcError;
31                 }
32             }
33             if (!rpcError) {
34                 return error;
35             }
36             info = rpcError["rpc-reply"]["rpc-error"];
37             let errorTag = info['error-tag']
38             error = `
39                 ${NETCONF_ERRORS[errorTag] && NETCONF_ERRORS[errorTag].description || 'Unknown NETCONF Error'}
40                 PATH: ${info['error-path']}
41                 INFO: ${JSON.stringify(info['error-info'])}
42             `
43         } catch (e) {
44             console.log('Unexpected string sent to buildNetconfError: ', e);
45         }
46         return error;
47     }
48     processMessage(data) {
49         let state = {
50                 displayNotification: true,
51                 notificationMessage: data,
52                 notificationType: 'error',
53                 displayScreenLoader: false
54             }
55         if(typeof(data) == 'string') {
56             //netconf errors will be json strings
57             state.notificationMessage = this.buildNetconfError(data);
58         } else {
59             let message = data.msg || '';
60             if(data.type) {
61                 state.notificationType = data.type;
62             }
63             if(data.rpcError){
64                 message += " " + this.buildNetconfError(data.rpcError);
65             }
66             state.notificationMessage = message;
67         }
68         console.log('NOTIFICATION: ', state.notificationMessage)
69         this.setState(state);
70     }
71     render() {
72         const {displayNotification, notificationMessage, notificationType, ...state} = this.state;
73         return (
74             <Crouton
75                 id={Date.now()}
76                 message={notificationMessage}
77                 type={notificationType}
78                 hidden={!(displayNotification && notificationMessage)}
79                 onDismiss={this.props.onDismiss}
80                 timeout={10000}
81             />
82         )
83     }
84 }
85 SkyquakeNotification.defaultProps = {
86     data: {},
87     onDismiss: function(){}
88 }
89 export default SkyquakeNotification;