react-cmf
Advanced tools
Comparing version 0.88.0 to 0.89.0
@@ -9,3 +9,2 @@ 'use strict'; | ||
exports.initState = initState; | ||
exports.applyCallback = applyCallback; | ||
exports.getStateAccessors = getStateAccessors; | ||
@@ -37,6 +36,12 @@ | ||
function applyCallback(callback, name, id) { | ||
return function (dispatch, getState) { | ||
var newState = callback(getStateProps(getState(), name, id)); | ||
dispatch(_actions2.default.componentsActions.mergeComponentState(name, id, newState)); | ||
function getAction(_ref) { | ||
var name = _ref.name, | ||
id = _ref.id, | ||
operation = _ref.operation, | ||
componentState = _ref.componentState; | ||
return { | ||
id: id, | ||
type: name + '.' + operation, | ||
cmf: { componentState: componentState } | ||
}; | ||
@@ -46,2 +51,11 @@ } | ||
function getStateAccessors(dispatch, name, id, DEFAULT_STATE) { | ||
var dispatchAction = function dispatchAction(operation, componentState) { | ||
dispatch(getAction({ | ||
id: id, | ||
name: name, | ||
componentState: componentState, | ||
operation: operation | ||
})); | ||
}; | ||
var accessors = { | ||
@@ -52,7 +66,10 @@ setState: function setState(state) { | ||
} | ||
if (typeof state === 'function') { | ||
dispatch(applyCallback(state, name, id)); | ||
} else { | ||
dispatch(_actions2.default.componentsActions.mergeComponentState(name, id, state)); | ||
} | ||
dispatch(function (_, getState) { | ||
var newState = state; | ||
if (typeof newState === 'function') { | ||
newState = state(getStateProps(getState(), name, id)); | ||
} | ||
var componentState = _actions2.default.componentsActions.mergeComponentState(name, id, newState); | ||
dispatchAction('setState', componentState); | ||
}); | ||
}, | ||
@@ -62,3 +79,4 @@ initState: function initState(initialState) { | ||
var state = DEFAULT_STATE.merge(initialState); | ||
dispatch(_actions2.default.componentsActions.addComponentState(name, id, state)); | ||
var componentState = _actions2.default.componentsActions.addComponentState(name, id, state); | ||
dispatchAction('initState', componentState); | ||
} | ||
@@ -68,3 +86,4 @@ }, | ||
if (DEFAULT_STATE) { | ||
dispatch(_actions2.default.componentsActions.removeComponentState(name, id)); | ||
var componentState = _actions2.default.componentsActions.removeComponentState(name, id); | ||
dispatchAction('deleteState', componentState); | ||
} | ||
@@ -71,0 +90,0 @@ } |
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _get = require('lodash/get'); | ||
var _get2 = _interopRequireDefault(_get); | ||
var _immutable = require('immutable'); | ||
@@ -24,5 +28,7 @@ | ||
var defaultState = exports.defaultState = new _immutable.Map(); /** | ||
* @module react-cmf/lib/reducers/componentsReducers | ||
*/ | ||
/** | ||
* @module react-cmf/lib/reducers/componentsReducers | ||
*/ | ||
var defaultState = exports.defaultState = new _immutable.Map(); | ||
function componentAlreadyExists(action) { | ||
@@ -78,3 +84,9 @@ var buff = 'Can\'t set up your component ' + action.componentName + ' '; | ||
default: | ||
return state; | ||
{ | ||
var subAction = (0, _get2.default)(action, 'cmf.componentState'); | ||
if (subAction) { | ||
return componentsReducers(state, subAction); | ||
} | ||
return state; | ||
} | ||
} | ||
@@ -81,0 +93,0 @@ } |
@@ -103,3 +103,3 @@ { | ||
}, | ||
"version": "0.88.0", | ||
"version": "0.89.0", | ||
"dependencies": { | ||
@@ -106,0 +106,0 @@ "hoist-non-react-statics": "^1.2.0" |
@@ -17,6 +17,7 @@ import { PropTypes } from 'react'; | ||
export function applyCallback(callback, name, id) { | ||
return (dispatch, getState) => { | ||
const newState = callback(getStateProps(getState(), name, id)); | ||
dispatch(actions.componentsActions.mergeComponentState(name, id, newState)); | ||
function getAction({ name, id, operation, componentState }) { | ||
return { | ||
id, | ||
type: `${name}.${operation}`, | ||
cmf: { componentState }, | ||
}; | ||
@@ -26,2 +27,11 @@ } | ||
export function getStateAccessors(dispatch, name, id, DEFAULT_STATE) { | ||
const dispatchAction = (operation, componentState) => { | ||
dispatch(getAction({ | ||
id, | ||
name, | ||
componentState, | ||
operation, | ||
})); | ||
}; | ||
const accessors = { | ||
@@ -35,7 +45,10 @@ setState(state) { | ||
} | ||
if (typeof state === 'function') { | ||
dispatch(applyCallback(state, name, id)); | ||
} else { | ||
dispatch(actions.componentsActions.mergeComponentState(name, id, state)); | ||
} | ||
dispatch((_, getState) => { | ||
let newState = state; | ||
if (typeof newState === 'function') { | ||
newState = state(getStateProps(getState(), name, id)); | ||
} | ||
const componentState = actions.componentsActions.mergeComponentState(name, id, newState); | ||
dispatchAction('setState', componentState); | ||
}); | ||
}, | ||
@@ -45,3 +58,4 @@ initState(initialState) { | ||
const state = DEFAULT_STATE.merge(initialState); | ||
dispatch(actions.componentsActions.addComponentState(name, id, state)); | ||
const componentState = actions.componentsActions.addComponentState(name, id, state); | ||
dispatchAction('initState', componentState); | ||
} | ||
@@ -51,3 +65,4 @@ }, | ||
if (DEFAULT_STATE) { | ||
dispatch(actions.componentsActions.removeComponentState(name, id)); | ||
const componentState = actions.componentsActions.removeComponentState(name, id); | ||
dispatchAction('deleteState', componentState); | ||
} | ||
@@ -54,0 +69,0 @@ }, |
/** | ||
* @module react-cmf/lib/reducers/componentsReducers | ||
*/ | ||
import get from 'lodash/get'; | ||
import { Map, fromJS } from 'immutable'; | ||
@@ -74,7 +75,12 @@ import invariant from 'invariant'; | ||
return state.deleteIn([action.componentName, action.key]); | ||
default: | ||
default: { | ||
const subAction = get(action, 'cmf.componentState'); | ||
if (subAction) { | ||
return componentsReducers(state, subAction); | ||
} | ||
return state; | ||
} | ||
} | ||
} | ||
export default componentsReducers; |
323995
4927