Skip to content

Commit

Permalink
feat(ToastContainer): add prevents duplicates functionality
Browse files Browse the repository at this point in the history
* Thanks to @RangarajKaushikSundar 
* Closes #60
  • Loading branch information
RangarajKaushikSundar authored and tomchentw committed Jul 7, 2016
1 parent 2b23a43 commit 3a8f772
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -85,6 +85,7 @@
"dependencies": {
"classnames": "^2.2.3",
"element-class": "^0.2.2",
"lodash": "^4.13.1",
"react-addons-update": "^0.14.0 || ^15.0.0",
"react-dom": "^0.14.0 || ^15.0.0"
}
Expand Down
13 changes: 10 additions & 3 deletions src/ToastContainer.js
Expand Up @@ -12,6 +12,8 @@ import {
default as ToastMessage,
} from "./ToastMessage";

import _ from "lodash";

export default class ToastContainer extends Component {

static propTypes = {
Expand Down Expand Up @@ -45,7 +47,7 @@ export default class ToastContainer extends Component {
state = {
toasts: [],
toastId: 0,
previousMessage: null,
messageList: [],
};

error(message, title, optionsOverride) {
Expand All @@ -72,7 +74,7 @@ export default class ToastContainer extends Component {

_notify(type, message, title, optionsOverride = {}) {
if (this.props.preventDuplicates) {
if (this.state.previousMessage === message) {
if (_.includes(this.state.messageList, message)) {
return;
}
}
Expand All @@ -99,9 +101,13 @@ export default class ToastContainer extends Component {
[`${this.props.newestOnTop ? `$unshift` : `$push`}`]: [newToast],
};

const messageOperation = {
[`${this.props.newestOnTop ? `$unshift` : `$push`}`]: [message],
};

const nextState = update(this.state, {
toasts: toastOperation,
previousMessage: { $set: message },
messageList: messageOperation,
});
this.setState(nextState);
}
Expand All @@ -126,6 +132,7 @@ export default class ToastContainer extends Component {
}
this.setState(update(this.state, {
toasts: { $splice: [[index, 1]] },
messageList: { $splice: [[index, 1]] },
}));
return true;
}, false);
Expand Down

0 comments on commit 3a8f772

Please sign in to comment.