Skip to content

Commit

Permalink
New vesion 2.0.0, use lodash.merge instead of Object.assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Petrov committed Mar 23, 2017
1 parent dc9673c commit f23cf8e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 34 deletions.
63 changes: 33 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,62 @@ npm install --save redux-entities

## Usage

WIP

### Use with `entitiesReducer`

```js
import { combineReducers } from 'redux';
import { entitiesReducer } from 'redux-entities';
import omit from 'lodash/omit';
import { merge, omit } from 'lodash';

function contacts(state = {}, action) {
const { type, payload } = action;

const { type, /* , payload */ meta } = action;

switch (type) {
switch (type) {

case UPDATE_CONTACT:
case REMOVE_CONTACT:
return Object.assign({}, state, { [payload.id]: {
...state[payload.id],
isPending: true
} });
case UPDATE_CONTACT:
case REMOVE_CONTACT:
return merge({}, state, { [payload.id]: {
...state[payload.id],
isPending: true
}});

case UPDATE_CONTACT_SUCCESS:
return Object.assign({}, state, { [payload.id]: {
...state[payload.id],
isPending: false
} });
case UPDATE_CONTACT_SUCCESS:
return merge({}, state, { [payload.id]: {
...state[payload.id],
isPending: false
}});

// return merge({}, state, { [meta.id]: { isPending: false } });
case REMOVE_CONTACT_SUCCESS:
return omit(state, meta.id);

case REMOVE_CONTACT_SUCCESS:
return omit(state, meta.id);

default:
return state;
}
default:
return state;
}
}

export default combineReducers({
contacts: entitiesReducer(contacts, 'contacts')
contacts: entitiesReducer(contacts, 'contacts')
});

```

### Use with `combineEntitiesReducers`

```js
import { combineEntitiesReducers } from 'redux-entities';
import { contacts, groups, images, notes } from './entities'
import { contacts, groups, images, notes } from './entities';

export default combineEntitiesReducers({
contacts,
groups,
images,
notes
contacts,
groups,
images,
notes
});

```

## Immutable

If you want to use `Immutable` with `Redux` please check out this version of the library: [redux-entities-immutable](https://github.com/beautyfree/redux-entities-immutable)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-entities",
"version": "1.0.0",
"version": "2.0.0",
"description": "Higher-order reducer for store entities received from normalizr and makes it easy to handle them.",
"main": "lib/index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isFunction from 'lodash/isFunction'
import mapValues from 'lodash/mapValues'
import merge from 'lodash/merge'
import get from 'lodash/get'

function selectEntities(action, name) {
Expand All @@ -9,14 +10,18 @@ function selectEntities(action, name) {
}
}

export function actionless(state = {}) {
return state
}

export function entitiesReducer(reducer, entitiesName) {
return (state, action) => {
let newState = state;
const entities = isFunction(entitiesName) ?
entitiesName(action) : selectEntities(action, entitiesName);

if (entities) {
newState = Object.assign({}, newState, entities);
newState = merge({}, newState, entities);
}

return reducer(newState, action);
Expand Down
55 changes: 53 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
import { expect } from 'chai';
import { entitiesReducer, combineEntitiesReducers } from '../src';
import { merge } from 'lodash';
import { entitiesReducer, combineEntitiesReducers, actionless } from '../src';

describe('redux-entities', () => {
const normalizedObject = {
Expand Down Expand Up @@ -28,6 +29,21 @@ describe('redux-entities', () => {
}
}

const normalizedObjectWithNewFields = {
entities: {
contacts: {
1: {
id: 1,
phone: '54321'
},
2: {
id: 2,
phone: '12345'
}
},
}
}

const fillEntitiesAction = {
type: 'FILL_ENTITIES',
payload: normalizedObject
Expand All @@ -40,6 +56,13 @@ describe('redux-entities', () => {
}
}

const fillEntitiesWithNewFieldsAction = {
type: 'FILL_ENTITIES',
payload: {
nested: normalizedObjectWithNewFields
}
}

const updateContactAction = {
type: 'UPDATE_CONTACT',
payload: {
Expand Down Expand Up @@ -85,7 +108,21 @@ describe('redux-entities', () => {

const state = reducer({}, fillEntitiesNestedAction)

expect(state).to.deep.equal(normalizedObject.entities.contacts)
expect(state).to.deep.equal(normalizedObject.entities.contacts)
})

it('can merge entities fields', () => {
const reducer = entitiesReducer(
contactsReducer,
(action) => action.payload.nested.entities.contacts
)

const state = reducer({}, fillEntitiesNestedAction)
const updatedState = reducer(state, fillEntitiesWithNewFieldsAction)

expect(updatedState).to.deep.equal(
merge({}, normalizedObject, normalizedObjectWithNewFields).entities.contacts
)
})

it('can update specific entitie', () => {
Expand Down Expand Up @@ -125,4 +162,18 @@ describe('redux-entities', () => {
expect(updatedState.contacts[1]).to.deep.equal({ id: 1, name: 'Andrey' })
})
})

describe('actionless', () => {
it('should return empty object', () => {
const state = actionless()

expect(state).to.deep.equal({})
})

it('should return passed object', () => {
const state = actionless(normalizedObject.entities.contacts)

expect(state).to.deep.equal(normalizedObject.entities.contacts)
})
})
})

0 comments on commit f23cf8e

Please sign in to comment.