Skip to content

Commit 8942e0c

Browse files
authored
Support for removing notifications by dispatching removeAll action (#33)
* Support for removing notifications by dispatching removeAll action * Readme - document hide action
1 parent 4549543 commit 8942e0c

File tree

10 files changed

+64
-20
lines changed

10 files changed

+64
-20
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ There is a working example in example/src/**
154154
### Properties
155155
It accepts all properties as react-notification-system does, actually it pipes them in the react-notification-system.
156156

157+
### Actions
158+
``` javascript
159+
import Notifications from 'react-notification-system-redux';
160+
161+
dispatch(Notifications.show(notification, level));
162+
dispatch(Notifications.success(notification));
163+
dispatch(Notifications.error(notification));
164+
dispatch(Notifications.warning(notification));
165+
dispatch(Notifications.info(notification));
166+
dispatch(Notifications.hide(uid)); // Hides notification based on uid
167+
dispatch(Notifications.removeAll()); // Removes all notifications
168+
```
169+
170+
157171
## Development (`src`, `lib` and the build process)
158172

159173
**NOTE:** The source code for the component is in `src`. A transpiled CommonJS version (generated with Babel) is available in `lib` for use with node.js, browserify and webpack. A UMD bundle is also built to `dist`, which can be included without the need for any build system.

example/src/components/container.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Container extends React.Component {
2222
super();
2323

2424
this.handleClick = this.handleClick.bind(this);
25+
this.handleRemoveAll = this.handleRemoveAll.bind(this);
2526
}
2627

2728
dispatchNotification(fn, timeout) {
@@ -37,13 +38,17 @@ class Container extends React.Component {
3738
this.dispatchNotification(Notifications.info, 1000);
3839
}
3940

41+
handleRemoveAll() {
42+
this.context.store.dispatch(Notifications.removeAll());
43+
}
44+
4045
render() {
4146
const {notifications} = this.props;
4247

4348
return (
4449
<div>
4550
<button onClick={this.handleClick}>Spawn some notifications!!!</button>
46-
51+
<button onClick={this.handleRemoveAll}>Remove all notifications</button>
4752
<Notifications notifications={notifications} />
4853
</div>
4954
);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"sinon": "^1.17.6"
4545
},
4646
"dependencies": {
47-
"react-notification-system": "^0.2.7"
47+
"react-notification-system": "^0.2.x"
4848
},
4949
"peerDependencies": {
5050
"react": "^0.14 || ^15.0.0-rc || ^15.0",

src/actions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RNS_SHOW_NOTIFICATION, RNS_HIDE_NOTIFICATION} from './const';
1+
import {RNS_SHOW_NOTIFICATION, RNS_HIDE_NOTIFICATION, RNS_REMOVE_ALL_NOTIFICATIONS} from './const';
22

33
//Example opts
44
// {
@@ -45,3 +45,7 @@ export function hide(uid) {
4545
uid
4646
};
4747
}
48+
49+
export function removeAll() {
50+
return { type: RNS_REMOVE_ALL_NOTIFICATIONS };
51+
}

src/const.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const RNS_SHOW_NOTIFICATION = 'RNS_SHOW_NOTIFICATION';
22
export const RNS_HIDE_NOTIFICATION = 'RNS_HIDE_NOTIFICATION';
3+
export const RNS_REMOVE_ALL_NOTIFICATIONS = 'RNS_REMOVE_ALL_NOTIFICATIONS';

src/notifications.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,32 @@ class Notifications extends React.Component {
1414
componentWillReceiveProps(nextProps) {
1515
const {notifications} = nextProps;
1616
const notificationIds = notifications.map(notification => notification.uid);
17+
const systemNotifications = this.system().state.notifications || [];
1718

18-
// Get all active notifications from react-notification-system
19-
/// and remove all where uid is not found in the reducer
20-
(this.system().state.notifications || []).forEach(notification => {
21-
if (notificationIds.indexOf(notification.uid) < 0) {
22-
this.system().removeNotification(notification.uid);
23-
}
24-
});
25-
26-
notifications.forEach(notification => {
27-
this.system().addNotification({
28-
...notification,
29-
onRemove: () => {
30-
this.context.store.dispatch(actions.hide(notification.uid));
31-
notification.onRemove && notification.onRemove();
19+
if (notifications.length > 0) {
20+
// Get all active notifications from react-notification-system
21+
/// and remove all where uid is not found in the reducer
22+
(systemNotifications).forEach(notification => {
23+
if (notificationIds.indexOf(notification.uid) < 0) {
24+
console.log('removing', this.system().state.notifications);
25+
this.system().removeNotification(notification.uid);
3226
}
3327
});
34-
});
28+
29+
notifications.forEach(notification => {
30+
this.system().addNotification({
31+
...notification,
32+
onRemove: () => {
33+
this.context.store.dispatch(actions.hide(notification.uid));
34+
notification.onRemove && notification.onRemove();
35+
}
36+
});
37+
});
38+
}
39+
40+
if ((this.props.notifications !== notifications) && notifications.length === 0) {
41+
this.system().clearNotifications();
42+
}
3543
}
3644

3745
shouldComponentUpdate(nextProps) {

src/reducer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RNS_SHOW_NOTIFICATION, RNS_HIDE_NOTIFICATION} from './const';
1+
import {RNS_SHOW_NOTIFICATION, RNS_HIDE_NOTIFICATION, RNS_REMOVE_ALL_NOTIFICATIONS} from './const';
22

33
export default function Notifications(state = [], action = {}) {
44
switch(action.type) {
@@ -12,6 +12,8 @@ export default function Notifications(state = [], action = {}) {
1212
return state.filter(notification => {
1313
return notification.uid !== action.uid;
1414
});
15+
case RNS_REMOVE_ALL_NOTIFICATIONS:
16+
return [];
1517
}
1618
return state;
1719
}

test/__tests__/actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('actions', () => {
1717
expect(Actions.success().uid).to.be.defined;
1818
});
1919

20-
if('sets the custom uid when provided', () => {
20+
it('sets the custom uid when provided', () => {
2121
expect(Actions.success({ uid: 1 }).uid).to.equal(1);
2222
});
2323
});

test/__tests__/const.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ describe('constants', () => {
55
it('should be defined', () => {
66
expect(Constants.RNS_SHOW_NOTIFICATION).to.be.defined;
77
expect(Constants.RNS_HIDE_NOTIFICATION).to.be.defined;
8+
expect(Constants.RNS_REMOVE_ALL_NOTIFICATIONS).to.be.defined;
89
});
910
});

test/__tests__/reducer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ describe('reducer', () => {
2424
const newState = Reducer(state, Actions.hide(uid));
2525
expect(newState.length).to.equal(0);
2626
});
27+
28+
it('removes all notifications', () => {
29+
const state = Reducer([], Actions.success({ uid: 1 }));
30+
const newState = Reducer(state, Actions.success({ uid: 2 }));
31+
const emptyState = Reducer(newState, Actions.removeAll());
32+
33+
expect(newState.length).to.equal(2);
34+
expect(emptyState.length).to.equal(0);
35+
});
2736
});

0 commit comments

Comments
 (0)