Merge pull request #112 from coralproject/more-getinit

keep it DRY. add getInit all over the place. update tests
This commit is contained in:
David Jay
2016-11-28 18:43:06 -05:00
committed by GitHub
7 changed files with 18 additions and 36 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import * as actions from '../constants/auth';
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
// Check Login
+1 -1
View File
@@ -9,7 +9,7 @@ import {
SET_ROLE
} from '../constants/community';
import {base, getInit, handleResp} from '../helpers/response';
import {base, getInit, handleResp} from '../../../coral-framework/helpers/response';
export const fetchCommenters = (query = {}) => dispatch => {
dispatch(requestFetchCommenters());
+1 -1
View File
@@ -1,4 +1,4 @@
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
export const SETTINGS_LOADING = 'SETTINGS_LOADING';
export const SETTINGS_RECEIVED = 'SETTINGS_RECEIVED';
@@ -1,4 +1,4 @@
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
/**
* The adapter is a redux middleware that interecepts the actions that need
+11 -30
View File
@@ -1,3 +1,4 @@
import {getInit, base, handleResp} from '../../coral-framework/helpers/response';
import {fromJS} from 'immutable';
/* Item Actions */
@@ -10,26 +11,6 @@ export const UPDATE_ITEM = 'UPDATE_ITEM';
export const UPDATE_SETTINGS = 'UPDATE_SETTINGS';
export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY';
const getInit = (method, body) => {
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
const init = {method, headers};
if (body) {
init.body = JSON.stringify(body);
}
return init;
};
const responseHandler = response => {
if (response.status === 204) {
return;
}
return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`);
};
/**
* Action creators
*/
@@ -114,8 +95,8 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) =>
*/
export function getStream (assetUrl) {
return (dispatch) => {
return fetch(`/api/v1/stream?asset_url=${encodeURIComponent(assetUrl)}`)
.then(responseHandler)
return fetch(`${base}/stream?asset_url=${encodeURIComponent(assetUrl)}`)
.then(handleResp)
.then((json) => {
/* Add items to the store */
@@ -185,8 +166,8 @@ export function getStream (assetUrl) {
export function getItemsArray (ids) {
return (dispatch) => {
return fetch(`/v1/item/${ids}`, getInit('GET'))
.then(responseHandler)
return fetch(`${base}/item/${ids}`, getInit('GET'))
.then(handleResp)
.then((json) => {
for (let i = 0; i < json.items.length; i++) {
dispatch(addItem(json.items[i]));
@@ -215,8 +196,8 @@ export function postItem (item, type, id) {
if (id) {
item.id = id;
}
return fetch(`/api/v1/${type}`, getInit('POST', item))
.then(responseHandler)
return fetch(`${base}/${type}`, getInit('POST', item))
.then(handleResp)
.then((json) => {
dispatch(addItem({...item, id:json.id}, type));
return json.id;
@@ -246,8 +227,8 @@ export function postAction (item_id, action_type, user_id, item_type) {
user_id
};
return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('POST', action))
.then(responseHandler);
return fetch(`${base}/${item_type}/${item_id}/actions`, getInit('POST', action))
.then(handleResp);
};
}
@@ -268,7 +249,7 @@ export function postAction (item_id, action_type, user_id, item_type) {
export function deleteAction (action_id) {
return () => {
return fetch(`/api/v1/actions/${action_id}`, {method: 'DELETE'})
.then(responseHandler);
return fetch(`${base}/actions/${action_id}`, {method: 'DELETE'})
.then(handleResp);
};
}
+2 -2
View File
@@ -3,10 +3,10 @@ export const base = '/api/v1';
export const getInit = (method, body) => {
let init = {
method,
headers: new Headers({
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}),
},
credentials: 'same-origin'
};
@@ -127,6 +127,7 @@ describe('itemActions', () => {
'Accept': 'application/json',
'Content-Type':'application/json'
},
credentials: 'same-origin',
body: JSON.stringify(item.data)
}
);