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

simple-redux-reducer

v1.0.2

Published

Simple redux reducer is a dead simple facade for CRUD operations.

Downloads

6

Readme

Simple-Redux-Reducer

Simple redux reducer is a dead simple facade for CRUD operations.

Usually the boilerplate that we create when we are writing new reducers is something like:

const initialState = { /* some state */ }
export default Products(state = initialState, action) {
  switch(action.type) {
    case 'PRODUCTS_FETCH_START':
      return {
        ...state,
        error: null,
        fetching: true,
      }
    case 'PRODUCTS_FETCH_SUCCEEDED':
      return {
        ...state,
        error: null,
        data: action.payload,
      }
    case 'PRODUCTS_FETCH_FAILED':
      return {
        ...state,
        error: action.error,
      }
    case 'PRODUCTS_FETCH_FINALLY':
      return {
        ...state,
        fetching: false,
      }
    ... AND SO ON...
  }
}

this library is intended to reduce the boilerplate, using composition over inheritance. So the above code is rewritten into this:

const { fetchStart, fetchSucceeded, fetchFailed, fetchFinally } from 'simple-redux-reducer'

const initialState = { /* some state */ }

export default Products(state = initialState, action) {
  switch(action.type) {
    case 'PRODUCTS_FETCH_START':
      return fetchStart(state, action)
    case 'PRODUCTS_FETCH_SUCCEEDED':
      return fetchSucceeded(state, action)
    case 'PRODUCTS_FETCH_FAILED':
      return fetchFailed(state, action)
    case 'PRODUCTS_FETCH_FINALLY':
      return fetchFinally(state, action)
    ... AND SO ON...
  }
}

This library have reducer handlers for the following events:

  • fetchStart
  • fetchSucceeded
  • fetchFailed
  • fetchFinally
  • createStart
  • createSucceeded
  • createFailed
  • updateStart
  • updateSucceeded
  • updateFailed
  • deleteStart
  • deleteSucceeded
  • deleteFailed

The State is modeled by the following,

for the example above, the state would look like this:

{ products: { data: [{ ...product, loading, deleting, error, provisory_id }], error: String, loading: Boolean, } }

The reducers, for fetching a list, will try to populate the data attribute, if it can't, it will populate the error attribute with a string, the string will depend on what your server did answer. Loading will be set to true when you handle fetchStart, and will be turned off when you call fetchFinally.

For creating an entity, you must call createStart:

createStart(state, { payload: { ...product, provisory_id: 'a unique provisory id' }})

The state will be updated with the new product in the list data, and with a loading attribute set to true. After you created the atribute, you must call updateFailed or updateSucceeded with an object containing the id of the newly created item and the provisory_id. This strategy works when you are generating the id of the entity on the server, so this can work with mongodb or other database like mysql that generates the id on the client.

createSucceeded(state, {_id: 'new id', provisory_id: 'a unique provisory id' })

If you don't know how to make a provisory id or you need a lightweight solution you can try this:

const makeProvisoryId = () => Math.random().toString(36).substring(2)

this README is a WIP