Getting items from endpoint

And updating ‘item_id’ to ‘id’
This commit is contained in:
David Jay
2016-11-02 15:37:36 -07:00
parent 1c70b33260
commit 15114c1b33
+51 -40
View File
@@ -10,7 +10,6 @@ import mocks from '../../mocks.json'
export const ADD_ITEM = 'ADD_ITEM'
export const UPDATE_ITEM = 'UPDATE_ITEM'
export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY'
export const APPEND_ITEM_RELATED = 'APPEND_ITEM_RELATED'
/**
* Action creators
@@ -26,13 +25,13 @@ export const APPEND_ITEM_RELATED = 'APPEND_ITEM_RELATED'
*/
export const addItem = (item) => {
if (!item.item_id) {
if (!item.id) {
console.warn('addItem called without an item id.')
}
return {
type: ADD_ITEM,
item: item,
item_id: item.item_id
id: item.id
}
}
@@ -41,36 +40,26 @@ export const addItem = (item) => {
* Useful for item-level toggles, etc.
*
* @params
* item_id - the id of the item to be posted
* id - the id of the item to be posted
* property - the property to be updated
* value - the value that the property should be set to
*
*/
export const updateItem = (item_id, property, value) => {
export const updateItem = (id, property, value) => {
return {
type: UPDATE_ITEM,
item_id,
id,
property,
value
}
}
export const appendItemArray = (item_id, property, value) => {
export const appendItemArray = (id, property, value) => {
return {
type: APPEND_ITEM_ARRAY,
item_id,
property,
value
}
}
export const appendItemRelated = (item_id, property, value) => {
return {
type: APPEND_ITEM_RELATED,
item_id,
id,
property,
value
}
@@ -89,26 +78,48 @@ export const appendItemRelated = (item_id, property, value) => {
* @dispatches
* A set of items to the item store
*/
export function getItemsQuery (rootId) {
export function getStream (assetId) {
return (dispatch) => {
// return fetch('/v1/exec/view/' + rootId)
// .then(
// response => {
// return response.ok ? response.json() : Promise.reject(response.status + ' ' + response.statusText)
// }
// )
// .then((json) => {
// let items = json.results[0].Docs
// for (var i = 0; i < items.length; i++) {
// dispatch(addItem(items[i]))
// }
// return (items)
// })
console.log('Loading mock data', mocks);
let mockData = mocks.query
for (var i=0; i < mockData.length; i++ ) {
dispatch(addItem(mockData[i]))
}
return fetch('/api/v1/stream?asset_id='+assetId)
.then(
response => {
return response.ok ? response.json() : Promise.reject(response.status + ' ' + response.statusText)
}
)
.then((json) => {
/* Sort comments by date*/
let rootComments = []
let childComments = {}
const sorted = json.sort((a,b) => b.created_at - a.created_at)
sorted.reduce((prev, item) => {
dispatch(addItem(item))
/* Check for root and child comments. */
if (
item.type === 'comment' &&
item.asset_id === assetId &&
!item.parent_id) {
rootComments.push(item.id)
} else if (
item.type === 'comment' &&
item.asset_id === assetId
) {
let children = childComments[item.parent_id] || []
childComments[item.parent_id] = children.concat(item.id)
}
}, {})
dispatch(addItem({
id: assetId,
comments: rootComments
}))
Object.keys(childComments).reduce((prev, key) => {
dispatch(updateItem(key, 'children', childComments[key]))
},{})
return (json)
})
}
}
@@ -166,7 +177,7 @@ export function postItem (data, type, id) {
version: 1
}
if (id) {
item.item_id = id
item.id = id
}
let options = {
method: 'POST',
@@ -182,7 +193,7 @@ export function postItem (data, type, id) {
.then((json) => {
// Patch until ID is returned from backend
dispatch(addItem(json))
return json.item_id
return json.id
})
}
}
@@ -194,7 +205,7 @@ export function postItem (data, type, id) {
* Posts an action to an item
*
* @params
* item_id - the id of the item on which the action is taking place
* id - the id of the item on which the action is taking place
* action - the name of the action
* user - the user performing the action
* host - the coral host