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-namespace

v1.0.2

Published

redux actions with inherited classes

Downloads

133

Readme

This is a javascript library for creating easy redux actions with less code. Based in class and inheritance

Getting Started

Installation

npm install redux-action-namespace
# or
yarn add redux-action-namespace

Content

Basic usage

You can create the redux and the actions in the same class.

import { DefaultAction, createActionTypes, createReducer } from 'redux-action-namespace'

// declare intial state
const initialState = {
	count: 0
};

// declare action classes
class INCREMENT_COUNT extends DefaultAction {
	callRdx(state, action) {
		const count = state.count++;
		return {
			...state,
			count
		}
	}
}

class DECREMENT_COUNT extends DefaultAction {
	//redefine callRDX method
	callRdx(state, action) {
		const count = state.count--;
		return {
			...state,
			count
		}
	}
}
const actions = {
	INCREMENT_COUNT : new INCREMENT_COUNT(),
	DECREMENT_COUNT: new DECREMENT_COUNT()
}
// create actions, namespace is required
const handlers = createActionTypes(actions, "COUNT_NS");

//export the reducer and add to the reducer state.
export const countReducer = createReducer(initialState, handlers);

export default actions;

// use the actions

state.dispatch(actions.INCREMENT_COUNT.call());
console.log(state.countReducer.count) // expect be 1

state.dispatch(actions.DECREMENT_COUNT.call());
console.log(state.countReducer.count) // expect be 0

Default Action

Default Action do not required custom implementation. Don't need to even describe the callRdx

import { DefaultAction, createActionTypes, createReducer } from 'redux-action-namespace'

// declare intial state
const initialState = {
	id: null,  
        user:{ address:null }
};

// declare action classes
const actions = {
	SET_ID : new DefaultAction("id"),
	SET_CITY_NAME: new DefaultAction("address.city.name")
}
// create actions, namespace is required
const handlers = createActionTypes(actions, "USER_NS");

//export the reducer and add to the reducer state.
export const userReducer = createReducer(initialState, handlers);

export default actions;

// use the actions

state.dispatch(actions.SET_ID.call(1));
console.log(state.userReducer.id) // expect be 1

state.dispatch(actions.SET_CITY_NAME.callRdx("Miami"));
console.log(state.userReducer.address.city.name) // Miami

State Action

State Action when you need 3 step actions. (SEND, SUCCESS, FAILED)

import { StateAction, ACTION_STATES, createActionTypes, createReducer } from 'redux-action-namespace'

// declare intial state
const initialState = {
	updateState: null,  
        user:{ address:null }
};

// declare action classes
const actions = {
	UPDATE_USER : new UPDATE_USER("loading")
}
// create actions, namespace is required
const handlers = createActionTypes(actions, "USER_NS");

//export the reducer and add to the reducer state.
export const userReducer = createReducer(initialState, handlers);

export default actions;

// use the actions

state.dispatch(actions.UPDATE_USER.send());
console.log(state.userReducer.updateState) // expect be ACTION_STATES.SEND

const user = {
	id:1
}
state.dispatch(actions.UPDATE_USER.success(user)); 
console.log(state.userReducer.user.id) // expect be 1
console.log(state.userReducer.updateState) // expect be SUCCESS

// you can failed
const error = new Error("message")
state.dispatch(actions.UPDATE_USER.failed(null, error));
console.log(state.userReducer.updateState) // expect be FAILED
console.log(state.userReducer.error.message) // expect be message

API

createActionTypes Function

Description

The createActionTypes function is used to generate action types based on the provided actions and namespace.

Parameters

  • actions: An object containing the actions for which types need to be generated.
  • nameSpace: String namespace to be used in creating action types.

Returns

  • ActionObjectFunction : An object containing the generated action types.

Example

import { createActionTypes } from 'redux-action-namespace';

// Define actions and namespace
const actions = {
    action1: new StateAction(),
    action2: new DefaultAction()
};
const nameSpace = 'exampleNamespace';
// Generate action types
const actionTypes = createActionTypes(actions, nameSpace);

Usage

  1. Provide the actions and namespace to the createActionTypes function.
  2. Receive the object containing the generated action types based on the input actions and namespace.

createReducer Function

Description

The createReducer function is used to create a Redux reducer based on the provided initial state and a mapping of action types to corresponding reducer functions.

Parameters

  • initialState: The initial state of the reducer.
  • actionHandlers : An object mapping action types to corresponding reducer functions.

Returns

  • reducer (function): A Redux reducer function that handles state updates based on the dispatched actions.

Example

import { createReducer, createActionTypes } from 'redux-action-namespace';

// Define initial state
const initialState = {
    count: 0,
    loading: false
};


// Define actions and namespace
const actions = {
	action1: new StateAction(),
	action2: new DefaultAction()
};
const nameSpace = 'exampleNamespace';
// Generate action types
const actionTypes = createActionTypes(actions, nameSpace);

// Create the reducer
const reducer = createReducer(initialState, actionHandlers);

Usage

  1. Provide the initial state and action handlers to the createReducer function.
  2. Receive a Redux reducer function that handles state updates based on the dispatched actions.

This function simplifies the process of creating Redux reducers by allowing you to define action handlers in a clear and concise manner.

LINKS

Check the TODO example here

Unit test cases here

Notes

For modifications create a Pull Request.