Adding action to upate asset state.

This commit is contained in:
David Jay
2016-12-15 13:10:23 -05:00
parent 86eef68b16
commit c7b232f0b3
5 changed files with 87 additions and 28 deletions
+3 -3
View File
@@ -1,11 +1,11 @@
import {FETCH_ASSETS, UPDATE_ASSET} from '../constants/assets';
import {FETCH_ASSETS, UPDATE_ASSET_STATE} from '../constants/assets';
/**
* Action disptacher related to assets
*/
export const updateAsset = (id, property, value) => (dispatch) => {
dispatch({type: UPDATE_ASSET, id, property, value});
export const updateAssetState = (id, property, value) => (dispatch) => {
dispatch({type: UPDATE_ASSET_STATE, id, property, value});
};
export const fetchAssets = (skip, limit, search, sort) => (dispatch) => {
+3 -1
View File
@@ -1,4 +1,6 @@
export const FETCH_ASSETS = 'FETCH_ASSETS';
export const FETCH_ASSETS_SUCCESS = 'FETCH_ASSETS_SUCCESS';
export const FETCH_ASSETS_FAILED = 'FETCH_ASSETS_FAILED';
export const UPDATE_ASSET = 'UPDATE_ASSET';
export const UPDATE_ASSET_STATE = 'UPDATE_ASSET_STATE';
export const UPDATE_ASSET_STATE_SUCCESS = 'UPDATE_ASSET_STATE_SUCCESS';
export const UPDATE_ASSET_STATE_FAILED = 'UPDATE_ASSET_STATE_FAILED';
@@ -1,5 +1,12 @@
import coralApi from '../../../coral-framework/helpers/response';
import {
FETCH_ASSETS,
FETCH_ASSETS_FAILED,
FETCH_ASSETS_SUCCESS,
UPDATE_ASSET_STATE,
UPDATE_ASSET_STATE_SUCCESS,
UPDATE_ASSET_STATE_FAILED,
} from '../constants/assets';
/**
* The adapter is a redux middleware that interecepts the actions that need
* to interface with the backend, do the job and return the results.
@@ -22,8 +29,10 @@ export default store => next => action => {
return createComment(store, action.name, action.body);
case 'USER_BAN':
return userStatusUpdate(store, action.status, action.userId, action.commentId);
case 'FETCH_ASSETS':
case FETCH_ASSETS:
return fetchAssets(store, action);
case UPDATE_ASSET_STATE:
return updateAssetState(store, action);
}
};
@@ -97,9 +106,20 @@ const fetchAssets = (store, action) => {
return coralApi(`/assets?skip=${skip}&limit=${limit}&search=${search}`)
.then(({result, count}) =>
/* Post comments and users to redux store. Actions will be posted when they are needed. */
store.dispatch({type: 'FETCH_ASSETS_SUCCESS',
store.dispatch({type: FETCH_ASSETS_SUCCESS,
assets: result,
count
}))
.catch(error => store.dispatch({type: 'FETCH_ASSETS_FAILED', error}));
.catch(error => store.dispatch({type: FETCH_ASSETS_FAILED, error}));
};
// Update an asset state
// Get comments to fill each of the three lists on the mod queue
const updateAssetState = (store, action) => {
const {id, closedAt} = action;
return coralApi(`/assets/${id}/status`, {method: 'PUT', body: {closedAt}})
.then(() =>
/* Post comments and users to redux store. Actions will be posted when they are needed. */
store.dispatch({type: UPDATE_ASSET_STATE_SUCCESS, closedAt}))
.catch(error => store.dispatch({type: UPDATE_ASSET_STATE_FAILED, error}));
};
+2 -2
View File
@@ -3,10 +3,10 @@ import {expect} from 'chai';
import assetsReducer from '../../../../client/coral-admin/src/reducers/assets';
describe ('assetsReducer', () => {
describe('ASSETS_FETCH_SUCCESS', () => {
describe('FETCH_ASSETS_SUCCESS', () => {
it('should replace the existing assets', () => {
const action = {
type: 'ASSETS_FETCH_SUCCESS',
type: 'FETCH_ASSETS_SUCCESS',
count: 200,
assets: [
{
@@ -12,30 +12,30 @@ const mockStore = configureStore();
describe('talk-adapter.js', () => {
let store;
const assets = [
{
url: 'http://test.com',
id: '123',
status: 'closed'
},
{
url: 'http://test.org',
id: '456',
status: 'open'
}
];
beforeEach(() => {
store = mockStore(new Map({}));
fetchMock.restore();
});
describe('ASSETS_FETCH', () => {
const assets = [
{
url: 'http://test.com',
id: '123',
status: 'closed'
},
{
url: 'http://test.org',
id: '456',
status: 'open'
}
];
describe('FETCH_ASSETS', () => {
it('should fetch a list of assets', () => {
const action = {
type: 'ASSETS_FETCH',
type: 'FETCH_ASSETS',
skip: 2,
limit: 20,
search: ''
@@ -48,7 +48,7 @@ describe('talk-adapter.js', () => {
return adapter(store)(()=>{})(action)
.then(() => {
expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_SUCCESS');
expect(store.getActions()[0]).to.have.property('type', 'FETCH_ASSETS_SUCCESS');
expect(store.getActions()[0]).to.have.property('count', 2);
expect(store.getActions()[0]).to.have.property('assets').
and.to.deep.equal(assets);
@@ -58,7 +58,7 @@ describe('talk-adapter.js', () => {
it('should return an error appropriatly', () => {
const action = {
type: 'ASSETS_FETCH',
type: 'FETCH_ASSETS',
skip: 2,
limit: 20,
search: ''
@@ -68,7 +68,44 @@ describe('talk-adapter.js', () => {
return adapter(store)(()=>{})(action)
.then(() => {
expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_FAILED');
expect(store.getActions()[0]).to.have.property('type', 'FETCH_ASSETS_FAILED');
});
});
});
describe('UPDATE_ASSET_STATE', () => {
it('should update an asset', () => {
const action = {
type: 'UPDATE_ASSET_STATE',
id: '123',
property: 'closedAt',
value: Date.now()
};
fetchMock.put('*', JSON.stringify(assets[0]));
return adapter(store)(()=>{})(action)
.then(() => {
expect(store.getActions()[0]).to.have.property('type', 'UPDATE_ASSET_STATE_SUCCESS');
});
});
it('should return an error appropriately', () => {
const action = {
type: 'UPDATE_ASSET_STATE',
id: '123',
property: 'closedAt',
value: Date.now()
};
fetchMock.put('*', 404);
return adapter(store)(()=>{})(action)
.then(() => {
expect(store.getActions()[0]).to.have.property('type', 'UPDATE_ASSET_STATE_FAILED');
});
});
});