var DBG = DBG || false; if (!URI_WPS) throw "Missing URI_WPS"; var DBG1 = true; if (!global.p5VendorJs) throw "Missing p5 Vendor js libs"; if (!global.p5VendorJs.Redux) throw "Missing p5 Vendor js lib: Redux"; var Redux = global.p5VendorJs.Redux; var ReduxThunk = global.p5VendorJs.ReduxThunk; var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js function createTableStoreWithInitialData(initialFilter) { DBG && console.log('INIT: initialFilter', { initialFilter }); var initialState = { width: 1000, isLoading: false, sentRequestId: 0, receivedRequestId: 0, isEmpty: true, rows: [], } Object.keys(initialFilter).forEach(function (key) { if ('currSortCol' === key) { initialState.currSortCol = initialFilter[key] } else if ('currSortFlip' === key) { if ('desc' === initialFilter[key]) initialState.currSortFlip = true; else if ('asc' === initialFilter[key]) initialState.currSortFlip = false; else initialState.currSortFlip = Boolean(initialFilter[key]); } else if ('f_' === key.substr(0, 2)) { initialState.filter.set(key.substr(2), initialFilter[key]) } else if ('sf_' === key.substr(0, 3)) { initialState.specialFilter.set(key.substr(3), initialFilter[key]) } }) return function tableStore(state, action) { var prevState = state || initialState; DBG && console.warn('DBG:tableStore:' + action.type, action); switch (action.type) { case 'SET_SENT_REQUEST_ID': return Object.assign({}, prevState, { sentRequestId: action.sentRequestId, filterParams: action.filterParams, // needed? isLoading: true, }); case 'SET_RESPONSE': return Object.assign({}, prevState, { receivedRequestId: action.response.requestId, rows: action.response.body.rows, isLoading: (prevState.sentRequestId > action.response.requestId) ? true : false, }); case 'SET_LOADING': return Object.assign({}, prevState, { isLoading: Boolean(action.isLoading), }); case 'SET_DATA': return Object.assign({}, prevState, { rows: action.rows }); case 'SET_WIDTH': return Object.assign({}, prevState, { width: action.width }); default: return prevState; } } } function createTableDataStoreActions(fetchDataPromise) { var _fetchDataPromise = fetchDataPromise; var delay = 450; var _filterTimeout = null; var _clearTimer = function () { if (_filterTimeout) { clearTimeout(_filterTimeout); } } var _setTimer = function (timeout) { _filterTimeout = timeout; } function setSentRequestId(sentRequestId, filterParams) { return { type: 'SET_SENT_REQUEST_ID', sentRequestId: sentRequestId, filterParams: filterParams }; } function setResponse(response) { return { type: 'SET_RESPONSE', response: response }; } function loadData(namespace, filterParams) { DBG && console.log('DBG:tableStore:loadData #JSTA1', { namespace, filterParams }); return function (dispatch, getState) { // dispatch( { type: 'SET_LOADING', isLoading: true } ); return new Promise(function (resolve, reject) { var state = getState(); DBG && console.log('DBG:tableStore:loadData #JSTA2 Promise', { namespace, filterParams, state }); var this__requestId = 1 + state.sentRequestId; dispatch(setSentRequestId(this__requestId)); // _clearTimer(); // _setTimer( // setTimeout(function () { // // reject("Error"); // var value = { // rows: window._.range( window._.random(3, 21) ).map(function (val, idx) { // var pk = idx + 1; // 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' }; // }).reverse() // }; // // var response = { // requestId: this__requestId, // msg: "Pobrano dane", // body: { // rows: value.rows // } // }; // // resolve(response); // dispatch(setResponse(response)); // TODO: depend on delay - ajax queue // }, delay) // ); _fetchDataPromise(namespace, filterParams).then(function (tableData) { DBG && console.log('DBG:tableStore:loadData #JSTA3 resolve', { tableData }); var response = { requestId: this__requestId, msg: "Pobrano dane", body: tableData, } dispatch(setResponse(response)); }).catch(reject) }).catch(function (err) { DBG && console.warn('DBG:tableStore:loadData #JSTA4 reject', { err }); // dispatch(setErrorResponse(err)); // TODO display error }) } } return { loadData: loadData, } } function createTableDataStateObject(initialData, fetchDataPromise) { var _initialData = initialData || {}; return { store: createStoreWithThunkMiddleware( createTableStoreWithInitialData( _initialData ) ), actions: createTableDataStoreActions(fetchDataPromise), } } global.createTableDataStateObject = createTableDataStateObject;