Source: action.js

  1. import get from 'lodash/get';
  2. import deprecated from './deprecated';
  3. import actionCreatorAPI from './actionCreator';
  4. /**
  5. * This module is DEPRECATED and will be removed in future version.
  6. * it provide low level api to register and handle action in a CMF App.
  7. * @module react-cmf/lib/action
  8. * @see module:react-cmf/lib/Dispatcher
  9. */
  10. /**
  11. * get the global actions registered in the settings
  12. * @param {object} context
  13. * @return {object} actions with key === action id
  14. */
  15. function getActionsById(context) {
  16. const state = context.store.getState();
  17. return get(state, 'cmf.settings.actions', {});
  18. }
  19. /**
  20. * return actions registered for a given content type
  21. * @param {object} context
  22. * @param {String} contentType
  23. * @param {String} category
  24. * @return {Array} actions
  25. */
  26. function getContentTypeActions(context, contentType, category) {
  27. const state = context.store.getState();
  28. return get(state, `cmf.settings.contentTypes[${contentType}.actions[${category}]`, []);
  29. }
  30. /**
  31. * Return information available about this action
  32. * @param {object} context
  33. * @param {String} id
  34. * @return {object}
  35. */
  36. function getActionInfo(context, id) {
  37. const action = getActionsById(context)[id];
  38. if (!action) {
  39. throw new Error(`action not found id: ${id}`);
  40. }
  41. return { ...action };
  42. }
  43. /**
  44. * Return the action object ready to be dispatched
  45. * This is supposed to be used outside of content type
  46. * @param {object} context
  47. * @param {String|Object} action or the action
  48. * @param {object} event event which have trigger this action
  49. * @param {object} data data attached to the action
  50. */
  51. function getActionObject(context, action, event, data) {
  52. let actionInfo;
  53. if (typeof action === 'string') {
  54. actionInfo = getActionInfo(context, action);
  55. } else {
  56. actionInfo = action;
  57. }
  58. if (actionInfo.actionCreator) {
  59. const actionCreator = actionCreatorAPI.get(context, actionInfo.actionCreator);
  60. return actionCreator(event, data, {
  61. store: context.store,
  62. getState: context.store.getState,
  63. registry: context.registry,
  64. actionInfo,
  65. });
  66. }
  67. return { ...actionInfo.payload, event, data, context };
  68. }
  69. /**
  70. * return every props name that start with 'on'
  71. * @param {object} props react props
  72. * @return {Array} of string
  73. */
  74. function getOnProps(props) {
  75. return Object.keys(props).filter(
  76. name => ({}).hasOwnProperty.call(props, name) && /^on.+/.test(name),
  77. );
  78. }
  79. /**
  80. * create a map dispatchable action function expecting event object, props, and context information
  81. * merge this map with non event properties
  82. * @param {Function} dispatch the dispatch function
  83. * @param {object} props props object containing maybe on(event) with string
  84. * or action creator function]
  85. * @return {object} the connected object
  86. * @throws if an action is unknown in configuration, throw
  87. */
  88. function mapDispatchToProps(dispatch, props) {
  89. const resolvedActions = {};
  90. getOnProps(props).forEach(name => {
  91. resolvedActions[name] = (event, data, context) => {
  92. let action = props[name];
  93. if (typeof action === 'string') {
  94. action = getActionObject(context, action, event, data);
  95. }
  96. dispatch(action);
  97. };
  98. });
  99. return { ...props, ...resolvedActions };
  100. }
  101. const registerActionCreator = deprecated(
  102. (id, actionCreator, context) => actionCreatorAPI.register(id, actionCreator, context),
  103. 'stop use cmf.action.registerActionCreator. please use cmf.actionCreator.register',
  104. );
  105. const getActionCreatorFunction = deprecated(
  106. (context, id) => actionCreatorAPI.get(context, id),
  107. 'stop use cmf.action.getActionCreatorFunction. please use cmf.actionCreator.get',
  108. );
  109. export default {
  110. getActionsById,
  111. getActionCreatorFunction,
  112. getActionInfo,
  113. getActionObject,
  114. getContentTypeActions,
  115. getOnProps,
  116. mapDispatchToProps,
  117. registerActionCreator,
  118. };