2016-07-16 02:06:10 +00:00
|
|
|
import {combineReducers} from 'redux';
|
|
|
|
import * as network from '../../lib/network';
|
|
|
|
import {show} from './modals';
|
|
|
|
import {MODAL_REQUEST_RENAME, MODAL_CURL_EXPORT} from '../../lib/constants';
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
export const REQUEST_CHANGE_FILTER = 'requests/filter';
|
2016-07-06 22:11:37 +00:00
|
|
|
export const REQUEST_SEND_START = 'requests/start';
|
|
|
|
export const REQUEST_SEND_STOP = 'requests/stop';
|
2016-04-23 06:08:52 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~ //
|
|
|
|
// REDUCERS //
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
function filterReducer(state = '', action) {
|
2016-04-23 06:08:52 +00:00
|
|
|
switch (action.type) {
|
2016-07-06 22:11:37 +00:00
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
case REQUEST_CHANGE_FILTER:
|
2016-04-26 07:29:24 +00:00
|
|
|
const filter = action.filter;
|
|
|
|
return Object.assign({}, state, {filter});
|
2016-07-06 22:11:37 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadingRequestsReducer(state = {}, action) {
|
2016-07-07 20:10:55 +00:00
|
|
|
let newState;
|
2016-07-06 22:11:37 +00:00
|
|
|
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
|
2016-07-06 22:11:37 +00:00
|
|
|
|
|
|
|
case REQUEST_SEND_STOP:
|
2016-07-07 20:10:55 +00:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
delete newState[action.requestId];
|
|
|
|
return newState;
|
2016-07-06 22:11:37 +00:00
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
export default combineReducers({
|
|
|
|
filter: filterReducer,
|
|
|
|
loadingRequests: loadingRequestsReducer
|
|
|
|
});
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// ACTIONS //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
export function changeFilter(filter) {
|
2016-04-23 06:08:52 +00:00
|
|
|
return {type: REQUEST_CHANGE_FILTER, filter};
|
|
|
|
}
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
export function send(request) {
|
2016-04-23 06:08:52 +00:00
|
|
|
return dispatch => {
|
2016-07-06 22:11:37 +00:00
|
|
|
dispatch({type: REQUEST_SEND_START, requestId: request._id});
|
2016-04-23 06:08:52 +00:00
|
|
|
|
2016-04-29 08:15:37 +00:00
|
|
|
network.send(request._id, () => {
|
2016-07-06 22:11:37 +00:00
|
|
|
dispatch({type: REQUEST_SEND_STOP, requestId: request._id});
|
2016-04-23 06:08:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
export function showUpdateNamePrompt(request) {
|
2016-04-23 06:08:52 +00:00
|
|
|
const defaultValue = request.name;
|
|
|
|
return show(MODAL_REQUEST_RENAME, {defaultValue, request});
|
|
|
|
}
|
2016-07-14 22:48:56 +00:00
|
|
|
|
|
|
|
export function showCurlExportModal (request) {
|
|
|
|
return show(MODAL_CURL_EXPORT, {request});
|
|
|
|
}
|
|
|
|
|