Bocian.php.store.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var DBG = DBG || 0;
  2. var DBG1 = true;
  3. if (!STORE_NAME) throw "Store name not defined";
  4. var Redux = global.p5VendorJs.Redux;
  5. var ReduxThunk = global.p5VendorJs.ReduxThunk;
  6. var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js
  7. function getDefaultState() {
  8. return {
  9. totalOdPracownicy: 0,
  10. totalOdPozostale: 0,
  11. totalDoKontrahenci: 0,
  12. totalDoPozostale: 0,
  13. pozostaleOd: [],
  14. pozostaleDo: [],
  15. depth: 6,
  16. onlyTargets: true, // true: "y", false: "n"
  17. title: 'Raport bez tytułu z dnia ' + CURRENT_DATE,
  18. }
  19. }
  20. function getLocalStorageState() {
  21. DBG && console.log('DBG: bocian store INIT');
  22. var defaultState = getDefaultState();
  23. var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n"
  24. var pozostaleOd = getListFromLocalStorage(getSelectedListLocalStorageKey('OD'));
  25. var pozostaleDo = getListFromLocalStorage(getSelectedListLocalStorageKey('DO'));
  26. var pracownicyIds = getListFromLocalStorage('Bocian.biAuditForm.pracownicyIds');
  27. var kontrahenciIds = getListFromLocalStorage('Bocian.biAuditForm.kontrahenciIds');
  28. return {
  29. totalOdPracownicy: pracownicyIds.length,
  30. totalOdPozostale: pozostaleOd.length,
  31. totalDoKontrahenci: kontrahenciIds.length,
  32. totalDoPozostale: pozostaleDo.length,
  33. pozostaleOd: pozostaleOd,
  34. pozostaleDo: pozostaleDo,
  35. depth: getSignedIntFromLocalStorage('Bocian.biAuditForm.depth', defaultState.depth),
  36. onlyTargets: "n" === getStringFromLocalStorage('Bocian.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString) ? false : true,
  37. title: getStringFromLocalStorage('Bocian.biAuditForm.smadReaportTitle', defaultState.title),
  38. }
  39. }
  40. function bocianRaportStore(state, action) {
  41. var prevState = state || getLocalStorageState();
  42. DBG && console.warn('DBG: bocian store', { prevState, action, actionType: action.type });
  43. switch (action.type) {
  44. case 'UPDATE': return Object.assign(prevState, {
  45. totalOdPracownicy: action.totalOdPracownicy,
  46. totalDoKontrahenci: action.totalDoKontrahenci,
  47. });
  48. case 'ADD_OD': {
  49. var listPozostaleOd = prevState.pozostaleOd.concat(action.featureKey);
  50. localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify(listPozostaleOd))
  51. return Object.assign(prevState, {
  52. pozostaleOd: listPozostaleOd,
  53. totalOdPozostale: listPozostaleOd.length,
  54. })
  55. }
  56. case 'ADD_DO': {
  57. var listPozostaleDo = prevState.pozostaleDo.concat(action.featureKey);
  58. localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify(listPozostaleDo))
  59. return Object.assign(prevState, {
  60. pozostaleDo: listPozostaleDo,
  61. totalDoPozostale: listPozostaleDo.length,
  62. })
  63. }
  64. case 'RM_OD': {
  65. var featureKey = action.featureKey;
  66. var listPozostaleOd = prevState.pozostaleOd.filter(function (item) {
  67. return ( item !== featureKey );
  68. })
  69. localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify(listPozostaleOd))
  70. return Object.assign(prevState, {
  71. pozostaleOd: listPozostaleOd,
  72. totalOdPozostale: listPozostaleOd.length,
  73. })
  74. }
  75. case 'RM_DO': {
  76. var featureKey = action.featureKey;
  77. var listPozostaleDo = prevState.pozostaleDo.filter(function (item) {
  78. return ( item !== featureKey );
  79. })
  80. localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify(listPozostaleDo))
  81. return Object.assign(prevState, {
  82. pozostaleDo: listPozostaleDo,
  83. totalDoPozostale: listPozostaleDo.length,
  84. })
  85. }
  86. case 'CLEAR_POZOSTALE_OD': {
  87. localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify([]))
  88. return Object.assign(prevState, {
  89. pozostaleOd: [],
  90. totalOdPozostale: 0,
  91. })
  92. }
  93. case 'CLEAR_POZOSTALE_DO': {
  94. localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify([]))
  95. return Object.assign(prevState, {
  96. pozostaleDo: [],
  97. totalDoPozostale: 0,
  98. })
  99. }
  100. case 'SET_DEPTH': {
  101. localStorage.setItem('Bocian.biAuditForm.depth', action.depth)
  102. return Object.assign(prevState, {
  103. depth: action.depth,
  104. })
  105. }
  106. case 'SET_ONLY_TARGETS': {
  107. localStorage.setItem('Bocian.biAuditForm.analizaOnlyTargets', action.onlyTargets ? '"y"' : '"n"')
  108. return Object.assign(prevState, {
  109. onlyTargets: action.onlyTargets,
  110. })
  111. }
  112. case 'SET_TITLE': {
  113. localStorage.setItem('Bocian.biAuditForm.smadReaportTitle', '"' + action.title + '"')
  114. return Object.assign(prevState, {
  115. title: action.title,
  116. })
  117. }
  118. case 'CLEAR_ALL': {
  119. var defaultState = getDefaultState();
  120. var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n"
  121. setItemLocalStorage('Bocian.biAuditForm.pracownicyIds', []);
  122. setItemLocalStorage('Bocian.biAuditForm.kontrahenciIds', []);
  123. setItemLocalStorage(getSelectedListLocalStorageKey('OD'), []);
  124. setItemLocalStorage(getSelectedListLocalStorageKey('DO'), []);
  125. setItemLocalStorage('Bocian.biAuditForm.pracownicy.filterIdGroup', 0);
  126. setItemLocalStorage('Bocian.biAuditForm.kontrahenci.filterIdGroup', 0);
  127. setItemLocalStorage('Bocian.biAuditForm.pracownicy.pagination.page', 1);
  128. setItemLocalStorage('Bocian.biAuditForm.kontrahenci.pagination.page', 1);
  129. setItemLocalStorage('Bocian.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString);
  130. setItemLocalStorage('Bocian.biAuditForm.depth', defaultState.depth);
  131. setItemLocalStorage('Bocian.biAuditForm.smadReaportTitle', defaultState.title);
  132. return Object.assign(prevState, defaultState)
  133. }
  134. default: return prevState;
  135. }
  136. }
  137. function getSelectedListLocalStorageKey(from) { // from: ( OD | DO )
  138. switch (from) {
  139. case 'OD': return 'Bocian.biAuditForm.odFeatureKeys';
  140. case 'DO': return 'Bocian.biAuditForm.doFeatureKeys';
  141. default: throw "Not implemented from selected list '" + from + "' - expected 'OD' or 'DO'";
  142. }
  143. }
  144. function getListFromLocalStorage(key) {
  145. return JSON.parse( localStorage.getItem(key) || '[]' );
  146. }
  147. function getSignedIntFromLocalStorage(key, defaultValue) {
  148. return JSON.parse( localStorage.getItem(key) || defaultValue );
  149. }
  150. function getStringFromLocalStorage(key, defaultValue) {
  151. var value = localStorage.getItem(key);
  152. value = (value) ? JSON.parse(value) : '';
  153. return (value) ? value : defaultValue;
  154. }
  155. function setItemLocalStorage(key, value) {
  156. localStorage.setItem( key, JSON.stringify(value) );
  157. }
  158. function getItemLocalStorage(key) {
  159. return JSON.parse( localStorage.getItem(key) );
  160. }
  161. global[STORE_NAME] = createStoreWithThunkMiddleware(bocianRaportStore);
  162. global[STORE_NAME + '__defaults'] = getDefaultState;