diff --git a/client/coral-admin/src/actions/assets.js b/client/coral-admin/src/actions/assets.js index 1e5f2716d..5918d1df8 100644 --- a/client/coral-admin/src/actions/assets.js +++ b/client/coral-admin/src/actions/assets.js @@ -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) => { diff --git a/client/coral-admin/src/constants/assets.js b/client/coral-admin/src/constants/assets.js index b13534726..8927b351d 100644 --- a/client/coral-admin/src/constants/assets.js +++ b/client/coral-admin/src/constants/assets.js @@ -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'; diff --git a/client/coral-admin/src/services/talk-adapter.js b/client/coral-admin/src/services/talk-adapter.js index 1b824c13e..dea4d7cb2 100644 --- a/client/coral-admin/src/services/talk-adapter.js +++ b/client/coral-admin/src/services/talk-adapter.js @@ -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})); }; diff --git a/tests/client/coral-admin/reducers/assets.js b/tests/client/coral-admin/reducers/assets.js index cf3e0dac4..2a3c15b45 100644 --- a/tests/client/coral-admin/reducers/assets.js +++ b/tests/client/coral-admin/reducers/assets.js @@ -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: [ { diff --git a/tests/client/coral-admin/services/talk-adapter.js b/tests/client/coral-admin/services/talk-adapter.js index 27ae8efcb..b30ae3905 100644 --- a/tests/client/coral-admin/services/talk-adapter.js +++ b/tests/client/coral-admin/services/talk-adapter.js @@ -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'); }); }); });