insomnia/app/actions/requests.js

138 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-03-20 04:00:40 +00:00
import * as types from '../constants/actionTypes'
import * as methods from '../constants/global'
import {loadStart} from "./global";
import {loadStop} from "./global";
2016-03-24 05:26:04 +00:00
const defaultRequest = {
id: null,
created: 0,
modified: 0,
url: '',
name: '',
method: methods.METHOD_GET,
body: '',
params: [],
2016-04-04 07:15:30 +00:00
headers: [{
name: 'Content-Type',
value: 'application/json'
}],
2016-03-24 05:26:04 +00:00
authentication: {}
};
2016-03-20 04:00:40 +00:00
function buildRequest (request) {
// Build the required fields
2016-03-20 20:42:27 +00:00
const id = request.id || `rq_${Date.now()}`;
2016-03-20 04:00:40 +00:00
const created = request.created || Date.now();
const modified = request.modified || Date.now();
// Create the request
2016-04-03 22:54:39 +00:00
return Object.assign({}, defaultRequest, request, {
2016-03-20 04:00:40 +00:00
id, created, modified
});
}
2016-04-04 07:15:30 +00:00
export function addRequest (requestGroupId = null) {
2016-03-22 05:01:58 +00:00
return (dispatch) => {
2016-03-20 04:00:40 +00:00
dispatch(loadStart());
2016-04-04 07:15:30 +00:00
const request = buildRequest({name: 'My Request'});
2016-03-20 20:42:27 +00:00
dispatch({type: types.REQUEST_ADD, request});
2016-03-20 04:00:40 +00:00
2016-04-05 06:08:03 +00:00
// HACK: Add request to group right away. Not sure how to get around this
// TODO: Make this not need to know about RequestGroup actions
2016-04-04 07:15:30 +00:00
if (requestGroupId) {
const id = requestGroupId;
const requestId = request.id;
dispatch({type: types.REQUEST_GROUP_ADD_CHILD_REQUEST, requestId, id});
}
2016-03-20 20:42:27 +00:00
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
}, 500);
});
2016-03-20 04:00:40 +00:00
};
}
2016-04-03 22:54:39 +00:00
export function deleteRequest (id) {
return (dispatch) => {
dispatch(loadStart());
dispatch({type: types.REQUEST_DELETE, id});
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
}, 500);
});
}
}
2016-03-20 04:00:40 +00:00
export function updateRequest (requestPatch) {
2016-03-20 23:20:00 +00:00
if (!requestPatch.id) {
throw new Error('Cannot update request without id');
}
2016-03-20 04:00:40 +00:00
return (dispatch) => {
dispatch(loadStart());
2016-03-20 23:20:00 +00:00
const modified = Date.now();
const patch = Object.assign({}, requestPatch, {modified});
dispatch({type: types.REQUEST_UPDATE, patch});
2016-03-20 20:42:27 +00:00
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
2016-03-20 23:20:00 +00:00
}, 800);
2016-03-20 20:42:27 +00:00
});
2016-03-20 04:00:40 +00:00
};
}
2016-03-20 04:47:43 +00:00
2016-04-05 05:35:21 +00:00
export function duplicateRequest (oldRequest, requestGroupId) {
return (dispatch) => {
dispatch(loadStart());
2016-04-05 06:08:03 +00:00
const request = buildRequest(
Object.assign({}, oldRequest, {id: null, name: `${oldRequest.name} Copy`})
);
2016-04-05 05:35:21 +00:00
dispatch({type: types.REQUEST_ADD, request});
if (requestGroupId) {
const id = requestGroupId;
const requestId = request.id;
dispatch({type: types.REQUEST_GROUP_ADD_CHILD_REQUEST, requestId, id});
}
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
}, 500);
});
};
}
2016-03-20 23:20:00 +00:00
export function updateRequestUrl (id, url) {
return updateRequest({id, url});
}
export function updateRequestBody (id, body) {
return updateRequest({id, body});
}
2016-03-20 23:27:46 +00:00
export function updateRequestMethod (id, method) {
return updateRequest({id, method});
}
2016-03-20 23:20:00 +00:00
export function activateRequest (id) {
return {type: types.REQUEST_ACTIVATE, id};
2016-03-20 04:47:43 +00:00
}
2016-03-23 05:26:27 +00:00
export function changeFilter (filter) {
return {type: types.REQUEST_CHANGE_FILTER, filter};
}
2016-04-04 07:15:30 +00:00
export function sendRequest (id) {
2016-04-05 05:35:21 +00:00
2016-04-04 07:15:30 +00:00
}