Skip to content

Instantly share code, notes, and snippets.

@jerairrest
Created October 23, 2015 05:11
Show Gist options
  • Save jerairrest/bcfd0e6692e90625d8d1 to your computer and use it in GitHub Desktop.
Save jerairrest/bcfd0e6692e90625d8d1 to your computer and use it in GitHub Desktop.

Revisions

  1. jerairrest created this gist Oct 23, 2015.
    1 change: 1 addition & 0 deletions NotficationActionTypes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    export const RECEIVE_MESSAGE = 'RECEIVE_MESSAGE';
    21 changes: 21 additions & 0 deletions NotficationActions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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
    })
    }
    }
    53 changes: 53 additions & 0 deletions NotficationComponent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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);
    23 changes: 23 additions & 0 deletions NotificationReducer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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;