3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * EventCenter module to display a list of events from the system
21 * @module framework/widgets/skyquake_container/EventCenter
22 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
26 import React from 'react';
27 import { Link } from 'react-router';
28 import Utils from 'utils/utils.js';
29 import Crouton from 'react-crouton';
30 import TreeView from 'react-treeview';
31 import _ from 'lodash';
32 import '../../../node_modules/react-treeview/react-treeview.css';
33 import './eventCenter.scss';
35 export default class EventCenter extends React.Component {
39 this.state.isOpen = false;
42 componentWillReceiveProps(props) {
45 let notificationList = sessionStorage.getItem('notificationList');
46 let latestNotification = sessionStorage.getItem('latestNotification');
48 if (props.newNotificationEvent && props.newNotificationMsg) {
49 if (latestNotification) {
50 latestNotification = JSON.parse(latestNotification);
51 if (!_.isEqual(props.newNotificationMsg, latestNotification)) {
52 stateObject.newNotificationEvent = props.newNotificationEvent;
53 stateObject.newNotificationMsg = props.newNotificationMsg;
54 sessionStorage.setItem('latestNotification', JSON.stringify(stateObject.newNotificationMsg));
56 stateObject.newNotificationEvent = false;
57 stateObject.newNotificationMsg = null;
60 stateObject.newNotificationEvent = true;
61 stateObject.newNotificationMsg = props.newNotificationMsg;
62 sessionStorage.setItem('latestNotification', JSON.stringify(stateObject.newNotificationMsg));
65 stateObject.newNotificationEvent = false;
66 stateObject.newNotificationMsg = null;
69 if (notificationList) {
70 stateObject.notifications = _.merge(notificationList, props.notifications);
72 stateObject.notifications = props.notifications;
74 sessionStorage.setItem('notifications', JSON.stringify(stateObject.notifications));
75 this.setState(stateObject);
78 newNotificationReset = () => {
80 newNotificationEvent: false
84 onClickToggleOpenClose(event) {
85 this.props.onToggle();
87 isOpen: !this.state.isOpen
90 constructTree(details) {
92 for (let key in details) {
93 if (typeof(details[key]) == "object") {
95 <TreeView key={key} nodeLabel={key}>
96 {this.constructTree(details[key])}
101 markup.push((<div key={key} className="info">{key} = {details[key].toString()}</div>));
107 getNotificationFields(notification) {
108 let notificationFields = {};
111 notificationFields.source = notification.source;
112 notificationFields.eventTime = notification.eventTime;
114 Object.keys(notification).map((notificationKey) => {
115 if (_.indexOf(['source', 'eventTime'], notificationKey) == -1) {
116 notificationFields.eventKey = notificationKey;
117 notificationFields.details = notification[notificationFields.eventKey];
122 return notificationFields;
127 let displayNotifications = [];
128 let displayNotificationsTableHead = null;
130 if (this.state.notifications && this.state.notifications.length > 0) {
131 displayNotificationsTableHead = (
133 <tr key='header' className='header'>
134 <th className='source column'> Source Event Stream </th>
135 <th className='timestamp column'> Timestamp </th>
136 <th className='event column'> Event </th>
137 <th className='details column'> Details </th>
143 this.state.notifications && this.state.notifications.map((notification, notifIndex) => {
144 let notificationFields = {};
146 notificationFields = this.getNotificationFields(notification);
148 displayNotifications.push(
149 <tr key={notifIndex} className='notificationItem'>
150 <td className='source column'> {notificationFields.source} </td>
151 <td className='timestamp column'> {notificationFields.eventTime} </td>
152 <td className='event column'> {notificationFields.eventKey} </td>
153 <td className='details column'>
154 <TreeView key={notifIndex + '-detail'} nodeLabel='Event Details'>
155 {this.constructTree(notificationFields.details)}
162 let openedClassName = this.state.isOpen ? 'open' : 'closed';
164 <div className={'eventCenter ' + openedClassName}>
166 <div className='notification'>
169 message={this.getNotificationFields(this.state.newNotificationMsg).eventKey +
170 ' notification received. Check Event Center for more details.'}
172 hidden={!(this.state.newNotificationEvent && this.state.newNotificationMsg)}
173 onDismiss={this.newNotificationReset}
174 timeout={this.props.dismissTimeout}
177 <h1 className='title' data-open-close-icon={this.state.isOpen ? 'open' : 'closed'}
178 onClick={this.onClickToggleOpenClose.bind(this)}>
181 <div className='notificationListBody'>
182 <table className='notificationList'>
183 {displayNotificationsTableHead}
185 {displayNotifications}
196 EventCenter.defaultProps = {