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

@ofirgeller/redux-reducer-builder

v3.1.0-a

Published

Util library to reduce boilerplate and common mistakes when working with redux

Downloads

319

Readme

Redux-Reducer-Builder

Util class to make creating reducers less verbose and error prone, with typescript support.

Install

yarn add @ofirgller/redux-reducer-builder

Usage

This library support typescript but can also be used by Javascript project, simply avoid adding the type annotations.

import { ReducerBuilder, opState, OpState } from '@ofirgeller/redux-reducer-builder';

type Todo = {
    id: number;
    descriptions: string;
    done: boolean;
}

type TodoReducerState = {
    items: Todo[];
    saveOp: OpState
}

const initState: TodoReducerState = {
    items: [],
    saveOp: opState
}

/**Create a reducer with the name "todo", the state it controls is of type TodoReducerState and the initial state is passed as the second parameter */
const builder = new ReducerBuilder<TodoReducerState>('todo', initState);

/** You can set the initial state separately or in addition, the state is shallow merged with later calls overriding previous ones  */
builder.withInitState({ items: [{ descriptions: 'initial todo', done: false, id: 0 }] });
/** Type checking will prevent you from passing in the wrong type*/
/// Will not compile: builder.withInitState({ name: 'hello' });

/**Add a simple action, the first parameter is the name, it will get prefixed with the name of the reducer, in this case becoming:
 * "TODO__MARK_ALL_DONE". */
export const markAllDone = builder.withAction('MARK_ALL_DONE', (state, action) => {
    return state;
});

/// Later you can import 'markAllDone' and dispatch the action like so: dispatch(markAllDone()); 
/// what we are doing is creating the action handler and the action creator at the same time.

/**If the action has a payload use this method */
export const removeTodo = builder.withActionOfT<Todo>('REMOVE', (state, action) => {
    return { ...state, items: state.items.filter(i => i.id !== action.payload.id) };
});

/// removeTodo will be dispatched like so: dispatch(removeTodo(todoThatShouldBeRemoved)); 

/**If the action name is used by another reducer or is dispatched by a library, you can control the action name by passing in the 3rd parameter.
 * so assuming you want to handle an action with the type "AFTER_NAVIGATION_ACTION" you can do this:
*/

/// In your code you will import the action name from a shared file, here we pretend it is available in the current scope.
declare const AFTER_NAVIGATION_ACTION = "@ROUTER_LIBRARY/AFTER_NAVIGATION";

/**This time we don't export the action creator, since we do not intend on dispatching ourselves */
builder.withAction(AFTER_NAVIGATION_ACTION, (state, _action) => {
    return state;
}, true);

/**op is short for operation, often we want to perform some async operation and keep the state of the operation in redux, for example so we can show
 * a progress indicator, disable the button until the operation is complete etc.  
   */
export const saveTodosOp = builder.withOp('saveOp')

/// saveTodosOp contains 4 action creators, so later we can call saveTodosOp.start, saveTodosOp.done and saveTodosOp.error from
/// our async operation, you can also call startWithParams and pass the params in which is useful in case you would like to retry the op later
/// or include the params when logging.


/**Fetch is an op that is also expected to set some resource into the state, in this case we will be fetching the todos from the server */
export const fetchTodos = builder.withFetch<Todo[]>('items');

/// instead of having a 'done' action we have a 'result' action which expects the payload to be of the type specified, in this case an 
/// array of todos. so after the request is done and we have the todos we will call dispatch(fetchTodos.result(data)) and the action handler will set the state

/**If we need special logic to run on done or result we can pass a handler as a parameter to the 'withOp'/'withFetch' methods */
export const altFetchTodos = builder.withFetch<Todo[]>('items', (state, action) => {
    const mappedTodos = action.payload.map(todo => ({ ...todo, done: false }));
    return { ...state, items: mappedTodos };
});

/// you can use lower level functions that the reducer builder uses like "createAction" and "createActionWithNoPayload" if you need to. 

Contributing

PRs accepted, could probably improve type inference if anyone wants to have a go at it.

License MIT © Ofir Geller