mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 10:25:08 +01:00
add que functionality to globalnotification
This commit is contained in:
parent
e0e75151e0
commit
d3fc902411
@ -6,8 +6,9 @@ import alt from '../alt';
|
|||||||
class GlobalNotificationActions {
|
class GlobalNotificationActions {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.generateActions(
|
this.generateActions(
|
||||||
'updateGlobalNotification',
|
'appendGlobalNotification',
|
||||||
'resetGlobalNotification'
|
'shiftGlobalNotification',
|
||||||
|
'emulateEmptyStore'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,36 +15,38 @@ let GlobalNotification = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
GlobalNotificationStore.listen(this.onChange);
|
GlobalNotificationStore.unlisten(this.onChange);
|
||||||
},
|
},
|
||||||
|
|
||||||
getInititalState() {
|
getInititalState() {
|
||||||
return GlobalNotificationStore.getState();
|
return this.extractFirstElem(GlobalNotificationStore.getState().notificationQue);
|
||||||
|
},
|
||||||
|
|
||||||
|
extractFirstElem(l) {
|
||||||
|
return l.length > 0 ? l[0] : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
onChange(state) {
|
onChange(state) {
|
||||||
this.setState(state);
|
let notification = this.extractFirstElem(state.notificationQue);
|
||||||
if(state.message && state.dismissAfter) {
|
|
||||||
setTimeout(GlobalNotificationActions.resetGlobalNotification, state.dismissAfter);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onClick() {
|
if(notification) {
|
||||||
if(this.state.onClick) {
|
this.setState(notification);
|
||||||
this.state.onClick();
|
} else {
|
||||||
|
this.replaceState(null);
|
||||||
}
|
}
|
||||||
GlobalNotificationActions.resetGlobalNotification();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let className = 'ascribe-global-notification ';
|
let className = 'ascribe-global-notification ';
|
||||||
className += this.state && this.state.message ? 'ascribe-global-notification-on' : 'ascribe-global-notification-off';
|
let message = this.state && this.state.message ? this.state.message : null;
|
||||||
|
|
||||||
return (
|
className += message ? 'ascribe-global-notification-on' : 'ascribe-global-notification-off';
|
||||||
<Row onClick={this.onClick}>
|
|
||||||
|
return (
|
||||||
|
<Row>
|
||||||
<Col>
|
<Col>
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
<div>{this.state ? this.state.message : null}</div>
|
<div>{message ? message : null}</div>
|
||||||
</div>
|
</div>
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
|
@ -14,6 +14,7 @@ import MenuItem from 'react-bootstrap/lib/MenuItem';
|
|||||||
import { getLangText } from '../utils/lang_utils';
|
import { getLangText } from '../utils/lang_utils';
|
||||||
|
|
||||||
import GlobalNotificationActions from '../actions/global_notification_actions';
|
import GlobalNotificationActions from '../actions/global_notification_actions';
|
||||||
|
import GlobalNotificationModel from '../models/global_notification_model';
|
||||||
|
|
||||||
let Link = Router.Link;
|
let Link = Router.Link;
|
||||||
|
|
||||||
@ -37,7 +38,8 @@ let Header = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
showNotification() {
|
showNotification() {
|
||||||
GlobalNotificationActions.updateGlobalNotification({message: 'transaction successful', dismissAfter: 2000});
|
let notification = new GlobalNotificationModel('transaction successful', 3500);
|
||||||
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
13
js/models/global_notification_model.js
Normal file
13
js/models/global_notification_model.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
export default class GlobalNotificationModel {
|
||||||
|
constructor(message, dismissAfter) {
|
||||||
|
if(!message) {
|
||||||
|
throw new Error('A notifications message must be defined.');
|
||||||
|
} else {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dismissAfter = dismissAfter ? dismissAfter : 0;
|
||||||
|
}
|
||||||
|
}
|
@ -6,34 +6,34 @@ import GlobalNotificationActions from '../actions/global_notification_actions';
|
|||||||
|
|
||||||
class GlobalNotificationStore {
|
class GlobalNotificationStore {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.message = '';
|
this.notificationQue = [];
|
||||||
this.action = '';
|
|
||||||
this.styles = {};
|
|
||||||
this.onClick = null;
|
|
||||||
this.dismissAfter = 0;
|
|
||||||
|
|
||||||
this.bindActions(GlobalNotificationActions);
|
this.bindActions(GlobalNotificationActions);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdateGlobalNotification({message, action, styles, onClick, dismissAfter}) {
|
onAppendGlobalNotification(newNotification) {
|
||||||
if(!message) {
|
let notificationDelay = 0;
|
||||||
throw new Error('A notifications message must be defined.');
|
for(let i = 0; i < this.notificationQue.length; i++) {
|
||||||
} else {
|
notificationDelay += this.notificationQue[i].dismissAfter;
|
||||||
this.message = message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.action = action ? action : '';
|
this.notificationQue.push(newNotification);
|
||||||
this.styles = styles ? styles : {};
|
setTimeout(GlobalNotificationActions.emulateEmptyStore, notificationDelay + newNotification.dismissAfter);
|
||||||
this.onClick = onClick ? onClick : null;
|
|
||||||
this.dismissAfter = dismissAfter ? dismissAfter : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onResetGlobalNotification() {
|
onEmulateEmptyStore() {
|
||||||
this.message = '';
|
let actualNotificitionQue = this.notificationQue.slice();
|
||||||
this.action = '';
|
|
||||||
this.styles = {};
|
this.notificationQue = [];
|
||||||
this.onClick = null;
|
|
||||||
this.dismissAfter = 0;
|
setTimeout(() => {
|
||||||
|
this.notificationQue = actualNotificitionQue.slice();
|
||||||
|
GlobalNotificationActions.shiftGlobalNotification();
|
||||||
|
}, 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
onShiftGlobalNotification() {
|
||||||
|
this.notificationQue.shift();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user