Source: componentState.js

  1. import PropTypes from 'prop-types';
  2. import Immutable from 'immutable';
  3. import actions from './actions';
  4. /**
  5. * This module provide props.setState and props.state into
  6. * cmfConnected component. It exposes CMF propTypes
  7. * @module react-cmf/lib/componentState
  8. * @see module:react-cmf/lib/cmfConnect
  9. * @example
  10. import { cmfConnect, componentState } from '@talend/react-cmf';
  11. class MyComponent extends React.Component {
  12. static propTypes = {
  13. ...componentState.propTypes,
  14. };
  15. render() {
  16. // ...
  17. }
  18. }
  19. export default cmfConnect({})(MyComponent);
  20. */
  21. export function getStateProps(state, name, id = 'default') {
  22. return {
  23. state: state.cmf.components.getIn([name, id]),
  24. };
  25. }
  26. export function initState(props) {
  27. if (!props.state && props.initState) {
  28. props.initState(props.initialState);
  29. }
  30. }
  31. function getAction({ name, id, operation, componentState }) {
  32. return {
  33. id,
  34. type: `${name}.${operation}`,
  35. cmf: { componentState },
  36. };
  37. }
  38. export function getStateAccessors(dispatch, name, id, DEFAULT_STATE) {
  39. const dispatchAction = (operation, componentState) => {
  40. dispatch(
  41. getAction({
  42. id,
  43. name,
  44. componentState,
  45. operation,
  46. }),
  47. );
  48. };
  49. const accessors = {
  50. setState(state) {
  51. dispatch((_, getState) => {
  52. let newState = state;
  53. if (typeof newState === 'function') {
  54. newState = state(getStateProps(getState(), name, id));
  55. }
  56. const componentState = actions.components.mergeState(name, id, newState);
  57. dispatchAction('setState', componentState);
  58. });
  59. },
  60. initState(initialState) {
  61. let state;
  62. if (DEFAULT_STATE) {
  63. state = DEFAULT_STATE.merge(initialState);
  64. } else if (initialState) {
  65. state = Immutable.Map.isMap(initialState) ? initialState : Immutable.fromJS(initialState);
  66. }
  67. if (state) {
  68. const componentState = actions.components.addState(name, id, state);
  69. dispatchAction('initState', componentState);
  70. }
  71. },
  72. deleteState(initialState) {
  73. if (DEFAULT_STATE || initialState) {
  74. const componentState = actions.components.removeState(name, id);
  75. dispatchAction('deleteState', componentState);
  76. }
  77. },
  78. };
  79. accessors.updateState = function updateState(state) {
  80. // eslint-disable-next-line no-console
  81. console.warn('DEPRECATION WARNING: please use props.setState');
  82. accessors.setState(state);
  83. };
  84. return accessors;
  85. }
  86. // DEPRECATION Warning: Please use cmfConnect.propTypes
  87. export const statePropTypes = {
  88. state: PropTypes.object,
  89. initialState: PropTypes.object,
  90. setState: PropTypes.func,
  91. initState: PropTypes.func,
  92. };
  93. export default {
  94. propTypes: statePropTypes,
  95. init: initState,
  96. getProps: getStateProps,
  97. getAccessors: getStateAccessors,
  98. };