ih-portal
Version:
A project for connecting interaction hub services with catalyst-ui components
66 lines (54 loc) • 1.83 kB
JSX
import React, { Component, PropTypes } from 'react';
import ObjectInspector from 'react-object-inspector';
export default class Notifications extends Component {
componentDidMount() {
const { notifications } = this.props;
if (!notifications || (notifications.lastAction && notifications.lastAction.status === 'error') || !notifications.data) {
const requestParams = {
url: __NOTIFICATIONS_URL__,
auth: [__USERNAME__, __PASSWORD__],
};
this.props.fetch(requestParams);
}
}
invalidateNotifications() {
const requestParams = {
url: __NOTIFICATIONS_URL__,
auth: [__USERNAME__, __PASSWORD__],
};
this.props.invalidate(requestParams);
}
copyToClipboard() {
const copyText = JSON.stringify(this.props.notifications.data);
window.prompt('Copy Response:', copyText);
}
render() {
let isFetching = false;
let hasData = false;
let responseDisp = '';
if (this.props.notifications) {
isFetching = this.props.notifications.isFetching;
if (this.props.notifications.data) {
hasData = true;
const json = this.props.notifications.data;
responseDisp = <ObjectInspector data={json}/>;
}
}
return (
<div>
<h2>Notifications Service</h2>
<input className="invalidateBtn" type="button" value="Invalidate Notifications" onClick={() => this.invalidateNotifications()} />
<input className="copyBtn" type="button" value="Copy Response" onClick={() => this.copyToClipboard()} disabled={isFetching || !hasData} />
<br /><br/>
{isFetching ? <p>Fetching Data...</p> : responseDisp}
<br/>
</div>
);
}
}
const { func, object } = PropTypes;
Notifications.propTypes = {
notifications: object,
fetch: func,
invalidate: func,
};