TableAjax.php.createTableDataStateObject.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var DBG = DBG || false;
  2. if (!URI_WPS) throw "Missing URI_WPS";
  3. var DBG1 = true;
  4. if (!global.p5VendorJs) throw "Missing p5 Vendor js libs";
  5. if (!global.p5VendorJs.Redux) throw "Missing p5 Vendor js lib: Redux";
  6. var Redux = global.p5VendorJs.Redux;
  7. var ReduxThunk = global.p5VendorJs.ReduxThunk;
  8. var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js
  9. function createTableStoreWithInitialData(initialFilter) {
  10. DBG && console.log('INIT: initialFilter', { initialFilter });
  11. var initialState = {
  12. width: 1000,
  13. isLoading: false, sentRequestId: 0, receivedRequestId: 0,
  14. isEmpty: true,
  15. rows: [],
  16. }
  17. Object.keys(initialFilter).forEach(function (key) {
  18. if ('currSortCol' === key) {
  19. initialState.currSortCol = initialFilter[key]
  20. } else if ('currSortFlip' === key) {
  21. if ('desc' === initialFilter[key]) initialState.currSortFlip = true;
  22. else if ('asc' === initialFilter[key]) initialState.currSortFlip = false;
  23. else initialState.currSortFlip = Boolean(initialFilter[key]);
  24. } else if ('f_' === key.substr(0, 2)) {
  25. initialState.filter.set(key.substr(2), initialFilter[key])
  26. } else if ('sf_' === key.substr(0, 3)) {
  27. initialState.specialFilter.set(key.substr(3), initialFilter[key])
  28. }
  29. })
  30. return function tableStore(state, action) {
  31. var prevState = state || initialState;
  32. DBG && console.warn('DBG:tableStore:' + action.type, action);
  33. switch (action.type) {
  34. case 'SET_SENT_REQUEST_ID': return Object.assign({}, prevState, {
  35. sentRequestId: action.sentRequestId,
  36. filterParams: action.filterParams, // needed?
  37. isLoading: true,
  38. });
  39. case 'SET_RESPONSE': return Object.assign({}, prevState, {
  40. receivedRequestId: action.response.requestId,
  41. rows: action.response.body.rows,
  42. isLoading: (prevState.sentRequestId > action.response.requestId) ? true : false,
  43. });
  44. case 'SET_LOADING': return Object.assign({}, prevState, {
  45. isLoading: Boolean(action.isLoading),
  46. });
  47. case 'SET_DATA': return Object.assign({}, prevState, {
  48. rows: action.rows
  49. });
  50. case 'SET_WIDTH': return Object.assign({}, prevState, {
  51. width: action.width
  52. });
  53. default: return prevState;
  54. }
  55. }
  56. }
  57. function createTableDataStoreActions(fetchDataPromise) {
  58. var _fetchDataPromise = fetchDataPromise;
  59. var delay = 450;
  60. var _filterTimeout = null;
  61. var _clearTimer = function () {
  62. if (_filterTimeout) {
  63. clearTimeout(_filterTimeout);
  64. }
  65. }
  66. var _setTimer = function (timeout) {
  67. _filterTimeout = timeout;
  68. }
  69. function setSentRequestId(sentRequestId, filterParams) {
  70. return { type: 'SET_SENT_REQUEST_ID', sentRequestId: sentRequestId, filterParams: filterParams };
  71. }
  72. function setResponse(response) {
  73. return { type: 'SET_RESPONSE', response: response };
  74. }
  75. function loadData(namespace, filterParams) {
  76. DBG && console.log('DBG:tableStore:loadData #JSTA1', { namespace, filterParams });
  77. return function (dispatch, getState) {
  78. // dispatch( { type: 'SET_LOADING', isLoading: true } );
  79. return new Promise(function (resolve, reject) {
  80. var state = getState();
  81. DBG && console.log('DBG:tableStore:loadData #JSTA2 Promise', { namespace, filterParams, state });
  82. var this__requestId = 1 + state.sentRequestId;
  83. dispatch(setSentRequestId(this__requestId));
  84. // _clearTimer();
  85. // _setTimer(
  86. // setTimeout(function () {
  87. // // reject("Error");
  88. // var value = {
  89. // rows: window._.range( window._.random(3, 21) ).map(function (val, idx) {
  90. // var pk = idx + 1;
  91. // return { '@primaryKey': pk, id: pk, label: 'row ' + pk, cell_2: 'cell 2', cell_3: 'cell 3', cell_4: 'cell 4', cell_5: 'cell 5', cell_6: 'cell 6', cell_7: 'cell 7', cell_8: 'cell 8' };
  92. // }).reverse()
  93. // };
  94. //
  95. // var response = {
  96. // requestId: this__requestId,
  97. // msg: "Pobrano dane",
  98. // body: {
  99. // rows: value.rows
  100. // }
  101. // };
  102. // // resolve(response);
  103. // dispatch(setResponse(response)); // TODO: depend on delay - ajax queue
  104. // }, delay)
  105. // );
  106. _fetchDataPromise(namespace, filterParams).then(function (tableData) {
  107. DBG && console.log('DBG:tableStore:loadData #JSTA3 resolve', { tableData });
  108. var response = {
  109. requestId: this__requestId,
  110. msg: "Pobrano dane",
  111. body: tableData,
  112. }
  113. dispatch(setResponse(response));
  114. }).catch(reject)
  115. }).catch(function (err) {
  116. DBG && console.warn('DBG:tableStore:loadData #JSTA4 reject', { err });
  117. // dispatch(setErrorResponse(err)); // TODO display error
  118. })
  119. }
  120. }
  121. return {
  122. loadData: loadData,
  123. }
  124. }
  125. function createTableDataStateObject(initialData, fetchDataPromise) {
  126. var _initialData = initialData || {};
  127. return {
  128. store: createStoreWithThunkMiddleware( createTableStoreWithInitialData( _initialData ) ),
  129. actions: createTableDataStoreActions(fetchDataPromise),
  130. }
  131. }
  132. global.createTableDataStateObject = createTableDataStateObject;