# @opensrp/reducer-factory

> Abstraction for creating other dux modules

Latest version **0.0.13** (published 2021-02-24) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @opensrp/reducer-factory
pnpm add @opensrp/reducer-factory
yarn add @opensrp/reducer-factory
bun add @opensrp/reducer-factory
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.13 |
| Published | 2021-02-24 |
| First published | 2020-06-11 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 29.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Ona Engineering |
| Maintainers | opensrp-admin |

## Links

- npm: https://www.npmjs.com/package/@opensrp/reducer-factory
- Repository: https://github.com/OpenSRP/web
- Homepage: https://github.com/OpenSRP/web#readme
- Issues: https://github.com/OpenSRP/web/issues
- npm.io page: https://npm.io/package/@opensrp/reducer-factory

## Dependencies (4)

- [redux](https://npm.io/package/redux.md) ^4.0.5
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [@onaio/utils](https://npm.io/package/@onaio/utils.md) ^0.0.1
- [seamless-immutable](https://npm.io/package/seamless-immutable.md) ^7.1.4

## Recent versions

- 0.0.13 (latest) — 2021-02-24
- 0.0.12 — 2021-02-18
- 0.0.11 — 2020-11-12
- 0.0.10 — 2020-11-05
- 0.0.9 — 2020-11-02
- 0.0.7 — 2020-10-29
- 0.0.6 — 2020-10-28
- 0.0.5 — 2020-10-27
- 0.0.3 — 2020-06-24
- 0.0.1 — 2020-06-11

## README

# Base Dux Module

## What am i

Am an abstraction (more like a template), that you can use to create other [dux modules](https://github.com/erikras/ducks-modular-redux).

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

```node
yarn add @opensrp/reducer-factory
```

## Code example

```typescript
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

```ts
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

```ts
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

---
_Source: https://npm.io/package/@opensrp/reducer-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
