Skip to content

# v0.1.5 process #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

# 0.1.5
Change selector payload type of data, now it's *Immutable JSON or unndefined* please see [test-source](https://github.com/edtoken/redux-tide/blob/master/test/selector.spec.js#L48)
Add query builder result to action store and selector response `args`
Implement `action.remove`
Implement `action.recall`
Improve documentation
Improve examples, add new example
Add code sandbox examples
Add new badges

# 0.1.4

# 0.1.3
Fix critical bug in method `Action.empty`
small refactoring action.js, selector.js files
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ ActionCreator + ActionSelector, reducers are created automatically
[coverage-report](https://edtoken.github.io/redux-tide/coverage/lcov-report/index.html)


[![Join the chat at https://gitter.im/practice-feature/redux-tide](https://badges.gitter.im/practice-feature/redux-tide.svg)](https://gitter.im/practice-feature/redux-tide?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Join the chat at https://gitter.im/practice-feature/redux-tide](https://badges.gitter.im/practice-feature/redux-tide.svg)](https://gitter.im/practice-feature/redux-tide?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![npm version](https://badge.fury.io/js/redux-tide.svg)](https://badge.fury.io/js/redux-tide)
[![Build Status](https://api.travis-ci.org/edtoken/redux-tide.svg?branch=master)](https://travis-ci.org/edtoken/redux-tide)
[![Maintainability](https://api.codeclimate.com/v1/badges/5952e9edfa038e49658f/maintainability)](https://codeclimate.com/github/edtoken/redux-tide/maintainability)
[![npm downloads](https://img.shields.io/npm/dm/redux-tide.svg?style=flat-square)](https://www.npmjs.com/package/redux-tide)
[![Coverage Status](https://coveralls.io/repos/github/edtoken/redux-tide/badge.svg?branch=master)](https://coveralls.io/github/edtoken/redux-tide?branch=master)
[![Inline docs](https://inch-ci.org/github/edtoken/redux-tide.svg?branch=master)](https://inch-ci.org/github/edtoken/redux-tide)
[![Inline docs](https://inch-ci.org/github/edtoken/redux-tide.svg?branch=master)](https://inch-ci.org/github/edtoken/redux-tide)
[![dependencies Status](https://david-dm.org/edtoken/redux-tide/status.svg)](https://david-dm.org/edtoken/redux-tide)
[![devDependencies Status](https://david-dm.org/edtoken/redux-tide/dev-status.svg)](https://david-dm.org/edtoken/redux-tide?type=dev)
[![HitCount](http://hits.dwyl.com/edtoken/redux-tide.svg)](http://hits.dwyl.com/edtoken/redux-tide)

[![NPM](https://nodei.co/npm/redux-tide.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/redux-tide/)
Expand Down Expand Up @@ -96,7 +98,7 @@ npm install redux-thunk --save
------
### Discussion
You can connect to [Gitter chat room](https://gitter.im/practice-feature/redux-tide)
[![Join the chat at https://gitter.im/practice-feature/redux-tide](https://badges.gitter.im/practice-feature/redux-tide.svg)](https://gitter.im/practice-feature/redux-tide?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Join the chat at https://gitter.im/practice-feature/redux-tide](https://badges.gitter.im/practice-feature/redux-tide.svg)](https://gitter.im/practice-feature/redux-tide?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

### Usage
1. You might install library
Expand Down Expand Up @@ -227,12 +229,22 @@ export const createNewPost = createAction(
]
)

export const getPostById = createAction(
postsSchema,
get,
(postId) => {
return (dispatch, getState)=>{
// return Promise (axios call or other)
}
}
)

// basic redux action can be use
export const openEditPost = (postId) => {
return {
type:OPEN_EDIT,
postId
}
}
}

```
Expand Down Expand Up @@ -324,6 +336,7 @@ import {getActionData} from 'redux-tide';
```
{String} actionId - your action id
{*} sourceResult - your source response from server (not mapped response)
{*} args - your args from query builder action method
{String} status - pending|success|error
{Number} time - timestamp of action
{Boolean} hasError - has error or not
Expand Down
74 changes: 38 additions & 36 deletions lib/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ var makeActionHandler = function makeActionHandler(Action, actionId, parentActio
*
* @param {String} error - action error text
* @param {Object|Array} payloadSource - response from action result
* @param {Object|Array} args - arguments from queryBuilder
* @returns {Object} - action dispatch body
*/
return function (error, payloadSource, sourceResult) {
return function (error, payloadSource, sourceResult, args) {
if (status === 'success' && payloadSource === undefined) {
error = 'Empty payload';
status = 'error';
Expand Down Expand Up @@ -117,10 +118,11 @@ var makeActionHandler = function makeActionHandler(Action, actionId, parentActio
* payloadSource: Object|Array
* }}
*/
return Object.freeze({
return {
time: new Date().getTime(),
type: '' + actionId,
prefix: _config.ACTION_TYPE_PREFIX,
args: args,
actionId: actionId,
parentActionId: parentActionId,
status: status,
Expand All @@ -134,22 +136,22 @@ var makeActionHandler = function makeActionHandler(Action, actionId, parentActio
sourceResult: sourceResult,
payload: payload,
payloadSource: payloadSource
});
};
};
};

var makeResultCallback = function makeResultCallback(responseMapper, success, error) {
return function (dispatch, getState, err, result) {
return function (dispatch, getState, err, result, args) {
try {
var errorMessage = (0, _helper.parseError)(err);

if (errorMessage) {
return dispatch(error(errorMessage, undefined));
return dispatch(error(errorMessage, undefined, undefined, args));
}

dispatch(success(undefined, responseMapper(result), result));
dispatch(success(undefined, responseMapper(result), result, args));
} catch (e) {
dispatch(error(String('' + (e.message || e)), undefined));
dispatch(error(String('' + (e.message || e)), undefined, undefined, args));
throw e;
}
};
Expand Down Expand Up @@ -183,29 +185,29 @@ var makeCallActionMethod = function makeCallActionMethod(resultCallBack) {

if (actionResult instanceof Promise) {
return actionResult.then(function (resp) {
return resultCallBack(dispatch, getState, false, resp);
return resultCallBack(dispatch, getState, false, resp, args);
}).catch(function (err) {
return resultCallBack(dispatch, getState, err, undefined);
return resultCallBack(dispatch, getState, err, undefined, args);
});
}

if (typeof actionResult === 'function') {
actionResult = actionResult.call(undefined, dispatch, getState);

if (!actionResult) {
return resultCallBack(dispatch, getState, undefined, undefined);
return resultCallBack(dispatch, getState, undefined, undefined, args);
}

if (actionResult instanceof Promise) {
return actionResult.then(function (resp) {
return resultCallBack(dispatch, getState, false, resp);
return resultCallBack(dispatch, getState, false, resp, args);
}).catch(function (err) {
return resultCallBack(dispatch, getState, err, undefined);
return resultCallBack(dispatch, getState, err, undefined, args);
});
}
}

return resultCallBack(dispatch, getState, false, actionResult);
return resultCallBack(dispatch, getState, false, actionResult, args);
};
};

Expand Down Expand Up @@ -377,7 +379,7 @@ var makeAction = function makeAction(actionId, parentActionId, actionSchema, act
*
* @returns {Undefined} - returns None, only clear action data
*/
this.action.empty = function () {
this.action.reset = function () {
return function (dispatch, getState) {
dispatch({
time: new Date().getTime(),
Expand All @@ -389,28 +391,28 @@ var makeAction = function makeAction(actionId, parentActionId, actionSchema, act
};
};

// /**
// * Clean entity from entity reducer
// *
// * @memberOf action.makeAction.Action
// * @type {Function}
// *
// * @example
// * store.dispatch(userLoginAction.clean())
// *
// * @returns {Undefined} - returns None, only clear entity data
// */
// this.action.clean = () => {
// return (dispatch, getState) => {
// dispatch({
// time: new Date().getTime(),
// type: ACTION_CLEAN_TYPE_NAME,
// prefix: ACTION_TYPE_PREFIX,
// actionId: this.actionId,
// actionSchema: this.schema
// })
// }
// }
/**
* Remove action entity id or ids from entities reducer
*
* @memberOf action.makeAction.Action
* @type {Function}
*
* @example
* store.dispatch(userLoginAction.remove())
*
* @returns {Undefined} - returns None, only remove entity items
*/
this.action.remove = function () {
return function (dispatch, getState) {
dispatch({
time: new Date().getTime(),
type: _config.ACTION_REMOVE_TYPE_NAME,
prefix: _config.ACTION_TYPE_PREFIX,
actionId: _this.actionId,
actionSchema: _this.schema
});
};
};

return this.action;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ var ENTITIES_REDUCER_NAME = exports.ENTITIES_REDUCER_NAME = ACTION_TYPE_PREFIX +
var ACTION_EMPTY_TYPE_NAME = exports.ACTION_EMPTY_TYPE_NAME = ACTION_TYPE_PREFIX + '-empty';

/**
* Action type name for clear entity data from entity reducer
* Action type name for delete entities from state
*
* @memberOf config
* @const
* @type {String}
*/
var ACTION_CLEAN_TYPE_NAME = exports.ACTION_CLEAN_TYPE_NAME = ACTION_TYPE_PREFIX + '-clean';
var ACTION_REMOVE_TYPE_NAME = exports.ACTION_REMOVE_TYPE_NAME = ACTION_TYPE_PREFIX + '-remove';

/**
* replaced default response mapper to callback
Expand Down
8 changes: 5 additions & 3 deletions lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var makeActionsReducer = function makeActionsReducer(defaultActionsState) {
return state;
}

var status = action.status,
var args = action.args,
status = action.status,
time = action.time,
actionId = action.actionId,
payload = action.payload,
Expand All @@ -52,8 +53,8 @@ var makeActionsReducer = function makeActionsReducer(defaultActionsState) {

var entityKey = actionSchema.key;

// action.clear
if (action.type === _config.ACTION_EMPTY_TYPE_NAME) {
// action.clear || action.remove
if (action.type === _config.ACTION_EMPTY_TYPE_NAME || action.type === _config.ACTION_REMOVE_TYPE_NAME) {
return state.set(actionId, (0, _immutable.fromJS)(Object.assign({ entityKey: entityKey }, actionDefaultData, { time: time })));
}

Expand All @@ -68,6 +69,7 @@ var makeActionsReducer = function makeActionsReducer(defaultActionsState) {
}

actionState = actionState.merge({
args: args,
status: status,
time: time,
hasError: hasError,
Expand Down
12 changes: 8 additions & 4 deletions lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ var getPayloadIds = function getPayloadIds(dataKey, isArray, actionState, stateK
};

var makeActionDenormalizedPayload = function makeActionDenormalizedPayload(isArray, payloadIds, schema, entities) {
// return empty immutable object
if (!payloadIds) {
return undefined;
}

var result = (0, _helper.denormalize)(payloadIds, [schema], entities).filter(function (v) {
return v;
}).map(function (item) {
return item.toJS();
});

return isArray ? result : result[0];
};

var makeActionDenormalizedPayloads = function makeActionDenormalizedPayloads(isFetching, actionSchema, entities, payloadIsArray, actionDataKey, entityState, actionState) {
if (!actionDataKey) {
return {};
return undefined;
}

if (!entityState) {
Expand Down Expand Up @@ -95,6 +95,7 @@ var _makeGetActionData = function _makeGetActionData(action, actionId, entityNam

return Object.assign(makeDefaultActionData(), {
actionId: actionId,
args: actionState.get('args'),
sourceResult: actionState.get('sourceResult'),
status: actionState.get('status'),
time: actionState.get('time'),
Expand Down Expand Up @@ -133,7 +134,10 @@ var getMergedActionsData = exports.getMergedActionsData = function getMergedActi
});

return sortedByUpate.reduce(function (memo, item) {
return Object.assign(memo, item);
return Object.assign(memo, item, {
payload: memo.payload ? memo.payload.merge(item.payload) : item.payload,
prevPayload: memo.prevPayload ? memo.prevPayload.merge(item.prevPayload) : item.prevPayload
});
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "redux-tide",
"description": "tiny factory for redux crud normalize",
"version": "0.1.4",
"version": "0.1.5",
"main": "./lib/index.js",
"files": [
"lib",
Expand Down Expand Up @@ -82,7 +82,7 @@
"jsdoc": "^3.5.5",
"json-server": "^0.12.1",
"minami": "^1.2.3",
"mocha": "^4.1.0",
"mocha": "^5.0.0",
"prettier": "^1.9.2",
"raw-loader": "^0.5.1",
"react": "^16.2.0",
Expand Down
Loading