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-action-creators

v0.7.3

Published

Configurable action creators for redux with sub-types

Downloads

25

Readme

Redux action creators

Configurable action creators for redux with sub-types

NPM Version NPM Downloads GitHub issues Licence

See source code for more information ;)

Installation

Install it globally:

npm install --save redux-action-creators

Usage

import {
  TYPE
  actionCreator,
  actionCreatorFactory
} from 'redux-action-creators';

// Normal action creators
export const openDrawer = actionCreator('OPEN_DRAWER');
export const closeDrawer = actionCreator('CLOSE_DRAWER');

// Normal action creator with some functionality
export const openModal = actionCreator('OPEN_MODAL', payload => {
  console.log(payload);

  // You can override standard action object here:
  // return { type: openModal[TYPE], meta: { section: 'main' } }
});

// Then just dispatch an action with some payload
store.dispatch(signOut({ msg: 'Bye!' }));

// asyncActionCreator
export const START = Symbol('START');
export const END = Symbol('END');
export const SUCCESS = Symbol('SUCCESS');
export const FAILURE = Symbol('FAILURE');

export const asyncActionCreator = actionCreatorFactory({
  subTypes: { START, END, SUCCESS, FAILURE }
});

// Async action with redux-thunk
export const signIn = asyncActionCreator('signIn', payload => (dispatch) => {
  const { username, password } = payload;

  dispatch(signIn[START]());

  // api is an axios instance with interceptors: const api = axios.create({ baseURL: apiRoot });
  api.post(API_SIGN_IN, { username, password }).then(
    (response) => {
      const { token, email, avatar } = response.data;
      localStorage.setItem('auth_token', token);
      dispatch(signIn[END]());
      dispatch(signIn[SUCCESS]({ email, avatar }));
    },
    (error) => {
      dispatch(signIn[END]());
      dispatch(signIn[FAILURE](error));
    }
  );
});

// You can write a helper for handling errors
const asyncErrorHandler = ($actionCreator, dispatch) =>
  (error) => {
    dispatch($actionCreator[END]());
    dispatch($actionCreator[FAILURE](error));
  };

// Sync thunkActionCreator
export const CALL = Symbol('CALL');
export const thunkActionCreator = actionCreatorFactory({
  subTypes: { CALL }
});

// Later in your code...
export const displayError = thunkActionCreator('DISPLAY_ERROR', payload => (dispatch) => {
  const { error, message } = payload;
  dispatch(displayError[CALL]());
  dispatch(openSnackbar({
    message: error
      ? String(error)
      : message || 'Unknown error'
  }));
});

// Custom async action creator with sub-types and prefix
export const CREATE = Symbol('CREATE');
export const READ = Symbol('READ');
export const UPDATE = Symbol('UPDATE');
export const DELETE = Symbol('DELETE');

export const crudActionCreator = actionCreatorFactory({
  actionCreator: asyncActionCreator, {
  subTypes: { CREATE, READ, UPDATE, DELETE }
  prefix: '@@app/'
}); // You can use prefixes

// Create async crud actionCreator
const todo = crudActionCreator('todo');

// Complex ajax action creator with map of sub-types
export const REQUEST = Symbol('REQUEST');
export const ERROR = Symbol('ERROR');
export const NORMAL = Symbol('NORMAL');
export const TIMEOUT = Symbol('TIMEOUT');
export const CANCEL = Symbol('CANCEL');

const _requestActionCreator = actionCreatorFactory({
  subTypes: { START, SUCCESS, END }
});

const _errorActionCreator = actionCreatorFactory({
  subTypes: { NORMAL, TIMEOUT, CANCEL, FAILURE }
});

export const ajaxActionCreator = actionCreatorFactory({
  subTypes: [
    [_requestActionCreator, { REQUEST }],
    [_errorActionCreator, { ERROR }],
  ]
});

const fetchUser = ajaxActionCreator('FETCH_USER')

// In reducer:
switch(action.type) {
  case todo[CREATE][START][TYPE]:
    //...
  // You can use $$ alias for TYPE (import it first)
  case todo[CREATE][SUCCESS][$$]:
    //...
  case signIn[START][$$]:
    return { ...state, fetching: true };
  case signIn[END][$$]:
    return { ...state, fetching: false };
  case signIn[SUCCESS][$$]:
    return { ...state, user: action.payload };
  case displayError[CALL][TYPE]:
    //...
  case openSnackbar[TYPE]:
    //...
  case openDrawer[TYPE]:
    //...
  case closeDrawer[TYPE]:
    //...
  case fetchUser[REQUEST][START][$$]:
    //...
  case fetchUser[ERROR][NORMAL][$$]:
    //...
}

Author

@doasync