Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. // resources/assets/js/helpers/redux-utils.js
  2. import Immutable from 'immutable';
  3. import { mapValues } from 'lodash';
  4. import { getModule, getResource } from './states';
  5. import { attachReducers } from '../stores/store';
  6. import { combineReducers } from 'redux';
  7.  
  8. const locationMap = {};
  9.  
  10. export function defaultLocator(listId) {
  11. return state => state[listId];
  12. }
  13.  
  14. /**
  15. * @param {String} action type
  16. * @param {String} id
  17. * @return {String} built action type
  18. */
  19. export function buildActionType(t, id) {
  20. return `${t}_${id}`;
  21. }
  22.  
  23. export function fromJSIterable(js) {
  24. return typeof js !== 'object' || js === null ? js :
  25. (Array.isArray(js) && js.every(v => typeof v !== 'object' && !Array.isArray(v)) ?
  26. Immutable.Seq(js).map(fromJSIterable).toSet() :
  27. (Array.isArray(js) ?
  28. Immutable.Seq(js).map(fromJSIterable).toList() :
  29. Immutable.Seq(js).map(fromJSIterable).toMap()
  30. )
  31. );
  32. }
  33.  
  34. /**
  35. * @params {Object} preState
  36. * @return {Object}
  37. */
  38. export function recipesToImmutable(preState) {
  39. const keyRecipes = 'recipes';
  40.  
  41. return mapValues(preState, (obj, key) => {
  42. if(
  43. obj instanceof Object &&
  44. (key.indexOf(keyRecipes) === 0 ||
  45. key.toLowerCase().lastIndexOf(keyRecipes) === (key.length - keyRecipes.length) ||
  46. 'recipes' in obj)
  47. ) {
  48. return recipesToImmutable(obj);
  49. }
  50.  
  51. if( key.indexOf('recipe') === 0 && key.indexOf('recipes') !== 0 ) {
  52. return fromJSIterable(obj);
  53. }
  54.  
  55. return obj;
  56. });
  57. }
  58.  
  59. /**
  60. * name content
  61. * @param {Number} modType
  62. * @param {Boolean} isResource
  63. * @return {String}
  64. */
  65. export function namedContent(modType, isResource) {
  66. const mod = {
  67. [getModule('blog')]: 'Article',
  68. [getModule('wall')]: 'Wall',
  69. [getModule('forum')]: 'Forum',
  70. [getModule('calendar')]: 'Event',
  71. [getModule('learn')]: 'Course'
  72. };
  73.  
  74. const rsc = {
  75. [getResource('discusion.gestion')]: 'Discussion',
  76. [getResource('discusion.comentario')]: 'DiscussionComment'
  77. };
  78.  
  79. if(!isResource) {
  80. return mod[modType];
  81. }else {
  82. return rsc[modType];
  83. }
  84.  
  85. };
  86.  
  87. /**
  88. * get content type
  89. * @param {Number} modType
  90. * @return {String} type
  91. */
  92. export function typeContent(modType) {
  93. const mod = {
  94. [getModule('blog')]: 'article',
  95. [getModule('wall')]: 'post',
  96. [getModule('forum')]: 'forum',
  97. [getModule('calendar')]: 'event',
  98. [getModule('learn')]: 'course'
  99. }
  100.  
  101. return mod[modType]
  102. }
  103.  
  104. function versioningLocation(location) {
  105. if( ({}).hasOwnProperty.call(locationMap, location) && locationMap[location] > 0 ) {
  106. locationMap[location] = locationMap[location] + 1;
  107. }else {
  108. locationMap[location] = 1;
  109. }
  110. }
  111. /**
  112. * function compose to build dynamic reducers
  113. * @param { Object } options
  114. * @return { Function } to be pass those parameters: entries { Array|Object }, options ({ location, updateCounter, dynamicName })
  115. */
  116. export function composeDynamicReducers({ nameReducer, createReducer, location }) {
  117. return (entries, options={}) => {
  118. const _entries = Array.isArray(entries) ? entries : [entries];
  119. let attachDynamics = {};
  120.  
  121. const settings = {
  122. location,
  123. updateCounter: () => {},
  124. ...options
  125. };
  126. const currentLocation = `${settings.location}${locationMap[settings.location] || ''}`;
  127. const replace = options.replace || false;
  128.  
  129. _entries.forEach(entry => {
  130. const prefixName = typeof nameReducer === 'function' ? nameReducer(entry) : (nameReducer || '');
  131. const sufixName = typeof options.dynamicName === 'function' ? options.dynamicName(entry) : (options.dynamicName || '');
  132.  
  133. const dynamicName = `${prefixName}${sufixName}`;
  134.  
  135. if(
  136. typeof dynamicName === 'string' &&
  137. typeof createReducer === 'function'
  138. ) {
  139. attachDynamics[dynamicName] = createReducer(entry, {
  140. ...settings,
  141. dynamicName,
  142. location: currentLocation
  143. });
  144. }
  145. });
  146.  
  147. // attach recipes to store root reducer
  148. if( Object.keys(attachDynamics).length ) {
  149. !replace && versioningLocation(settings.location);
  150.  
  151. attachReducers({ [currentLocation]: combineReducers(attachDynamics) });
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement