npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-flash-messages

v1.0.0

Published

Storing flash messages and removing them via Redux.

Downloads

389

Readme

About

Build Status Codecov

This library makes it easy to create flash messages and to store them in a Redux store.

What makes this project a little different from most flash message libraries is that it is UI agnostic. This library does not render the FlashMessages for you it only stores them!

Installation

npm install redux-flash-messages --save

Getting started.

We assume you have a working Redux project, if you do not yet have Redux add Redux to your project by following the Redux's instructions.

First install the following dependencies in the package.json:

  1. "react-redux": "5.0.3",
  2. "redux": "3.6.0",

Now add the flash-message-reducer to your rootReducer for example:

import { combineReducers } from 'redux';

import { flashMessage, FlashMessageStore } from 'redux-flash-messages';

export interface Store {
  flashMessage: FlashMessageStore;
}

// Use ES6 object literal shorthand syntax to define the object shape
const rootReducer = combineReducers({
  flashMessage
});

export default rootReducer;

This should add the FlashMessageStore to Redux, which will store the flash messages.

Next you have to configure the flashMessages module:

import { createStore } from 'redux';
import { configureFlashMessages } from 'redux-flash-messages';

export const store = createStore(
  rootReducer,
);

configureFlashMessages({
  // The dispatch function for the Redux store.
  dispatch: store.dispatch
});

The redux-flash-messages module must be configured before the application is rendered.

Rendering flash messages

Next we need to render the flash messages from the Redux store. How you do this is entirely up to you, but here is a small example:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Store, Dispatch } from 'redux';

import { removeFlashMessage, FlashMessage as FlashMessageShape } from 'redux-flash-messages';

import './FlashMessage.css';

interface Props {
  messages: Array<FlashMessageShape>;
  dispatch: Dispatch;
}

export class FlashMessage extends Component<Props, {}> {

  onFlashMessageClick(flashMessage: FlashMessageShape) {
    /* 
      Make sure the onClick is called when a user clicks 
      on the flash message.
      
      Otherwise callbacks on Flash Messages will not work.
    */
    flashMessage.onClick(flashMessage);

    // This implementation deletes the flash message when it is clicked.
    this.props.dispatch(removeFlashMessage(flashMessage.id));
  }

  render() {
    const messages = this.props.messages;

    return (
      <div>
        { messages.map((message) => this.renderMessage(message))}
      </div>
    );
  }

  /* 
    This renders a rather simple flash message. 
    
    But you could and should use the 'type' property to
    render the flash message in a different style for each 'type'.
  */
  renderMessage(message: FlashMessageShape) {
    return (
      <div
        key={ message.id }
        className={ `flash-message ${message.type}`}
        onClick={ () => this.onFlashMessageClick(message) }
      >
        { message.text }
      </div>
    );
  }
}

export default connect((store: Store) => {
  return {
    messages: store.flashMessage.messages
  };
})(FlashMessage);

Where the contents of 'FlashMessage.css' is:

.flash-message {
  position: absolute;
  width: 100%;
  height: 50px;
  text-align: center;
  z-index: 9000;
  background-color: white;
  border: black solid 2px;
  padding: 12.5px 0;
}

Sending flash messages

Now that we can see the flash messages we can use the following convenience methods to send flash messages:

import { addError, addWarning, addSuccess, addInfo, addApocalypse } from 'redux-flash-messages';

// Renders a message for 10000 milliseconds
addError({ text: 'Epic error', data: { age: 12 }, onClick: (flashMessage) => {
  console.log('I was clicked');
  console.log(flashMessage);
}});

// Renders a message for 7000 milliseconds
addWarning({ text: 'Epic warning', data: { tree: 'house' }, onClick: (flashMessage) => {
  console.log('I was clicked');
  console.log(flashMessage);
}});

// Renders a message for 2000 milliseconds
addSuccess({ text: 'Epic success', data: { win: true }, onClick: (flashMessage) => {
  console.log('I was clicked');
  console.log(flashMessage);
}});

// Renders a message for 5000 milliseconds
addInfo({ text: 'Epic info', data: { yo: 'man' }, onClick: (flashMessage) => {
  console.log('I was clicked');
  console.log(flashMessage);
}});

// Renders a message which is not automatically removed
addApocalypse({ text: 'TOTAL ANNIHILATION', data: { fail: true }, onClick: (flashMessage) => {
  console.log('I was clicked');
  console.log(flashMessage);
}});

The onClick and the data keys are optional. The data key can be used to send whatever data you want to the component which renders the flash message.

Creating a custom flash message type.

If the default types are not enough you can always create your own flash message creator:

You do this by calling addFlashMessageOfType manually.

import { addFlashMessageOfType } from 'redux-flash-messages';

enum CustomTypes {
  Notice = 'NOTICE',
}

export function addNotice({ text, onClick, data }: FlashMessageConfig) {
  addFlashMessageOfType(CustomTypes.Notice, 1000, text, onClick, data);
}