-
-
Save box-turtle/e4306276601f40fc24ff to your computer and use it in GitHub Desktop.
React-toastr Redux Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as NotificationActions from './NotificationActionTypes'; | |
export function resetNotification() { | |
return { | |
type: NotificationActions.RECEIVE_MESSAGE, | |
payload: Object.assign({}, { | |
title: "", | |
message: "" | |
}) | |
} | |
} | |
export function displayNotification(title, message) { | |
return { | |
type: NotificationActions.RECEIVE_MESSAGE, | |
payload: Object.assign({}, { | |
title: title, | |
message: message | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const RECEIVE_MESSAGE = 'RECEIVE_MESSAGE'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import toastr from 'react-toastr'; | |
import { connect } from 'react-redux'; | |
import _ from 'lodash'; | |
import * as NotificationActions from './NotificationActions'; | |
let {ToastContainer} = toastr; | |
let ToastMessageFactory = React.createFactory(toastr.ToastMessage.jQuery); | |
class Notification extends Component { | |
constructor(props) { | |
super(props); | |
this.addAlert = this.addAlert.bind(this); | |
} | |
componentWillReceiveProps(nextprops) { | |
if (this.props.notification.message != nextprops.notification.message && | |
nextprops.notification.message != "") { | |
this.addAlert(nextprops.notification); | |
this.props.dispatch(NotificationActions.resetNotification()); | |
} | |
} | |
render() { | |
return ( | |
<ToastContainer toastMessageFactory={ToastMessageFactory} ref="container" | |
className="toast-container toast-bottom-right"/> | |
); | |
} | |
addAlert(message) { | |
this.refs.container.success( | |
message.message, | |
message.title, | |
{ | |
timeOut: 3000, | |
extendedTimeOut: 1000 | |
} | |
); | |
} | |
} | |
function mapStateToProps(state) { | |
return { | |
notification: state.app.data.notification | |
}; | |
} | |
export default connect(mapStateToProps)(Notification); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux'; | |
import * as NotificationActions from './NotificationActionTypes'; | |
const initialState = { | |
message: "", | |
title: "" | |
} | |
function notification(state, action) { | |
switch (action.type) { | |
case 'RECEIVE_MESSAGE': | |
return Object.assign({}, state, action.payload); | |
default: | |
return state; | |
} | |
} | |
const reducer = combineReducers({ | |
notification | |
}); | |
export default reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment