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

@opensrp/reducer-factory

v0.0.13

Published

Abstraction for creating other dux modules

Downloads

32

Readme

Base Dux Module

What am i

Am an abstraction (more like a template), that you can use to create other dux modules.

I think the biggest win with me, is that there is reduced boilerplate when it comes to creating dux modules.

Currently , the only opinionated part about me is how i structure the stored data, basically it requires that my action creators are given data as list of objects, where each object bears a property akin to a primary_key i.e. this one single field should have unique values that are of type string| number.

Installation

yarn add @opensrp/reducer-factory

Code example

import {
  fetchActionCreatorFactory,
  removeActionCreatorFactory,
  setTotalRecordsFactory,
  reducerFactory,
  getItemsByIdFactory,
  getItemsArrayFactory,
  getItemByIdFactory,
  getTotalRecordsFactory,
  BaseDux,
} from '@opensrp/reducer-factory';

/** reducer name for the Item module */
export const reducerName = '<preferred reducer name>';

/** Item Reducer */
const reducer = reducerFactory<DataType>(reducerName);

// action
/** actionCreator returns action to to add Item records to store */
export const fetchItem = fetchActionCreatorFactory<DataType>(reducerName, 'uniqueKey');
export const removeItemAction = removeActionCreatorFactory(reducerName);
export const setTotalItemRecords = setTotalRecordsFactory(reducerName);

// selectors
export const getAllItemById = getItemsByIdFactory<DataType>(reducerName);
export const getItemById = getItemByIdFactory<DataType>(reducerName);
export const getAllItemArray = getItemsArrayFactory<DataType>(reducerName);
export const getTotalItemRecords = getTotalRecordsFactory(reducerName);

export default reducer;

The default action types are

FETCHED = 'opensrp/reducer/objects/FETCHED';
REMOVE = 'opensrp/reducer/objects/REMOVE';
SET_TOTAL_RECORDS = 'opensrp/reducer/objects/SET_TOTAL_RECORDS';

If you would like to override the default values for the action types, pass the values as arguments when creating the reducer

const customfetchedActionType = 'locations/location-units/LOCATION_UNITS_FETCHED';
const customRemoveActionType = 'locations/location-units/REMOVE_LOCATION_UNITS';
const customSetTotalRecordsActionType = 'locations/location-units/SET_TOTAL_LOCATION_UNITS';

const reducer = reducerFactory<DataType>(
  reducerName,
  customfetchedActionType,
  customRemoveActionType,
  customSetTotalRecordsActionType
);

What am i provisioning

Glad you asked; The above example should have given you some clue, but am all to happy to summarize this for you here:

reducerFactory - this a factory method that returns a function that can be used as the reducer. currently it supports 3 action types: - adding items - removing items - setting total records

for action creators

fetchActionCreatorFactory - Use this to create action creators that you can use to create actions that when dispatched will add the data to a slice of the store. The argument is a key - which must be unique in the data set,

removeActionCreatorFactory - Use this to create action creators that return actions to remove all items from a slice of the store

setTotalRecordsFactory - This will create an action creator that adds the total number of records to specific slice of the store

for the selectors

getItemsByIdFactory which returns a selector that gets all items from a store slice as an object

getItemByIdFactory which returns a selector that gets a single item from a store slice as an object

getItemsArrayFactory which returns a selector that gets all objects from a store slice as an array

getTotalRecordsFactory which returns a selector that gets the total number of records in a store slice