insomnia/app/ui/redux/modules/requests.js

71 lines
1.5 KiB
JavaScript
Raw Normal View History

import {combineReducers} from 'redux';
import {trackEvent} from '../../../backend/analytics';
import * as network from '../../../backend/network';
2016-04-23 06:08:52 +00:00
export const REQUEST_CHANGE_FILTER = 'requests/filter';
export const REQUEST_SEND_START = 'requests/start';
export const REQUEST_SEND_STOP = 'requests/stop';
2016-04-23 06:08:52 +00:00
2016-04-23 06:08:52 +00:00
// ~~~~~~~~ //
// REDUCERS //
// ~~~~~~~~ //
function filterReducer(state = '', action) {
2016-04-23 06:08:52 +00:00
switch (action.type) {
2016-04-23 06:08:52 +00:00
case REQUEST_CHANGE_FILTER:
const filter = action.filter;
return Object.assign({}, state, {filter});
default:
return state;
}
}
function loadingRequestsReducer(state = {}, action) {
2016-07-07 20:10:55 +00:00
let newState;
switch (action.type) {
case REQUEST_SEND_START:
2016-07-07 20:10:55 +00:00
newState = Object.assign({}, state);
newState[action.requestId] = Date.now();
return newState;
case REQUEST_SEND_STOP:
2016-07-07 20:10:55 +00:00
newState = Object.assign({}, state);
delete newState[action.requestId];
return newState;
2016-04-23 06:08:52 +00:00
default:
return state;
}
}
export default combineReducers({
filter: filterReducer,
loadingRequests: loadingRequestsReducer
});
2016-04-23 06:08:52 +00:00
// ~~~~~~~ //
// ACTIONS //
// ~~~~~~~ //
export function send(request) {
return async function (dispatch) {
dispatch({type: REQUEST_SEND_START, requestId: request._id});
2016-07-27 20:07:50 +00:00
trackEvent('Request Send');
2016-04-23 06:08:52 +00:00
try {
await network.send(request._id);
2016-07-20 21:15:11 +00:00
dispatch({type: REQUEST_SEND_STOP, requestId: request._id});
} catch (e) {
// console.info('Error sending request', e);
dispatch({type: REQUEST_SEND_STOP, requestId: request._id});
}
2016-04-23 06:08:52 +00:00
}
}